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
JJE_OLDJJE_OLD 

Trigger fired twice on Update

Hi,

I created a custom object, with a status field and workflow rules with are based on this status.
On each status change, the workflow send an email to the object creator
On some status change, the workflow also change the Owner and the record type.

I created a trigger on this object to inform related contacts that the status evolved, but, when the status is updated to a value for which there is a record type and an owner change, the trigger is fired twice and therefore, I get two emails for each contacts.
It works well for others status.

Is there a problem when firing trigger and workflow on the same event?
What should be done to correct this?

regards,

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

If you want your trigger that sends the email to be executed only once per transaction, this link

 

http://www.salesforce.com/docs/developer/cookbook/Content/apex_controlling_recursive_triggers.htm

 

shows how to achieve that.

All Answers

bob_buzzardbob_buzzard

According to the order of execution rules in the Apex developer's guide, after the triggers have been executed, workflow rules are evaluated, workflow field updates take place and then triggers fire one more time if necessary.

 

I'm assuming that your trigger checks for a status change rather than firing in all cases? That being the case, could you also check to see if the record type and owner has changed as well, and then not send the email?  Failing that, I guess you could mark the record has having an email sent and then clear that down using an @future method?

 

 

JJE_OLDJJE_OLD

Thank you for your answer. This seems to be my case.

 

But, if in the future I add a new field update in my workflow rule, will I have to modify my trigger in consequence?

Isn't there a way to take this into account globally?

 

What if I choose to create a before trigger on another status? This will also change the context, no?

 

Are there good practices in these cases, grouping all the updates in one trigger or is it better to use workflow rules? any ideas?

 

regards,

bob_buzzardbob_buzzard

If you want your trigger that sends the email to be executed only once per transaction, this link

 

http://www.salesforce.com/docs/developer/cookbook/Content/apex_controlling_recursive_triggers.htm

 

shows how to achieve that.

This was selected as the best answer
JJE_OLDJJE_OLD

That works! thank you.