+ Start a Discussion
BobBob 

Stopping an trigger update after it has fired?

How would I stop this trigger below from firing once it has already done an update. I tried the trigger old.map but that didn't work.

 
trigger TriggerUpdateUnitExtWarranty on Unit__c (before insert, before update) {
  
  // Step 1: Create a set of all extended warranties of Units to query
  Set<String> extwrnty = new Set<String>();
  
  
  for (Unit__c newUnit : Trigger.new) {
   
      extwrnty.add(newUnit.uordernum_prodcode__c);    
  }

  // Step 2: Query for all the Units in Step 1
  List<Extended_Warranty__c> potentialproduct = [SELECT Id, Name,exordernum_prodcode__c FROM Extended_Warranty__c
                                 WHERE exordernum_prodcode__c IN :extwrnty];

  // Step 3: Make a Map that lets you search for units by extended warranty
  Map<String, Extended_Warranty__c> ewrntyMap = new Map<String, Extended_Warranty__c>();
  for (Extended_Warranty__c w : potentialproduct) {
    ewrntyMap.put(w.exordernum_prodcode__c, w);
  }

  // Step 4: Get the matching extended warranty in the Map by extended warranty!
  for (Unit__c newUnit : Trigger.new) {
    
    if (ewrntyMap.containsKey(newUnit.uordernum_prodcode__c)) {
      Extended_Warranty__c ordnm = ewrntyMap.get(newUnit.uordernum_prodcode__c);

  //ordnm is the name of the extended warranty record that is associated with the unit.      
    if (ordnm != null) {
      newUnit.Ext_Warranty__c  = ordnm.Id;
      }
     if (Trigger.oldMap == null) {
      newUnit.Ext_Warranty__c  = null;
      }
    }
  }

}
Ravi Dutt SharmaRavi Dutt Sharma
Use a static variable to control the execution of trigger.
 
public class TriggerExecutionController {
     
     public static boolean hasExecutedOnce = false;

}

In your trigger, check the value of this variable.
 
trigger TriggerUpdateUnitExtWarranty on Unit__c (before insert, before update) {

     if(TriggerExecutionController.hasExecutedOnce == false){
          // your code here
          TriggerExecutionController.hasExecutedOnce = true;
     }
}


 
venkat-Dvenkat-D
Refer below help from salesforce
https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US
Amit Chaudhary 8Amit Chaudhary 8
Hi Bob,
please check below post to resolve this issue
1) http://amitsalesforce.blogspot.in/2015/03/how-to-stop-recursive-trigger-in.html

you can create a class with a static Boolean variable with default value true. In the trigger, before executing your code keep a check that the variable is true or not. Once you check make the variable false.

NOTE:- your this trigger look good to me as you are using before insert and before update and your are not perfoming any DML in trigger. Please check other trigger on same object if your trigger is recursive