Skip to content

Instantly share code, notes, and snippets.

View SalesforceBobLightning's full-sized avatar
💭
Easy-peasy

Salesforce Bob Lightning SalesforceBobLightning

💭
Easy-peasy
View GitHub Profile
@SalesforceBobLightning
SalesforceBobLightning / FormattingDateAndDateTimeHelpers.cls
Created July 19, 2018 12:49
Apex Date & DateTime Formatting Helpers
// Useful Apex snippets for formatting date and datetime to String
String value = String.format('{0}T00:00:00', new List<String> {DateTime.newInstance(Date.today(), Time.newInstance(0, 0, 0, 0)).format('YYYY-MM-dd')});
System.debug(value);
// expected output:
// YYYY-MM-ddT00:00:00
@SalesforceBobLightning
SalesforceBobLightning / TestClassWithMultipleCallouts.cls
Created July 19, 2018 15:50
Using Multiple Callouts in a Single Unit Test
@isTest
public class TestClassWithMultipleCallouts {
@isTest
public static void testMethodMultipleCallouts(){
String contactResource = 'ContactsMock';
String invoiceResource = 'InvoicesMock';
StaticResourceCalloutMock contactMock = getStaticResourceCalloutMock(200, contactResource);
@SalesforceBobLightning
SalesforceBobLightning / ChangeRecordTypeAction.cls
Last active July 21, 2018 16:18
Change Object Record Type via Salesforce Process Builder using Record Type Developer Name
public class ChangeRecordTypeAction {
Private static final String RECORD_TYPE_ID = 'RecordTypeId';
@InvocableMethod(
label = 'Change Object Record Type'
description = 'Change Record Type by Developer Name'
)
public static List<Response> execute(List<Request> requests) {
@SalesforceBobLightning
SalesforceBobLightning / SimpleEmailAction.cls
Created July 26, 2018 09:25
Simple InvocableMethod for Sending Email using Email Template Developer Name via Process Builder
public class SimpleEmailAction {
@InvocableMethod(
label='Simple Email Action'
)
public static List<Result> sendEmails(List<Request> requests) {
List<Result> results = new List<Result>();
for(Request request : requests){
@SalesforceBobLightning
SalesforceBobLightning / BigClass.cls
Created July 26, 2018 09:46
Increase your Salesforce Apex code coverage by cheating
/*
* Having problems with test coverage, this will fix it
*/
public class BigClass {
public static String getValue(String input){
String output;
output = input;
@SalesforceBobLightning
SalesforceBobLightning / CustomExceptionWithoutConstructor.cls
Created July 26, 2018 22:33
Custom Salesforce Apex Exception Class Without Constructor
private class CustomExceptionWithoutConstructor extends Exception {
public override String getMessage() {
List<String> args = new String[]{super.getMessage()};
return String.format('Your custom message here with one argument: {0}', args);
}
}
@SalesforceBobLightning
SalesforceBobLightning / CustomExceptionWithConstructor
Created July 26, 2018 22:36
Custom Salesforce Apex Exception With Constructor
private class CustomExceptionWithConstructor extends Exception {
Private Object obj;
public CustomExceptionWithConstructor(Object obj){
this.obj = obj;
}
public override String getMessage() {
List<String> args = new String[]{obj.property1, obj.property2};
return String.format('Custom exception message with multiple arguments: {0} and {1}', args);
}
}
@SalesforceBobLightning
SalesforceBobLightning / SendEmailAndPDFAction.cls
Last active July 26, 2018 22:48
Generate PDF using Email Template Developer Name and Docomotion Form Name with and send as email attachment via Salesforce Process Builder. Rather than using Email Template ID and Docomotion Form ID which change per Org
/**
* @description: Using Docomotion: create PDF and send via Email from Process Builder using Email Template Developer Name and Form Name
* @documentation: https://www.docomotion.com/support/generating-documents/silent-mode-generating-output-through-an-api/?highlight=API
**/
public class SendEmailAndPDFAction {
@InvocableMethod(
label='Send Email with PDF'
description = 'Send email with PDF attachment, which takes an Form Name, Email Template Developer Name, who Id and What Id'
)
@SalesforceBobLightning
SalesforceBobLightning / GeneratePDFAction.cls
Created July 26, 2018 22:48
Generate PDF from a Docomotion Form Name via Salesforce Process Builder. Rather than using Docomotion Form ID
/**
* @description: Using Docomotion: create PDF via Process Builder using Form Name. Rather than using Form IDs which are different in each Org
* @documentation: https://www.docomotion.com/support/generating-documents/silent-mode-generating-output-through-an-api/?highlight=API
**/
public class GeneratePDFAction {
@InvocableMethod(
label='Custom Generate PDF'
description = 'Generate PDF using a Form Name and What Id'
)
@SalesforceBobLightning
SalesforceBobLightning / makeHttpCallout.cls
Created July 28, 2018 16:01
Making Http Callout using Salesforce Apex, with PATCH workaround
public static HttpResponse makeHttpCallout(String url, String method, String body, Map<String,String> headers){
HttpRequest request = new HttpRequest();
Http http = new Http();
if(method == 'PATCH'){
request.setMethod('POST');
url += '?_HttpMethod=PATCH';
} else {
request.setMethod(method);