Why to avoid using Workflow rule and Process Builder field update with Trigger

Consideration before using Trigger, Workflow and Process builder field update at same time in Salesforce

Workflow rule and Process builder are the tools because of which Salesforce Admins can perform so many tasks, which were only possible by writing Triggers. Before release of Process builder, if we wanted to perform field update on child records or post chatter messages or auto execute Visual Flow, Triggers were used by developers.

As a best practice in Salesforce, it is always suggested that we should prefer “point and clicks” over code. Indirectly, prefer Workflow rule or Process builder over Triggers.

Trigger Workflow and Process Builder in Salesforce
Trigger, Workflow and Process Builder in Salesforce

In this post, I will explain a scenario where above statement will not stand true.

Using Trigger

Lets consider that there is a trigger written on custom object “Vehicle__c”. For simplicity, below trigger just have some debug statements.

trigger Vehicle_Trigger on Vehicle__c (before insert, before update, after insert, after update) {
    
    if(Trigger.isBefore){
        //some other code and SOQL statements
        System.debug('******** - In Before Update Trigger');
    }
    if(Trigger.isAfter){
        //some other code and SOQL statements
        System.debug('******** - In After Update Trigger');
    }

}

Whenever any record of custom object “Vehicle__c” is updated or created, above trigger will execute “two times” in before and after event. To know more about sequence of execution of trigger, you can refer this link.

output :

******** - In Before Update Trigger
******** - In After Update Trigger

Trigger and Workflow field update

Let’s say after sometime, requirement came to calculate and update field on Vehicle object. Considering timeline to deliver this requirement, team decided to choose Workflow field update over modifying existing trigger. Consider that workflow rule is written to execute every time when record is either created or updated.

Now, whenever any record of custom object “Vehicle__c” is updated or created, chances that trigger will execute will be “four times” because of sequence of execution.

When record is created or updated trigger will execute twice (before and after event) and after that workflow field update will cause trigger to execute again (before and after event).

output :

******** - In Before Update Trigger
******** - In After Update Trigger
... WF rule operation if condition meets ...
******** - In Before Update Trigger
******** - In After Update Trigger

Trigger, Workflow and Process builder field update

To make situation worst, if we write field update on same object “Vehicle” in Process builder also then trigger may execute “six times” instead of only two times.

In this case, whenever any record is created or updated on custom object “Vehicle__c” then trigger will execute twice after that workflow field update will cause its execution again. After workflow field update, process builder will update record again causing execution of trigger again.

Difference between Workflow and Process builder field update

If field update is done using Process builder, then record will go through complete Save cycle again.

If field update is done using Workflow field update, then Custom validation rules, duplicate rules, and escalation rules will not run again. Read here more.

We can avoid multiple execution of trigger by using static variable (Question 26). However, if trigger is already written then instead of creating workflow or process builder field update, its wise decision to use existing trigger for field update. Multiple execution of trigger could cause hitting various governor limits like SOQL query or concurrent apex limit.

output :

******** - In Before Update Trigger
******** - In After Update Trigger
... WF rule operation if condition meets ...
******** - In Before Update Trigger
******** - In After Update Trigger
... Process builder if condition meets ...
******** - In Before Update Trigger
******** - In After Update Trigger

Roll-Up Summary Field and its implications

If trigger is written on Master object and “Roll-Up summary field” is created on it. If child record creation , updation causes Roll-Up summary field to recalculate, then parent record undergoes through save operation causing its trigger to execute again.

In short, having trigger, workflow and process builder field update on same object at same time (same condition) is not a good idea and it should be analysed carefully before deploying to production.

Related posts

13 thoughts on “Why to avoid using Workflow rule and Process Builder field update with Trigger”

  1. Then the purpose of using declarative features (as possible) defeated easily.

    One of my client has 2 processes, 40+ workflows (field updates& email alerts), one trigger(9 SOQL) for single object.

    Working on removing the process and subsequently all the WFS ;( Thanks a lot for this post, feel like a dumb after seeing this.

    1. WF and Process builder can do some other operations than Field update. If there is field update and no trigger then declarative is suggested. But in your case, it seems you need to move WF field updates in trigger itself. I am glad this post came handy for you.

  2. Consider that many trigger operations could be moved to Declarative process builder or process builder apex actions through InvocableMethods. I think those are processed asynchronously in a different cycle or is that incorrect ?

    1. Tested “InvocableMethod” scenario as well, However It executed “synchronously”. Checked documentation as well, it doesn’t says that it executes asynchronously.

  3. Hi Sir,
    I am a beginner in salesforce. Can you please explain me what is the difference between Process builder’s submit for approval and Approval processes? If a record is submitted through process builder, to whom it will be assigned for approval?

  4. Hi,

    Using process builder, can we create a record with a specific owner and also send email alert to that owner in the same transaction..i created a process builder which creates another record and also assigns to the right owner as specified in the field mappings, but the email alert after the create record is not triggered. Appreciate any help or pointers on this.
    Srini

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.