+ Start a Discussion
S_2013S_2013 

Field Update causes Trigger to invoke twice

Hi,

I have around 15 field updates through WF that update Opportunity record, and also a trigger that works after update of opportunity. (I cannot incorporate the field update logic into my trigger due to various reasons). Now everytime the Opportunity is updated, the trigger is fired, and then the WF field update happens that fires the trigger a second time which is giving absolutely undesired. I tried creating a flag field on Opportunity that tracks if the trigger is fired from a WF field update, but even that doesn't help, as, I cannot reset back the field (i cant do that from after update block of trigger).

Any suggestions will be greatly helpful.

Regards

Sankalita

Avidev9Avidev9
Have a look at this post http://boards.developerforce.com/t5/Apex-Code-Development/Help-with-Recurssion-Error/m-p/620943#M114359 . This explains how to use a static variable to stop recursion, this will help you to solve the issue
Rajesh SriramuluRajesh Sriramulu

Hi,

 

 

Create one class that contains static boolean variable and set it false.

 

check  it before enters into trigger. i.e Classname.Booleanvariable=false;{

 

Classname.Booleanvariable=true;

............

........

}

this will stop recursion.It will not fire again.

 

Regards,

Rajesh.

S_2013S_2013

Thank you both for replying.

Actually as it is a trigger and bulk handling is necessary, I cannot use static variable, as there is one signle copy of the variable and as far as I can think probably it will give unexpected results in parallel updates from multiple users or bulk updates from triggers...

 

Avidev9Avidev9
I dont think this will cause a problem in bulk handling situation. I guess the code needs to be executed only once per transaction.

Ans also after a batch is processed all the static variables a reset
Rajesh SriramuluRajesh Sriramulu

Hi,

 

As Avi(Avidev9) said is correct. It will take as one trasaction of no. of records is done on dml statemensts.

 

Regards,

Rajesh.

S_2013S_2013
Ok if static variables are reset then this will work for me. I will try out
this one... Thanks both of you!