loading... Salesforce Cat

Sunday, October 12, 2014

Salesforce Work Flow Field Updates make Triggers Run Twice

Salesforce Workflow Rule Field Updates are one of the most useful features provided by the platform for making updating record field values.They are simple and easy to use  but among all positives there is one major downside..That is Workflows make your trigger run twice for a single update

  • First time for the actual record update
  • Second time for the Workflow field update
To find more details please refer Apex Triggers Order Of Execution . Now you may be wondering Is this bad? . Well it depends on whether you have apex trigger(s) on the object workflow is defined and what that trigger(s) does.If you don't have any triggers on workflow related object there  is nothing to worry but if you have they may have some negative side effects such as
  • make your code reach governor limits -for an example if your field update cause your trigger to run 20 Queries with workflow updates it may consume 40.
  • Logic Errors - if your trigger send mails or do DML operations , those operations  will get duplicated which results in sending duplicate mails and duplicate record inserts or updates.
  • Performance : running same logic twice may hardly do any favors to the application performance. 
To avoid trigger code's duplicate execution you can use Class Level Static Variable
 global Class SingleExecution 
 {  
    public Static boolean HasTriggerAlreadyRun = false;  
    public static boolean HasAlreadyExecuted()  
    {  
      return HasTriggerAlreadyRun ;  
    }  
    public static void SetHasAlreadyExectued()  
    {  
     HasTriggerAlreadyRun = true;   
    }  
 }
  
 Trigger MyobjectTrigger on MyObject__c (before update )  
 {  
   if(SingleExecution.HasAlreadyExecuted())  
   {  
    return;  
   }   
   else  
   {  
    //execute your logic  
   SingleExecution.SetHasAlreadyExectued()  
   }  
 }  

Note :- Checking Old and new values on record fields will not prevent workflow field update related second trigger excection as Salesforce States "Trigger.old contains a version of the objects before the specific update that fired the trigger. However, there is an exception. When a record is updated and subsequently triggers a workflow rule field update, Trigger.old in the last update trigger won’t contain the version of the object immediately prior to the workflow update, but the object before the initial update was made. For example, suppose an existing record has a number field with an initial value of 1. A user updates this field to 10, and a workflow rule field update fires and increments it to 11. In the update trigger that fires after the workflow field update, the field value of the object obtained from Trigger.old is the original value of 1, rather than 10, as would typically be the case."


Learn more about WorkFlow Rule Field Updates Here

  


1 comment:

  1. The static variable approach will not work with AllOrNone = false use cases (e.g. Data Loader) and where the batch has 1+ successes and 1+ failures. This is because when SFDC retries the successes after putting aside the failures (see doc), the database is rolled back but the static variable is not reset and when the trigger is re-executed in the retry, the static variable will indicate work is already done and the trigger will do nothing. Your database will be left inconsistent.

    ReplyDelete