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

Trigger Firing Twice due to a field Update



i have a trigger on Opportunity that fires "after Update" operation and whenever opportunity stage is "Won".

Also, i have a workflow that is being executed , which updates 5 fields whenever stage of the opportunity is set to "Won".


Due to this workflow update, the trigger is fired again. How can i stop this from happening?


1 thing i can think is:

create  another field (preferably check box) and also update this field to "True" as part of workflow update.

And in my trigger check for the field status as "false".


Any other way we could accomplish this without creating a field?


Your help is highly appreciated.


Trigger code:


trigger createEntitlement on Opportunity (after update) {
  List<Entitlement> newEntitlement=New List<Entitlement>();
  Set<Id> oppIds=New Set<Id>();
  for(Opportunity op : Trigger.New){
    if(op.StageName=='Won' && Trigger.oldMap.get(op.Id).StageName!='Won')
  List<Opportunity> oppty=[Select Id,Name,AccountId,CloseDate,Contract_Activation_Date__c From Opportunity Where Id IN:oppIds];
  System.Debug('List Size=='+oppty.size());
  for(Integer i=0;i<oppty.size();i++){
    Entitlement ent=New Entitlement();
    String name='Entitlement For-';
  System.debug('Entitlement Size=='+newEntitlement.size());





Imran MohammedImran Mohammed

I recommend you to have before update in your trigger. In this waay you can prevent your trigger from firing twice.

Also one more thing,  make the changes accordingly to your current trigger.




I believe that even if i change my trigger to "Before Insert" event, it would still get fired as part of workflow field update.

The documentation says, before and after triggers fire one more time if there is a workflow field update.

Any idea?




Imran MohammedImran Mohammed

I assume you have the trigger that gets fired when Stage reaches Won.

In the remaining 5 Field updates i hope dont have a Field Update for StageName.

If this is true, then make your trigger a before update trigger.

In the trigger modify the if condition

 if(op.StageName=='Won' && Trigger.oldMap.get(op.Id).StageName!='Won' && Trigger.oldMap.get(op.Id).AnyOneFieldUpdatedNameUSedInWorkflow == op.ANyOneFieldUpdateNameUSedInWorkflow ) 


If you have any questions, let me know.


Hey its ok even if you use trigger on after update,

one way to avoid this is use static class

hope it help....

Imran MohammedImran Mohammed

Yes, later i thought that we can do it with using after update in the code i posted.

Let me know if that works.




aah.. i got you now!

if(op.StageName=='Won' && Trigger.oldMap.get(op.Id).StageName!='Won' &&Trigger.oldMap.get(op.Id).AnyOneFieldUpdatedNameUSedInWorkflow == op.ANyOneFieldUpdateNameUSedInWorkflow ) 


It should also work for after update as Rahul pointed out.


I need to check on the Static class thing. It worked for me now.


Thanks Guys!