I have a trigger on Oppurtunity and this trigger run on after update. If Delivery/Installation Status is updated create a new record in customer object. In the same time there is a workflow in which Delivery/Installation Status is update to 'progress' then update field Tracking Number by adding 1 to it. My issue is when Delivery/Installation Status is update to 'progress' 2 customer record are getting created because the same time trigger and workflow get active. There is a way by using class with a static boolean variable we can control it. But I want to know is there any other way through my code itself I am manage to overcome this issue. My code :

trigger CreateCustomer on Opportunity (after Update) {
oppTriggerhandler opptrighandler = new oppTriggerhandler ();

  if(Trigger.isUpdate && Trigger.isAfter){
    opptrighandler.afterUpdate(Trigger.oldMap, Trigger.NewMap);

trigger handler :

public class oppTriggerhandler {

List<Customer__c> customerList = new List<Customer__c> ();
public void afterUpdate(Map<id, Opportunity> oppOld, Map<id, Opportunity> oppNew){    

    for(Id opp : oppOld.keyset()){
        if(oppOld.get(opp).DeliveryInstallationStatus__c != oppNew.get(opp).DeliveryInstallationStatus__c){ 

              Customer__c newRec = new Customer__c();
              newRec.Opportunity__c = opp;
              newRec.Name_Opp__c = oppNew.get(opp).Name; 

            customerList.add(new Customer__c(Opportunity__c = opp, Name_Opp__c = oppNew.get(opp).Name));
    insert customerList;


Please guide me the best way I can find the solution on this issue.


The best practice to avoid trigger running twice, as you probably already know, is to use a static variable so to be able to store how many time trigger ran and stop it before the second execution.

Also, there cannot be static class in Apex.

So, you could use a boolean static variable, (e.g.naming it triggerAlreadyRun) initialized to false value, and set it to true at the start of your handler method. Then, in your trigger, make the call to the your handler method conditional, on the boolean value basis.

In this way, your handler method will execute only if it's the first time the trigger is fired.

  • 1
    You mean Set so you can call contains.
    – Adrian Larson
    Jan 31 '15 at 16:38
  • 1
    Based on your logic, you could use a Set instead of the Boolean var, storing in the list the Ids of processed record to avoid they are re-processed. So you should check, instead of the boolean var value, if the 'currently being processed' - record Id is present in that list.
    – user9959
    Jan 31 '15 at 16:44

Based on your description, it does not seem like you should run into trigger recursion, and therefore I disagree with the solution that a static flag is the answer, although there are many cases where it would be a fine choice.

I find that I like to separate my concerns in my handler into two types: filter and action. I usually put them in entirely separate methods. Here, you are having trouble with your filter. You may want to check that it changes to a specific value.

public static final String PROGRESS = 'progress';

public static List<Opportunity> isNewlyInProgress(
    List<Opportunity> newRecords, Map<Id, Opportunity> oldMap
) {
    List<Opportunity> matches = new List<Opportunity>();
    for (Opportunity newRecord : newRecords)
        Opportunity oldRecord = oldMap.get(newRecord.Id);
        if (oldRecord != null &&
            newRecord.DeliveryInstallationStatus__c != oldRecord.DeliveryInstallationStatus__c &&
            newRecord.DeliveryInstallationStatus__c == PROGRESS)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.