Workflow Field Updates vs Process Builder Field Updates vs Trigger in Salesforce

Triggers
The trigger can work across objects.
Coding is required.
The trigger works before and after some actions.
DML Operations(insert, update, delete, etc.) are not supported.
Triggers can handle delete and undelete DML operations.
We cannot develop directly in production.

Workflow Field Updates
Workflow Field Updates will be helpful to update the same object or master object in a custom master-detail relationships.
Coding is not required.
Workflows work only after some actions.
DML Operations(insert, update, delete, etc.) are supported. Note: Task creation alone is supported.
Workflows cannot handle delete and undelete DML operations.
We can develop directly in production.

Process Builder Field Updates
Process Builder Field Updates can work across related objects.
Coding is not required.
Process Builders work only after some actions.
DML Operations(insert, update, delete, etc.) are supported.
Process Builder cannot handle delete and undelete DML operations.
We can develop directly in production.

Things to note:

1. The trigger is executed first.

2. If the record was updated with the workflow field update, it fires before and after triggers one more time. Custom validation rules, duplicate rules, and escalation rules are not run again.

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

It’s always important to keep the order of these events in mind when developing as it can cause unintended consequences and a debugging nightmare.

Avoid interweaving Apex, Workflow, and Processes together for the same process. As Workflows and Triggers don’t always place nice together so holds true for Processes. Even if you do get them to work, it’s a bad practice to spread logic over multiple methods. Keep it all together in one place so you can see what’s happening down the entire flow.

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

Let's see what happens and how many times trigger will be executed when we have multiple field updates and process builders.

Sample Trigger:


  1. trigger AccountTrigger on Account ( before insert, before update, after insert, after update ) {  
  2.   
  3.     if ( trigger.isBefore) {  
  4.           
  5.         if ( trigger.isInsert )   
  6.             system.debug('Before Insert');  
  7.         else if ( trigger.isUpdate )   
  8.             system.debug('Before Update');  
  9.               
  10.     } else if ( trigger.isAfter ) {  
  11.           
  12.         if ( trigger.isInsert )   
  13.             system.debug('After Insert');  
  14.         else if ( trigger.isUpdate )   
  15.             system.debug('After Update');  
  16.       
  17.     }  
  18.       
  19. }  

Field Updates:



Process Builders:



Debug log of Trigger Execution

When a new Account record is created:



When a Account record is updated:



No comments:

Post a Comment