Skip to content

Instantly share code, notes, and snippets.

View sbutterfield's full-sized avatar

Butters sbutterfield

  • Salesforce
  • Vancouver, British Columbia
View GitHub Profile
@sbutterfield
sbutterfield / TriggerHandler.cls
Created February 3, 2024 22:41
Comprehensive trigger handler
public without sharing virtual class TriggerHandler {
/** VARIABLES **/
// static map of handlername, times run() was invoked
private static Map<String, LoopCount> loopCountMap; //Map of handlername to the number of times run() was invoked
private static Set<String> bypassedHandlers; //Set of handlers who will be bypassed
private List<Business_Unit_Data__mdt> businessUnitDataList; //List of the business unit metadata records for an object
private String objectName; //Api name of the object on which triggger is running
private Map<String,Map<Id,sObject>> oldBusinessRecordMap; //Map of Business Unit name to the corresponding trigger.oldMap
private Map<String,Map<Id,sObject>> newBusinessRecordMap; //Map of Business Unit name to the corresponding trigger.newMap
<?xml version="1.0" encoding="UTF-8"?>
<Flow xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>56.0</apiVersion>
<assignments>
<name>Assign_Knowledge_Prefill_Info</name>
<label>Assign Knowledge Prefill Info</label>
<locationX>176</locationX>
<locationY>758</locationY>
<assignmentItems>
<assignToReference>knowledgeArticle.Title</assignToReference>
@sbutterfield
sbutterfield / getLastItem.js
Created July 31, 2022 17:29
Efficient initializers for _last in map_ function
export const lastItemInMap = map => Array.from(map).pop();
export const lastKeyInMap = map => Array.from(map.keys()).pop();
export const lastValueInMap = map => Array.from(map.values()).pop();
@sbutterfield
sbutterfield / UriUtils.cls
Created June 21, 2022 16:26
apex uri utils for matching and parsing URI & URL groups
/**
* @author: Shawn Butterfield
* Utility class for URI (URL/URN/Email) data validation, parsing and matching
* Methods for pattern extraction defined by URI standard syntax, here: http://en.wikipedia.org/wiki/URI_scheme#Generic_syntax
*/
public class URIUtils {
private static final String REGEX_URL_SCHEME = '^([a-zA-Z][a-zA-Z0-9\\+\\.\\-]+):';
private static final String REGEX_URI_EMAIL_PATTERN = getUrlPatternString();
private static final String[] UNSAFE_CHARS = new String[] { ',','.','&','_','0','Inc','Corp','Co','Ltd' }; // Strings or fragments that can screw up an api request due to encoding
@sbutterfield
sbutterfield / sfdc-datetime-to-groovy2.4-date.groovy
Last active July 31, 2022 17:32
sfdc-datetime-to-groovy2.4-date
import java.text.DateFormat
import java.text.SimpleDateFormat
// def inputDatTimeString = "2015-07-23T13:46:00.000Z"
def inputDatTimeString = "2015-07-23T14:06:00.000Z"
DateFormat inputDateTimeFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX")
// X instead of 'Z'
// inputDateTimeFormat.setTimeZone(TimeZone.getTimeZone("GMT"))
@sbutterfield
sbutterfield / IFrameLWCFlowAttributeChangeEvent.js
Created October 29, 2020 03:02
IFrameLWCFlowAttributeChangeEvent.js The LWC has an embedded iFrame. I believe that the listenForMessage() reference window.addEventListener() call in my connectedCallback() is really within the this context of the iFrame, not the LWC. Therefore, when I set a value in that callback, I'm setting a different value than the one in the LWC. Which is…
import { LightningElement, api, track } from 'lwc';
import { FlowAttributeChangeEvent } from 'lightning/flowSupport';
import pageUrl from '@salesforce/resourceUrl/recaptcha2';
export default class GoogleCapatcha extends LightningElement {
navigateTo;
@api result = "undefined";
connectedCallback(){
var self = this;
@sbutterfield
sbutterfield / JobRunner.cls
Created October 27, 2020 22:36
Apex job runner class to recursively execute scheduled job ever "n" minutes
global without sharing class JobRunner implements Schedulable {
Integer intervalMinutes;
public JobRunner(Integer intervalMinutes) {
this.intervalMinutes = intervalMinutes;
}
public void execute(SchedulableContext sc) {
// Re-schedule ourself to run again in "intervalMinutes" time
DateTime now = DateTime.now();
DateTime nextRunTime = now.addMinutes(intervalMinutes);
String cronString = '' + nextRunTime.second() + ' ' + nextRunTime.minute() + ' ' +