You need to sign in to do that
Don't have an account?
Enry
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.
If yes, update trigger will get fired due to update event called by first trigger.
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
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