Andy in the Cloud

From BBC Basic to Force.com and beyond…

Disabling Trigger Events in Apex Enterprise Patterns

7 Comments

Iautobat.jpeg‘m proud to host my first guest bloggerChris 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.

7 thoughts on “Disabling Trigger Events in Apex Enterprise Patterns

  1. 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.

    • 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.

  2. Thanks, Really helpful article. It was not obvious how to skip triggers.

  3. 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

  4. 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s