Skip to content

Instantly share code, notes, and snippets.

@gbutt
gbutt / ScheduledDispatcher.cls
Last active February 6, 2022 15:24
apex scheduled dispatcher - this is how you can schedule code in SFDC without locking up all your classes
/***
Adapted from the great Dan Appleman.
For more on this and many other great patterns - buy his book - http://advancedapex.com/
This class can be used to schedule any scheduled job without risk of locking the class.
DO NOT CHANGE THIS CLASS! It is locked by the scheduler. Instead make changes to ScheduledHelper or your own IScheduleDispatched class
To use:
1) Create a new class to handle your job. This class should implement ScheduledDispatcher.IScheduleDispatched
2) Create a new instance of ScheduledDispatcher with the type of your new class.
3) Schedule the ScheduledDispatcher instead of directly scheduling your new class.
See ScheduledRenewalsHandler for a working example.
@gbutt
gbutt / ConfigLoader.java
Created May 16, 2014 20:46
configloader
package com.example;
import org.apache.commons.lang.NullArgumentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public static String buildSoapLogin(String username, String password) {
def builder = new StreamingMarkupBuilder()
builder.encoding = 'UTF-8'
def soapEnv = builder.bind {
mkp.xmlDeclaration()
mkp.declareNamespace(soapenv: SOAP.SOAP11_NS)
mkp.declareNamespace(tns: 'urn:partner.soap.sforce.com')
mkp.declareNamespace(ens: 'urn:sobject.partner.soap.sforce.com')
'soapenv:Envelope' {
'soapenv:Header' {
@gbutt
gbutt / TasksUpdateSalesActivity.cls
Created July 18, 2016 16:43
Apex class for updating last sales activity and next sales activity when tasks are created or updated
public with sharing class TasksUpdateSalesActivity {
public static final String STATUS_COMPLETED = 'Completed';
@TestVisible
private Task[] newLeadContactTasks {
get {
if (newLeadContactTasks == null) {
newLeadContactTasks = new Task[]{};
// process trigger.new and only return tasks associated with a lead or contact
@gbutt
gbutt / LoadAngularFromLocalhost.js
Created November 8, 2016 02:26
Load angular app from localhost, and fallback to a static resource
var j$ = jQuery.noConflict();
if ({! debugEnabled }) {
loadSourcesLocalhost().fail(loadSourcesSfdc);
} else {
loadSourcesSfdc();
}
function loadSourcesLocalhost() {
return j$.ajax({url: '//localhost:3000/vendor.js', dataType: 'script', timeout: 100})
@gbutt
gbutt / PharmcasImportController.cls
Last active November 24, 2020 15:24
PharmCAS download for Salesforce
/*
PharmCAS Import Controller - Used by VF Page pharmcasImport
This controller mostly offers some RemoteAction methods to our Visualforce page
Most of the Remote Actions act as proxies for web service calls to the WebAdMIT system in order to work around CORS restrictions.
e.g. initiateExport, checkExportStatus, and downloadExport
upsertApps is the main action on this controller. It will receive a list of WebAdMIT records and process them in Salesforce.
For more info on this refer to the Technical Documentation at https://docs.google.com/document/d/edit
*/
@gbutt
gbutt / soql2csv.sh
Last active April 5, 2019 22:43
SFDX convert SOQL into CSV
#!/bin/sh -e
function printHelp() {
echo "Incorrect usage. Example:\n\t./soql2csv.sh \"SELECT Id, Name FROM Account LIMIT 10\" myorg > accounts.csv";
}
if [ ! -e "`which jq`" ]; then
echo "You need to install jq to use this script."
if [[ "`uname`" == "Darwin" && -e "`which brew`" ]]; then
echo "Try running 'brew install jq'"
@gbutt
gbutt / _sfdx
Created November 8, 2017 05:20
SFDX completions for oh-my-zsh
#compdef sfdx
# SFDX Autocomplete plugin for Oh-My-Zsh
# Requires: The SFDX client CLI
# Author: gbutt
# Usage: add this file to ~/.oh-my-zsh/plugins/sfdx, and then add sfdx to your plugins in ~/.zshrc
local -a _1st_arguments
_1st_arguments=(
"force\:alias\:list":"list username aliases for the Salesforce CLI"
@gbutt
gbutt / load-data.sh
Created November 30, 2017 14:53
Get Record Types for Scratch Org Data Load
#!/bin/sh
mkdir tmp
# get all record types and save to a file
sfdx force:data:soql:query --query "SELECT ID, DeveloperName, SObjectType FROM RecordType WHERE IsActive = true" > tmp/recordtypes.txt
# get record type id for account record type with developer name = Educational_Institution
RTID_ACCOUNT_INSTITUTION=`cat tmp/recordtypes.txt | awk ' $3 ~ /Account/ && $2 ~ /Educational_Institution/ { print $1 } ' `
@gbutt
gbutt / TestDataFactory.cls
Last active October 6, 2018 16:15
Apex TestDataFactory
public class TestDataFactory {
public class SObjectGenerator {
public Integer seed = 0;
private Map<String,Object> fields;
private SObjectType sobType;
public SObjectGenerator(SObjectType sobType, Map<String,Object> fields) {
this.fields = fields;
}