2

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.

1 Answer 1

3

A few initial points: 1. You don't need to have the line "opptsToInsert.add(newOppt);" in your before trigger. The before trigger will just fill the right values in the fields, and the system will do the DML anyway. 2. When you add OppLineItem records to an Opportunity, it affects the Opportunity (Roll-up amount to the Opp.Amount field) so an Opportunity trigger could fire. Put in a recursion static boolean preventer so you don't execute the second trigger. To do that, search Google for "trigger prevent recursive firing" or something similar.

1
  • Thanks a lot for your help.I have removed "opptsToInsert.add(newOppt);"
    – Enry
    Jul 18, 2013 at 8:28

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.