0

I need to update a custom field (ProductType__c) on opportunity on insert and update. the value of this field should be picked from OpportunityLineItem (productfamily__c)

the way our relationships were built means that one opportunity can have more than one opportunityLineItem. if it is the case, ProductType__c should be set to "multi". otherwise it simply copies the value.

I have having difficulties as the trigger drops an error "maximum trigger depth exceeded Opportunity trigger event AfterUpdate for [006c000000CNFdg".

I haven't been working on salesforce for few months and I can't see where the problem is.

Trigger UpdateProductType on Opportunity (after insert, after update) {

 List<Opportunity> OpportunitiesToUpdate = new List<Opportunity>();

 List <OpportunityLineItem> OpportunityLineItemsProduct= new List <OpportunityLineItem>();

      for(Opportunity OP : [select id, ProductType__c from Opportunity where id IN  :trigger.newMap.keySet()]) {

        System.debug(Op.id);

        OpportunityLineItemsProduct = Database.query('select Product_Family__c from OpportunityLineItem where opportunityid = \''+OP.id+'\'');

   for(integer i=0;i<OpportunityLineItemsProduct.size();i++){

             System.debug(OpportunityLineItemsProduct);


             //Here i need to add more logic so if the OpportunityLineItemsProduct.size>1 then the field producttype is set to "multi"

             If (OpportunityLineItemsProduct.size()>1){
             OP.ProductType__c = 'multi';
             }
             else
             {
             OP.ProductType__c = OpportunityLineItemsProduct[i].Product_Family__c;
             }
              System.debug(OP.ProductType__c);

              OpportunitiesToUpdate.add(OP);       
       }   

       if(!OpportunitiesToUpdate.isEmpty())
                update OpportunitiesToUpdate;
      }
}
  • 2
    This is because of you are updating the trigger context opportunity inside the trigger. Try to maintain a static boolean to make the trigger to be fired only once if you really need to perform the opportunity update. Also try to bulkify your code – highfive Nov 5 '14 at 8:14
  • and I'd consider making this a trigger on OpportunityLineItem instead. So if any change is done there (insert, update, delete), update the relevant field on Opportunity. Otherwise any reports etc you run between the OpportunityLineItem update and the Opportunity update will be showing an incorrent Opportunity ProductType__c – Guy Clairbois Nov 5 '14 at 10:14
0

Try:

 Trigger UpdateProductType on Opportunity (before insert, before update) {Map<ID,Opportunity> setOpId = new Map<ID,Opportunity>();
     for(Opportunity OP : Trigger.new)
     {
         setOpId.put(Op.Id,op);
     }
      for(Opportunity OP : [select id, ProductType__c,(select Product_Family__c from OpportunityLineItems) from Opportunity where id IN  :setOpId.keySet()]) {
             If (op.OpportunityLineItems.size()>1){
             setOpId.get(OP.Id).ProductType__c = 'multi';
             }
             else
             {
             setOpId.get(OP.Id).ProductType__c = op.OpportunityLineItems[0].Product_Family__c ;
             }

       }   

}

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.