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
shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com 

Update trigger getting fired twice --Please help!!!

Hi ,

   As pe salesforce order of execution  Before After trigger fires twice, if a workflow field update is there after 1st fire.

 

Before After trigger

Workflow rule

 

Before After trigger

 

 

Thebeforeandaftertriggers fire one more time only if something needs to be updated.

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_order_of_execution.htm

 

 

I have written a trrigger on opportunity to create a task after update. Opportunity also has multiple field update on it.Bexcause of this this trigger is getting fired twice and inserting two tasks.

I wanna avoid insertion of second task, please suggest me a way out here

 

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
Vinit_KumarVinit_Kumar

Shrey,

 

You can use TriggerHelper class to control the firing of Trigger recursively.

 

Please find below code : -

 

public class TriggerHelperClass {

private static boolean flagvalue = false;


public static boolean hasAlreadyfired() {
return flagvalue;
}

public static void setAlreadyfired() {
flagvalue = true;
}


}

 

and in your Trigger you can use it as below :-

 

trigger myTrigger on Task (before insert) {


if (!TriggerHelperClass.hasAlreadyfired()) {

---write your code here ---


TriggerHelperClass.setAlreadyfired();
}
}

 

 

All Answers

sfdcfoxsfdcfox

That's explained in the docs somewhere (I forget where, exactly...) Basically, a worlfkow field update will cause a secondary invocation of a trigger. You should use a static variable to see if your code has already run, and if so, abort early.

Vinit_KumarVinit_Kumar

Shrey,

 

You can use TriggerHelper class to control the firing of Trigger recursively.

 

Please find below code : -

 

public class TriggerHelperClass {

private static boolean flagvalue = false;


public static boolean hasAlreadyfired() {
return flagvalue;
}

public static void setAlreadyfired() {
flagvalue = true;
}


}

 

and in your Trigger you can use it as below :-

 

trigger myTrigger on Task (before insert) {


if (!TriggerHelperClass.hasAlreadyfired()) {

---write your code here ---


TriggerHelperClass.setAlreadyfired();
}
}

 

 

This was selected as the best answer
shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com

Thanks a lot Vinit, this solves my problem!!!!!

Suresh RaghuramSuresh Raghuram

Hi Vinit,

 

Does it support batch process. What i want to know is if this trigger to be fired under batch update or insert, what happens.

 

Thanks in advance.

Vinit_KumarVinit_Kumar

Yes Suree,

 

It would support batch process as well.Please make sure that while calling any Apex batch class from Trigger,you are not having any future method.

Eric KintzerEric Kintzer
The static variable solution will not work if the trigger is invoked when AllOrNone header = false (Data Loader for example) and one or more records in the batch fail. This is because SFDC will rollback any updates from the successes, and retry the records in the batch that succeeded. But the static variable is not reset in the retry and your trigger will then end up doing nothing and your database will be inconsistent.