function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kingslee Velu 5Kingslee Velu 5 

Apex Trigger vs Workflow/Process Builder

Give an example of why you would use a trigger over a workflow or process builder to accomplish an update to a field?

Is there any pros/cons apart from below, Please share your thoughts.
  • If we need to perform an update on fields by validating the before/after validations of any other fields save to database then Triggers can be used instead of Workflow/Process Builder
  • Workflow/Process Builder cannot handle delete and undelete DML. Whereas Apex triggers can handle all DML operations.
  • Errors reported in Workflow/Process Builder is more generic which makes it difficult to find the origin of the error. With Apex triggers, exception handling can be made more specific.
  • It is all or none in case of Workflow/Process Builder failure. But with Apex triggers partial success is possible
MagulanDuraipandianMagulanDuraipandian
Check this - https://www.jitendrazaa.com/blog/salesforce/why-to-avoid-using-workflow-rule-and-process-builder-field-update-with-trigger/
Deepali KulshresthaDeepali Kulshrestha
Hi Kingslee,

I hope this will help you to differentiate between Process Builder or Apex Triggers.

1-Process Builder – Pros:
We recommend starting with Process Builder, especially for processes that can be simplified to if/then statements. Below are some ideal scenarios for using it:

-Process Builder can do the following actions without Apex code:
-Create records and set field values
-Update related records
-Create Chatter posts
-Send an email
-Create an approval
-Simple triggers like populating a lookup field based on certain criteria can now be automated in Process Builder
2-Launch an automated flow.
3-Call an Apex class.

Process Builder – Cons:
Below are some limitations to using Process Builder and the advantages of using Apex triggers to address these particular scenarios:

1. Process Builders cannot handle before DML It executes after a record has been created or updated. Whereas Apex triggers can handle both before and after DML operations.
2. Process Builder cannot handle delete and undelete DML. Whereas Apex triggers can handle all DML operations.
3.Validation: Processes do not trigger validation rules and can, therefore, invalidate data.
   An error reported in Process Builder is more generic which makes it difficult to find the origin of the error. With Apex triggers, exception handling can be made more specific.
4. It is all or none in the case of Process Builder failure. But with Apex triggers partial success is possible.
5. Whenever a particular use case is not possible using Process Builder, consider using Apex triggers. For example, consider the below use case:-
   A custom field on a Parent object which is based on the field related to the max (roll-up) among the child records. Since roll-up functionality involves insert, update, delete and undelete operations, using an Apex trigger is the best option in this scenario.
   
   And For Understanding you can refer the video:
   
   https://www.youtube.com/watch?v=ICYV35wH4DI
   
   
   
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Kingslee VeluKingslee Velu
Thanks Deepali and Magulan