+ Start a Discussion
sales4cesales4ce 

Trigger Firing Twice due to a field Update

Hi,

 

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')
      oppIds.Add(op.Id);
  }
  
  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-';
    ent.Close_Date__c=oppty[i].CloseDate;
    ent.Contract_Activation_Date__c=oppty[i].Contract_Activation_Date__c;
    ent.AccountId=oppty[i].AccountId;
    ent.Name=name+oppty[i].Name;
    //ent.Status='Active';
    ent.Opportunity__c=oppty[i].Id;
    newEntitlement.Add(ent);
    
  }
  System.debug('Entitlement Size=='+newEntitlement.size());
  Database.insert(newEntitlement);

}

 

Thanks,

Sales4ce

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.

sales4cesales4ce

Imran,

 

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?

 

Thanks,

Sales4ce

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.

rahulsharmarahulsharma

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.

sales4cesales4ce

Imran,

 

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!

 

Sales4ce