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
JN22JN22 

Trigger Error

Hello,

I've created the trigger below on my OpportunityLineItem object.  There is a custom field on the Opportunity object called Max_Deliv_Hx__c which captures the highest number of Opportunity Products that have been included on an Opportunity.  I am trying to assign a value to each OpportunityLineItem from 1 to Max_Deliv_Hx__c, but only if that OpportunityLineItem does not already have a value.  When I run this trigger I get and error:

Error: Invalid Data.
Apex trigger UniqueDelivID caused an unexpected exception, contact your administrator: UniqueDelivID: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 00kn00000032bk8AAA; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 00kn00000032bk8) is currently in trigger UniqueDelivID, therefore it cannot recursively update itself: []: Trigger.UniqueDelivID: line 18, column 1


Can anyone provide some help?  Thanks.


trigger UniqueDelivID on OpportunityLineItem (before insert, before update) {

    //get the Product Numbers and other fields from the OpportunityLineItem and build a list of OpportunityLineItems to update

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

        for(OpportunityLineItem oli : [SELECT Id, Opportunity.Max_Deliv_Hx__c, Max_Deliv__c
                                       FROM OpportunityLineItem
                                       WHERE Id in : trigger.new]) {

        IF(oli.Max_Deliv__c = null){
            oli.Max_Deliv__c = oli.Opportunity.Max_Deliv_Hx__c + 1; //Update the OpportunityLineItem
        }
        oliList.add(oli);

    }

    update oliList; //Update the OpportunityLineItems with the Max_Deliv__c field

}
Best Answer chosen by JN22
himanshu323himanshu323
Hi JN22:

The following trigger I have  written based on your scenatio,Modify your parent and child objects field accordingly and this will solve your problem  :

Use Map and List as I have used :

trigger UpdateContact on Contact (before insert , before update) {
  
   Map<Id, Account> conMap = new  Map<Id, Account>();
   List<Id> accList = new List<Id>();
   for(Contact con1 : trigger.New){
       accList.add(con1.AccountId);
   }
  
   for(Account acc : [Select Id,Max_Delivery__c  from Account where Id In : accList]){
       conMap.put(acc.Id,acc);
   }
  
   if(trigger.Isbefore && (trigger.isInsert || trigger.isupdate)){
  for(Contact con : trigger.New){
     if(conMap.get(con.AccountId).Max_Delivery__c != null){
      if(con.max_Delivery__c == null){
    con.max_Delivery__c = conMap.get(con.AccountId).Max_Delivery__c+1;
      }
     }
   
  }
}
}


Please accept my this comment as answer , if it helps you.

Thanks

All Answers

kk

Hi,
 

Could you try with after insert and after update events and see

JN22JN22
When I change to an after trigger I get this error:

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger UniqueDelivID caused an unexpected exception, contact your administrator: UniqueDelivID: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 00kn00000032bk8AAA; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UniqueDelivID: maximum trigger depth exceeded OpportunityLineItem trigger event AfterUpdate for [00kn00000032bk8] OpportunityLineItem trigger event AfterUpdate for [00kn00000032bk8] OpportunityLineItem trigger event AfterUpdate for [00kn00000032bk8] OpportunityLineItem trigger event AfterUpdate for [00kn00000032bk8] OpportunityLineItem trigger event AfterUpdate for [00kn00000032bk8] OpportunityLineItem trigger event AfterUpdate for [00kn00000032bk8] OpportunityLineItem trigger event AfterUpdate for [00kn00000032bk8] OpportunityLineItem trigger event AfterUpdate for [00kn00000032bk8] OpportunityLineItem trigger event AfterUpdate for [00kn00000032bk8] OpportunityLineItem trigger event AfterUpdate for [00kn00000032bk8] OpportunityLineItem trigger event AfterUpdate for [00kn00000032bk8] OpportunityLineItem trigger event AfterUpdate for [00kn00000032bk8] OpportunityLineItem trigger event AfterUpdate for [00kn00000032bk8] OpportunityLineItem trigger event AfterUpdate for [00kn00000032bk8] OpportunityLineItem trigger event AfterUpdate for [00kn00000032bk8] OpportunityLineItem trigger event AfterUpdate for [00kn00000032bk8]: []: Trigger.UniqueDelivID: line 18, column 1
himanshu323himanshu323
hi @jn22 Please try this:


trigger UniqueDelivID on OpportunityLineItem (before insert, before update) {

    //get the Product Numbers and other fields from the OpportunityLineItem and build a list of OpportunityLineItems to update
if(trigger.isbefore && (trigger.isInsert || trigger.isupdate)){
 
  List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();

   for(OpportunityLineItem oli : [SELECT Id, Opportunity.Max_Deliv_Hx__c, Max_Deliv__c
             FROM OpportunityLineItem
             WHERE Id in : trigger.new]) {

   IF(oli.Max_Deliv__c = null){
    oli.Max_Deliv__c = oli.Opportunity.Max_Deliv_Hx__c + 1; //Update the OpportunityLineItem
   }
  }
}
}
himanshu323himanshu323
Hi Jn22 are you still facing this issue?
JN22JN22
Your changes removed the error, however, nothing is updating when the trigger fires.  Any idea why?
himanshu323himanshu323
Hi JN22:

The following trigger I have  written based on your scenatio,Modify your parent and child objects field accordingly and this will solve your problem  :

Use Map and List as I have used :

trigger UpdateContact on Contact (before insert , before update) {
  
   Map<Id, Account> conMap = new  Map<Id, Account>();
   List<Id> accList = new List<Id>();
   for(Contact con1 : trigger.New){
       accList.add(con1.AccountId);
   }
  
   for(Account acc : [Select Id,Max_Delivery__c  from Account where Id In : accList]){
       conMap.put(acc.Id,acc);
   }
  
   if(trigger.Isbefore && (trigger.isInsert || trigger.isupdate)){
  for(Contact con : trigger.New){
     if(conMap.get(con.AccountId).Max_Delivery__c != null){
      if(con.max_Delivery__c == null){
    con.max_Delivery__c = conMap.get(con.AccountId).Max_Delivery__c+1;
      }
     }
   
  }
}
}


Please accept my this comment as answer , if it helps you.

Thanks
This was selected as the best answer
JN22JN22
Thanks!  I used that structure and came up with the trigger below that now works!
trigger UniqueDelivID on OpportunityLineItem (before insert, before update) {   

    Set<ID> setOliIds = new Set<ID>();

    for(OpportunityLineItem oli:Trigger.new){
        setOliIds.add(oli.Id);
    }

    Map<ID, OpportunityLineItem> mapOli = new Map<ID, OpportunityLineItem>([SELECT Id, Opportunity.Max_Deliv_Hx__c, Max_Deliv__c
                                                            FROM OpportunityLineItem
                                                            WHERE Id in:setOliIds]);
    if(mapOli.size()>0){
        for(OpportunityLineItem oli:Trigger.New){
            IF(mapOli.containsKey(oli.Id) && oli.Max_Deliv__c == null){
                oli.Max_Deliv__c = mapOli.get(oli.Id).Opportunity.Max_Deliv_Hx__c + 1;
            }
        }
    }
}