Skip to content

Instantly share code, notes, and snippets.

View douglascayers's full-sized avatar

Doug Ayers douglascayers

View GitHub Profile
@douglascayers
douglascayers / VisualforceCsvExample.html
Last active December 31, 2020 16:55
Visualforce CSV Example. Note the 'contentType' page attribute and the # to specify the file name.
<!--
This page must be accessed with an Account ID in the URL.
For example: https://<salesforceInstance>/apex/ContactsCsvPage?id=001D000000JRBet
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_styling_content_type.htm
-->
<apex:page standardController='Account' contentType='application/vnd.ms-excel#contacts.xls'>
<apex:pageBlock title='Contacts'>
<apex:pageBlockTable value='{!account.Contacts}' var='contact'>
<apex:column value='{!contact.Name}'/>
<apex:column value='{!contact.MailingCity}'/>
@douglascayers
douglascayers / ApexCsvExample.java
Created January 2, 2017 18:58
Apex CSV Example. Note the use of String.escapeCsv() method
String csv = 'Id,Name\n';
for ( List<Account> accts : [ SELECT id, name FROM Account LIMIT 10 ] ) {
for ( Account acct : accts ) {
csv += acct.id + ',' + acct.name.escapeCsv() + '\n';
}
}
ContentVersion file = new ContentVersion(
title = 'accounts.csv',
versionData = Blob.valueOf( csv ),
@douglascayers
douglascayers / SendEmailBatchable.java
Created January 4, 2017 05:32
Example Apex classes to enable Process Builder to send emails without using Email Alerts + Templates or launching a Flow.
/**
* Developed by Doug Ayers
* douglascayers.com
*
* Designed to be used by SendEmailInvocable class when sending
* several emails but need to stay within the apex governor limits
* of how many emails can be sent per transaction. Call this batchable
* with all the emails to send and set the batch size to max per transaction.
* https://developer.salesforce.com/docs/atlas.en-us.salesforce_app_limits_cheatsheet.meta/salesforce_app_limits_cheatsheet/salesforce_app_limits_platform_apexgov.htm
*
@douglascayers
douglascayers / FeedItem.java
Created January 14, 2017 21:37
Example creating FeedItem in Apex and setting the author. Note, requires the "Insert System Field Values for Chatter Feeds" permission.
FeedItem item = new FeedItem(
parentId = UserInfo.getUserId(), // where to post message
createdById = '005j000000Bz31U', // author to impersonate
body = '<p><b>FeedItem</b> supports <i>rich text</i> but not @ mentions:</p> @[0F9j00000008TNc] @0F9j00000008TNc {0F9j00000008TNc} @{0F9j00000008TNc}',
isRichText = true
);
insert item;
@douglascayers
douglascayers / GetUserPermissionSetsInvocable.cls
Last active January 4, 2018 08:17
Invocable Apex class that retrieves custom permission sets assigned to a user.
/**
* Designed to be used in Flow to retrieve all the custom permission sets
* assigned to the given user ids. Excludes permission sets tied to profiles.
*/
public with sharing class GetUserPermissionSetsInvocable {
@InvocableMethod(
label = 'UPS: Get User Permission Set Assignments'
description = 'Get User Permission Set Assignments'
)
@douglascayers
douglascayers / PrintTriggerEvent.java
Created February 5, 2017 19:37
One-liner to print the trigger context event (e.g. "before insert" or "after delete")
trigger AccountTrigger on Account ( before insert, before update, before delete, after insert, after update, after delete, after undelete ) {
System.debug( 'trigger event: ' + ( Trigger.isBefore ? 'before' : 'after' ) + ' ' + ( Trigger.isInsert ? 'insert' : Trigger.isUpdate ? 'update' : Trigger.isDelete ? 'delete' : 'undelete' ) );
}
@douglascayers
douglascayers / AttachmentTrigger.java
Last active February 27, 2017 06:31
Example triggers for detecting when links to Chatter Files are deleted.
trigger AttachmentTrigger on Attachment ( before delete ) {
Set<ID> parentIds = new Set<ID>();
for ( Attachment a : Trigger.old ) {
parentIds.add( a.parentId );
}
System.debug( parentIds );
@douglascayers
douglascayers / query-by-id
Last active December 2, 2020 14:05
Example SOQL to query for all fields using Dyanamic Query in Apex
ID recordId = '5001a00000CgCE2';
DescribeSObjectResult describeResult = recordId.getSObjectType().getDescribe();
List<String> fieldNames = new List<String>( describeResult.fields.getMap().keySet() );
String query =
' SELECT ' +
String.join( fieldNames, ',' ) +
' FROM ' +
@douglascayers
douglascayers / query-by-sobject
Last active March 24, 2018 17:11
Example SOQL to query for all fields using Dyanamic Query in Apex
// without an ID, simply specify the object to then derive the sobject type
DescribeSObjectResult describeResult = Account.getSObjectType().getDescribe();
List<String> fieldNames = new List<String>( describeResult.fields.getMap().keySet() );
String query =
' SELECT ' +
String.join( fieldNames, ',' ) +
' FROM ' +
describeResult.getName()
@douglascayers
douglascayers / SampleOpportunityController.java
Created March 14, 2017 19:49
Example Visualforce page that shows Opportunities with or without Contact Roles.
public with sharing class SampleOpptyController {
public List<Opportunity> opportunities { get; set; }
public SampleOpptyController() {
this.opportunities = queryOpportunities();
}
private List<Opportunity> queryOpportunities() {
return [