Some small utility classes for working with Salesforce Apex.
Using the githubsfdeploy app, add this code to your Salesfore Org.
Retrieve the standard pricebook or standard pricebook Id from anywhere.
Id stdPricebookId = PricebookSingleton.getStdPricebookId();
Pricebook2 stdPricebook = PricebookSingleton.getStdPricebook();
Generic Helpers for Salesforce Apex development.
Schema.SObjectType accountType = ToolBox.getSObjectType('Account');
Schema.SObjectType accountType = ToolBox.getSObjectType('001000000000000');
Id recordTypeId = ToolBox.getRecordTypeId('Case', 'Master');
Map<String, Id> recordTypeIdsByDeveloperName = ToolBox.getRecordTypeIdsByObject('Case');
List<Schema.PicklistEntry> entries = ToolBox.getPicklistEntries('Account', 'Type');
Disable Triggers by Configuration. Very useful for production trigger control.
// User.trigger
trigger User on User (after insert, after update) {
// Determine if the CustomMetadata Record is active.
if(TriggerBypass.isActive('User')) {
// Trigger logic
}
// Will throw TriggerBypass.TriggerBypassException if the CustomMetadata record does not exist.
}
Generate unique Salesforce Record Ids by SObjectType or SObject
// Construct with SObject or SObjectType
MockIdGenerator idGenBySObject = new MockIdGenerator(new Account());
MockIdGenerator idGenBySObjectType = new MockIdGenerator(Account.SObjectType);
// All IDs are unique, and can be invoked as a static method too.
Id newMockId = idGenBySObject.getMockId();
Id anotherMockId = idGenBySObjectType.getMockId();
Id stateslessMockId = MockIdGenerator.getMockId(Account.SObjectType);
System.debug(newMockId); // -> 001000000000001AAA
System.debug(anotherMockId); // -> 001000000000002AAA
System.debug(statelessMockId); // -> 001000000000003AAA