Prevent Flow Updates With Triggers

August 11, 2017 Appirio

By Nitish Bansal

In this blog, we will discuss some considerations of using Salesforce Workflow field update or Salesforce Process Builder record update, along with Salesforce’s Apex Trigger.

Before the release of Process Builder, if a Salesforce admin or developer wanted to execute operations (like posting Salesforce Chatter messages, or enforcing an auto-execution of Salesforce Visual Flow, or performing field updates on child records), they could only use triggers. Now, Process Builder is the preferred tool of Salesforce Admins. This tool performs multiple specific tasks, which are otherwise only possible via a trigger.

Using automation tools simultaneously

As a Salesforce best practice, we always recommend building “point and clicks” over code (i.e. prefer Workflow or Process Builder, over an Apex Trigger).  

However, there’s one limitation to the statement above — which occurs when a user prompts simultaneous field updates and triggers. Let us consider if there is an Apex Trigger written on a custom object “Blog__c”. (For simplicity sake in this example, the trigger only includes the debug statements.)

Now, let’s assume a record is updated within the custom object “Blog__c”,  causing the trigger in the image above to execute twice (once in “before update”, and once in “after update”). Let’s take a closer look at this trigger’s debug logs.

To know more about the execution sequence of a trigger, refer to this link.

Apex Trigger with Workflow Field Update

When a requirement to calculate and update a field on “Blog__c” object occurs, and depending on the delivery timeline, the team chooses “Workflow Field Update” over modifying the existing trigger. In this situation, it’s important to remember that the workflow rule is executed whenever an old record is updated, or a new record is created.

Now, whenever any record of custom object “Blog__c” is updated or created, the trigger will most likely execute four times, because of the execution sequence you’ve created. For instance, when a record is updated, the trigger will execute twice (before and after the event). In addition, the “Workflow Field Update” will cause the trigger to execute again (before and after the event).

Here’s a code snippet of the Apex Trigger and Workflow field update debug logs:

Apex Trigger with Process Builder

Let’s assume the same requirement from the section above was implemented via Process Builder. Again, the trigger will execute four times.

If the record update is done using Process Builder, the record will then go through the entire save cycle again. But if record update has completed using “Workflow Field Update”, then specific rules are skipped (like custom validation rules, duplicate rules, and escalation rules). For more details, refer this link.

Prevent multiple triggers

Multiple trigger executions could cause various governor limitations (like “too many SOQL”). We can use static variables to avoid triggering multiple executions. However, if the trigger is already written on an object, it’s wise to use the existing trigger for the record update (instead of creating a new Workflow, or triggering a Process Builder record update).

In short, including a simultaneous use of Apex Trigger, Workflow Field Update, and Process Builder Record update on the same object is not a good idea. Generally, it should be avoided.

Previous Article
Is Selenium the Right Automation Testing Tool For You?
Is Selenium the Right Automation Testing Tool For You?

By Niza Khunger People are sometimes confused about what automation testing tools are out there, and which ...

Next Article
Salesforce and Acquia Drupal Integrate
Salesforce and Acquia Drupal Integrate

By Pankaj Mehra Drupal is a free CMS platform. Acquia is a company which provides a mix of Drupal services....