You need to sign in to do that
Don't have an account?
JN22
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
}
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
}
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
Hi,
Could you try with after insert and after update events and see
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
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
}
}
}
}
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