James Derulo's

Portfolio

Controlling Recursive Triggers | How to invoke Trigger only once

1 comment
You may come across a situation where in a trigger you update a field which in result invoke a workflow. On successful running of workflow (action) may occurs (field update) and hence the trigger is invoked twice.

Triggered when fired twice, could lead you in trouble of updating data twice. I came across the same situation today and developed work-around

In my code, I was updating a trigger I update field which bring workflow  in action. The workflow update another field, which in result call Trigger twice. While invoking trigger, I create task every time and henceforth I end up create two task in place of one.

To handle this situation, the class below helped me to write controlled trigger. Not sure if you came across this situation, but I believe this could be of help

This helper class will stop the recursive calling of Trigger or calling trigger twice


How to implement this helper class

Add this piece of code which you want to invoke only once inside the Trigger 


Here I want to invoke the method 'CheckOwnerChange' only once hence I am using this helperclass code to flag it after using it once

Pretty simple, this is more of flagging and can be reused at bunch of places

Read more on controlled trigger here

Next PostNewer Post Previous PostOlder Post Home

1 comment:

  1. Alternatively you could use ApexCodePatrol this allows you to do the same but for multiple triggers/methods etc..

    http://code.google.com/p/apex-code-patrol/

    ReplyDelete