I‘m proud to host my first guest blogger! Chris Mail or Autobat as he is known on GitHub. Take it away Chris….
How to put the safety on…
Being an architect in a professional services organisation is a funny game. Each project is either a shiny new Salesforce instance without a fingerprint on it or an unknown vault of code and configuration that we must navigate through.
I have been using the fflib pattern now for some time, and more of our teams are adopting it for our programs of work. My latest addition is something that an architect might wonder why we need; the ability to turn off triggers via a simple interface on all domains.
In an ever growing complex environment, perhaps multiple projects over time delivering iterative enhancements I was noticing a common piece of code being developed within the Domain layer. It looked something along the lines of this:
public override void onAfterInsert() { // if this is set we are already in a loop and want to exit! if(bProhibitAfterInsertTrigger) { return; } // down here we do something, maybe insert an Account! }
While small and inconspicuous it allowed our code base to become inconsistent as there was no control over the exposure of these controlling flags and worse, we were repeating ourselves in every domain!
The solution was simple, a fluent style API within fflib_SObjectDomain. Any code can now simply set the control flags for any domain class:
fflib_SObjectDomain.getTriggerEvent(YourDomain.class).disableAll(); // dont fire anything fflib_SObjectDomain.getTriggerEvent(YourDomain.class).disableAllBefore(); fflib_SObjectDomain.getTriggerEvent(YourDomain.class).disableAllAfter(); fflib_SObjectDomain.getTriggerEvent(YourDomain.class).disableBeforeInsert(); fflib_SObjectDomain.getTriggerEvent(YourDomain.class).disableBeforeUpdate(); fflib_SObjectDomain.getTriggerEvent(YourDomain.class).disableBeforeDelete(); fflib_SObjectDomain.getTriggerEvent(YourDomain.class).disableAfterInsert(); fflib_SObjectDomain.getTriggerEvent(YourDomain.class).disableAfterUpdate(); fflib_SObjectDomain.getTriggerEvent(YourDomain.class).disableAfterDelete(); fflib_SObjectDomain.getTriggerEvent(YourDomain.class).disableAfterUndelete();
To enable, just call the inverse e.g. .enableAfterInsert(); etc.
While not every code base will need to use these flags, they allow you to control quickly and easily your trigger execution with a single line of code that all your development team can reuse and follow.
April 22, 2016 at 3:46 am
Hi Andrew,
I would like to seek your advice regarding on declarative rollup summaries. I have install and have everything setup as per guideline, it was working for sometime but recently i install the latest version 2.3 and setup the same. it does not work after I click on the “Calculate” button under Lookup Rollup Summary record.
I would like to get some insight from you what is the possible issue that could cause the calculation does not work.
April 22, 2016 at 7:29 am
Thanks for reaching out Jobby, it’s best if you post on the chatter group, with some screenshots and more details of the issue, then either myself or a member of the community will likely respond and help.
January 17, 2018 at 5:45 am
Thanks, Really helpful article. It was not obvious how to skip triggers.
April 2, 2018 at 3:39 am
Hi Andrew,
Where should we place below line when we want to disableAll events:
fflib_SObjectDomain.getTriggerEvent(YourDomain.class).disableAll();
We have a requirement to disable all the events based on a custom setting value for that trigger.
Thanks
April 4, 2018 at 7:09 am
Place that in your constructor code
September 22, 2022 at 4:23 am
Hi Andrew,
Is it possible to Disable a subscriber trigger temporarily on a Platform Event and then re-Enable it and no messages are lost between that duration? Your reply is greatly appreciated.
Thanks.
November 28, 2022 at 10:06 pm
Sorry no this is not possible – something needs to subscribe to the events – salesforce does not store them