3

I have a scenario. I am updating some Opportunity records through Data loader which causes after update trigger to run twice and cause issues. It shows that some WF Field update runs again and then the triggers. Now REevaluate triggers is false for all WF field update. There are no errors while uploading from data loader. Also when I update the values using Developer console the after trigger runs only once.

Can you help with the reason for same THanks

2

This is normal behavior for an After Update Trigger that causes workflow to fire. See Item 12 in Triggers and Order of Execution in the Apex Docs.

If you want to prevent your trigger from running it's code a second time, what you can do is create a public static boolean that's checked before the code in trigger runs. If false, the code enters your trigger. If true, the code executes and the code within your trigger doesn't run.

Regardless, your trigger will still be called after the workflow runs the first time. The utility code would looks something like below:

//create a utility class to hold the boolean

public with sharing MyTriggerUtility class {

    public static boolean IsFirstRun = false;

}

At the start of your trigger's Before Update or After Update code, add the following line:

MyTrigger (BeforeInsert,AfterInsert,BeforeUpdate,AfterUpdate){
   Boolean isFirstRun;
   // ... other code
   If(trigger.isUpdate){
      If(!MyTriggerUtility.isFirstRun){
         MyTriggerUtility.isFirstRun = true;
         // ... continue with trigger code
      }// end of FirstRun test
   }// end of Update code section 

} end of trigger
  • 2
    N.B. the static variable will work fine for Data Loader use case as no xact will create more than 200 records for the trigger; however, for execute anonymous, custom rest classes, or any other DML statement that updates > 200 objects, the static variable will prevent the trigger from running on objects 201... – cropredy Feb 5 '16 at 19:17
  • @crop1645 Then will there be any solution for that as I was stuck there only for not using static var – Kaushik Ray Feb 6 '16 at 9:17
  • @KaushikRay. Instead of simple Boolean static, use a static set of ID. – cropredy Feb 6 '16 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.