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.
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.
Leave a Reply