+ Start a Discussion
ColealarsColealars 

Why does my before update trigger fire when creating a new record

When I create an Opp the before update trigger below is firing.  I have a lot of field update workflows, its that what's causing the trigger to fire on the creation of the Opp.  If so, how do I prevent this trigger from firing?

 

//This trigger display error if no Account Executive is specified on Contact Role
trigger opportunity_ae_required on Opportunity (before update) {

    Map<String, Opportunity> oppy_contact = new Map<String, Opportunity>();

        if (Trigger.isUpdate && Trigger.new[i].ae_required__c == 1)
                {
                oppy_contact.put(Trigger.new[i].id,Trigger.new[i]);
                //system.debug('This is the trigger.new.id - '+Trigger.new[i].id);
                system.debug('Trigger.new[i]: ' + Trigger.isUpdate);
        }
    }

    map<Id, OpportunityContactRole> oppycontactroles = new map<Id, OpportunityContactRole>();

    for (OpportunityContactRole ocr : [select OpportunityId, Role from OpportunityContactRole
                                       where (OpportunityContactRole.Role = 'Account Executive'
                                       and OpportunityContactRole.OpportunityId in :oppy_contact.keySet())]) {

        oppycontactroles.put(ocr.OpportunityId,ocr);
    }

    for (Opportunity oppy : system.trigger.new) {
        //system.debug('List oppy Id - '+oppy.id);
        system.debug('trigger.isUpdate: ' + Trigger.isUpdate);
        if (Trigger.isUpdate) {
            if  (oppycontactroles.containsKey(oppy.id) || oppy.ae_required__c == 0 || 1==1)
                {
                // Do nothing
                } 
            else
                {
                    //oppy.addError(Trigger.isInsert +' Cannot make changes to this Opportunity until an Account Executive has been Selected in the Contact Roles section.');          
                    oppy.addError('U:'+Trigger.isUpdate + ' I:'+Trigger.isInsert+' AE:'+oppycontactroles.containsKey(oppy.id)+' AEreq:' + oppy.ae_required__c);
                 }
         }
    } //for 
 }

Best Answer chosen by Admin (Salesforce Developers) 
ColealarsColealars

Creating the new recursiveTrigger class and beforeOppInsert trigger allow me to create a new Opp and save it when a workflow field update fired. 

 

However, now after I click save and then try to add a product, my beforeUpdate trigger fires again and tells me I can't add the product without add a contact role.  This sounds like its related to the new save behavior where the update on the product forces the triggers to fire on the opp.  So I created a beforeProdInsert trigger just like the beforeOppInsert trigger and that solves the problem.

 

Thanks for the help.

All Answers

ColinKenworthy1ColinKenworthy1

Check setup > AppSetup > Deploy > CriticalUpdates. Possibly something in that.

Also in the apex language reference Book lookup Workflow in the Index.

sfdcfoxsfdcfox

Field updates cause recursive trigger firing; this can cause all sorts of unwanted problems. There are several solutions to this problem. Probably the easiest would be a quick boolean check to see if the trigger should ignore it's usual logic. It can work like this:

 

 

public class recursiveTrigger { public static boolean isRecursive = false; } trigger beforeOppInsert on Opportunity (before insert) { recursiveTrigger.isRecursive = true; } trigger beforeOppUpdate on Opportunity (before update) { if(recursiveTrigger.isRecursive) { return; } // Other logic goes here. }

 

This should probably cover what you're trying to do. Basically, a static variable exists until the entire trigger chain has executed, so you can use this method to prevent an initial update from triggering; if the opportunity is not an original insert, isRecursive will be false and therefore will execute the rest of the logic.

 

ColealarsColealars

I'm not sure what I do here.  I got an error when I created a new class as follows:

 

public class recursiveTrigger {
  public static boolean isRecursive = false;
}

trigger beforeOppInsert on Opportunity (before insert) {
  recursiveTrigger.isRecursive = true;
}

trigger opportunity_ae_required on Opportunity (before update) {
  if(recursiveTrigger.isRecursive) {
    return;
  }
... my trigger code

}

 

And I couldn't put this code in the trigger, so how exactly do I code this?

sfdcfoxsfdcfox
Those are 3 separate files. Make the class a class (Setup > App Setup > Develop > Apex Classes), then make the two triggers, each one separate.
ColealarsColealars

Creating the new recursiveTrigger class and beforeOppInsert trigger allow me to create a new Opp and save it when a workflow field update fired. 

 

However, now after I click save and then try to add a product, my beforeUpdate trigger fires again and tells me I can't add the product without add a contact role.  This sounds like its related to the new save behavior where the update on the product forces the triggers to fire on the opp.  So I created a beforeProdInsert trigger just like the beforeOppInsert trigger and that solves the problem.

 

Thanks for the help.

This was selected as the best answer