PAD - An Apex Micro Framework
Managing bypass mechanism in Apex
Performance management requires sometimes to avoid running all or part of the code. As an example, if you want to mass update a technical field value for all the records in your SObject, you probably don't want to waste time in running business logic.
PAD is an Apex class that is managing this situation. You can bypass any code by adding the 'bpAll' Custom Permission to the current user, or just bypass a block code by adding the dedicated permission to the running user.
trigger TestBeforeUpdate on Test__c (before update) {
if(PAD.canTrigger('bpjla')){
//call here your business logic
myclass.doSomething(Trigger.new);
}
}
It is as easy as the above code to implement a bypass logic in Apex. Of course, the PAD class should be optimized for performance. And it is. No need to wait more, here is the full code of the class, that you can freely reuse and adapt (most of the time you don't need to change anything).
public class PAD{
//Naming convention : all Custom Permissions starting with 'bp'
//Can be changed - Name of custom permission to bypass everything
private static final String bpAll = 'bpAll';
//Can be changed - List of Apex codes that should run only once. Add any code to the list
private static final Set requiredOnce=new Set();
//List of Apex code that has already been run. Keep this list empty.
private static Set hasRun=new Set();
//List of triggers that can be bypassed
public static String BypassTrigger{get; private set;}
//List of Apex codes that need to be bypassed, dynamically and temporaryly managed by Apex Code
public static Set ApexForcedBypass=new Set();//ex: 'AP123','AP432'
public static Boolean bypassAll=false;//Can be set by Apex Code to avoid running any logic
static{//init() can be called from testMethods too (after a runAs())
init();
}
public static void init() {//Retrieving all custom permissions assigned to the current user
String strTmp='';
BypassTrigger='';
for(CustomPermission cp:[SELECT DeveloperName,
(SELECT Id
FROM Setupentityaccessitems
WHERE SetupEntityType='CustomPermission'
AND ParentId IN (SELECT PermissionSetId
FROM PermissionSetAssignment
WHERE AssigneeId= :UserInfo.getUserId()) limit 1)
FROM CustomPermission
WHERE DeveloperName LIKE 'bp%'])
if(cp.SetupEntityAccessItems!=null && cp.SetupEntityAccessItems.size()>0)strTmp+=cp.DeveloperName+';';
BypassTrigger=';'+strTmp;
}
// return True if the code has to run. False if we must not run the code block
public static Boolean canTrigger(String keyword){//If no bypass
if(requiredOnce.contains(keyword)){//If it should run Once
if(hasRun.contains(keyword))return false;//Already run, should not run
hasRun.add(keyword);//Never run, can run only if not bypassed
}
return(BypassTrigger.indexOf(';'+keyword+';')==-1 && !ApexForcedBypass.contains(keyword) && !bypassAll && BypassTrigger.indexOf(';'+bpAll+';')==-1);
}
}
To use the class and initiate a DML that should not run any apex logic, you will have to write a few lines like this:
boolean tmpOld=PAD.canTrigger('bpAll');
PAD.bypassAll=true;
//do any DML here
PAD.bypassAll=tmpOld;
To keep most of the code running and just avoid running selected blocks, this is the way to do it:
//Switch during the transaction into bpMyLogic bypass, then revert to the original value
boolean bCanTriggerMyLogic=PAD.canTrigger('bpMyLogic');
if(bCanTriggerMyLogic) PAD.ApexForcedBypass.add('bpMyLogic'); //Remove access
//do any DML here
if(bCanTriggerMyLogic) PAD.ApexForcedBypass.remove('bpMyLogic');//restore
PAD is a framework being used on more than 90% of Salesforce projects in France, and lots of Salesforce customers in other countries. After more than 10 years of development and usage, it is still very small.
I'm (JLA) the author of PAD. This page is the official URL where you can find the latest version of the code.
Enjoy!