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
EnryEnry 

control trigger execution

I have 2 triggers:

The first:

trigger AutoProduct on Opportunity (before insert,after insert) {

    for(Opportunity newOppt : Trigger.new){

           if  ((trigger.isBefore)&&(Trigger.isInsert)){

               // SET SOME FIELDS
                }

              opptsToInsert.add(newOppt); 



            if ((Trigger.isAfter)&&(Trigger.isInsert)){
               //GET OPPORTUNITY
                //CREATE OPPORTUNITY LINE ITEM FOR EACH NEW OPPORTUNITY
                //CREATE OPPORTUNITY LINE ITEM QUANTITY AND REVENUE SCHEDULE FOR EACH NEW OPPORTUNITY PRODUCT
                          }


                   }  

 The second

trigger AutoProductupdate on Opportunity ( before update,after update) {



    for(Opportunity newOppt : Trigger.new){

           if  (trigger.isAfter){


                                 //DELETE OPPORTUNITY LINE ITEM SCHEDULES FOR ALL THE PRODUCTS
                                 // UPDATE OPPORTUNITY LINE ITEM

                                 // CREATE OPPORTUNITY LINE ITEM SCHEDULES FOR ALL THE PRODUCTS
                     } 

             }      

}

 

The problem is that when i insert an opportunity(execution first trigger) it's also executed the second trigger on update.

I want that on creation of an opportunity is only executed the first trigger.

How can avoid this?

Thank in advantage for any advice.

BR.

_Prasu__Prasu_
Are you again updating the opportunity in first trigger? As I see "after insert" event also used in first trigger.
If yes, update trigger will get fired due to update event called by first trigger.
souvik9086souvik9086

Create a controller

public class FutureTriggerController{

public static boolean isFutureUpdate = false;

}

 

Then modify both the triggers like this

First Trigger

 

trigger AutoProduct on Opportunity (before insert,after insert) {
if(FutureTriggerController.isFutureUpdate != true){
FutureTriggerController.isFutureUpdate =true;
for(Opportunity newOppt : Trigger.new){

if ((trigger.isBefore)&&(Trigger.isInsert)){

// SET SOME FIELDS
}

opptsToInsert.add(newOppt);

 

if ((Trigger.isAfter)&&(Trigger.isInsert)){
//GET OPPORTUNITY
//CREATE OPPORTUNITY LINE ITEM FOR EACH NEW OPPORTUNITY
//CREATE OPPORTUNITY LINE ITEM QUANTITY AND REVENUE SCHEDULE FOR EACH NEW OPPORTUNITY PRODUCT
}

}
}

 

Second Trigger

 

trigger AutoProductupdate on Opportunity ( before update,after update) {


if(FutureTriggerController.isFutureUpdate != true){
FutureTriggerController.isFutureUpdate =true;
for(Opportunity newOppt : Trigger.new){

if (trigger.isAfter){


//DELETE OPPORTUNITY LINE ITEM SCHEDULES FOR ALL THE PRODUCTS
// UPDATE OPPORTUNITY LINE ITEM

// CREATE OPPORTUNITY LINE ITEM SCHEDULES FOR ALL THE PRODUCTS
}

}
}

}

 

THIS WILL PREVENT CALLING TWO TRIGGERS AT THE SAME INSTANCE.

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

Avidev9Avidev9

This is why saesforce recommends to use a Single Trigger per Object !

You can modify the structure a lil bit and combine them.

 

But the best solution will be making your second trigger more selective!. Means check for particular value if they change and ignore the value that changed due to first trigger

 

trigger AutoProductupdate on Opportunity ( before update,after update) {



    for(Opportunity newOppt : Trigger.new){
           //here you are making it more selecting and making it fire for very selective set of fields, Say you want to execute this piece of code only when Field__c changes(which is not changed by trigger 1) 
           if  (trigger.isAfter && newOppt.Field__c != trigger.oldMap.get(newOppt).Field__c){

                                 //DELETE OPPORTUNITY LINE ITEM SCHEDULES FOR ALL THE PRODUCTS
                                 // UPDATE OPPORTUNITY LINE ITEM

                                 // CREATE OPPORTUNITY LINE ITEM SCHEDULES FOR ALL THE PRODUCTS
                     } 

             }      

}