0

We have difference scenarios to reach account trigger, such as:

  • Users create or update new records
  • External systems upsert via Bulk or SOPA/REST
  • insert or update from another trigger context (such as in contact trigger logic)

Is it possible to define whether to run or pass the trigger logic according to the scenarios? I am thinking of updating custom Boolean fields in Account, but it seems to become very complicated soon.

2

You can determine the context you're in and go from there, but this can be kind of complicated by its own right. Note that this linked answer leverages some rather undocumented features and may break at any time, so use at your own risk.

Generally, if your triggers are decently designed, they should be able to run "all the time". which is ideal, as it enforces your business logic.

For controlling if one trigger fires another trigger, a static variable works just fine.

public class ContactTriggerHandler {
  public static Boolean shouldExecute = true;
  public static void handleBeforeInsert(Contact[] oldRecords, Contact[] newRecords) {
    if(shouldExecute) {
      ...

Remember to reset this variable when you're done:

ContactTriggerHandler.shouldExecute = false;
insert contacts;
ContactTriggerHandler.shouldExecute = true;

If you have a more formal framework, that's okay, too. We have a trigger framework, so we can:

TriggerFramework.disableAll();
insert contacts;
TriggerFramework.enableAll();

You can also use a Custom Setting and set a flag there. A Hierarchy custom Setting can be set at the org, profile, an user level, so a user could individually override trigger execution (and flows, etc!) by setting or resetting a flag on this custom setting. This is ideal for global on/off switches that can be controlled down to the user level.

Finally, if no other solution works, then a custom field might be your only choice. I have seen this used before, and it does work, it's just a bit cumbersome. The upside is that it works in any context, can be controlled per profile, etc.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.