You need to sign in to do that
Don't have an account?
helpo12
Use trigger to Insert Custom Object but get an Error CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY
Hi All,
I'm attmepting to create a trigger on a custom object, which is a child of the opportunity. The triggers intention, is to check the oppotunity's Opportunity Line Items, and see if the product exists then create a custom object that is a child of the custom object the trigger is being initiated at.
I get the follwoing error, Error:Apex trigger ReturnSends caused an unexpected exception, contact your administrator: ReturnSends: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ReturnSends: maximum trigger depth exceeded... I'm trying to figure out how to avoid this error, I know why I'm getting just can't figure out how to correct so it doesn't happen again.
I've put my code below for any help. Thanks
trigger ReturnSends on Change_Request__c (after insert, after update) {
List<Change_Request_Product__c> CRUpdates = new List<Change_Request_Product__c>();
Map<ID, Change_Request__c> oppList = new Map<ID, Change_Request__c>();
List<ID> CRList = new List<ID>();
Map<ID, Change_Request__c> coppList = new Map<ID, Change_Request__c>();
for (Change_Request__c cr : Trigger.new){
if (cr.CR_State__c == 'Created'){
if (cr.OAP_Send_POE__c != null){
//oppList.put(cr.id,cr);
coppList.put(cr.id,cr);
//CRList.add(cr.Opportunity_Name__c);
}
}
}
if (!coppList.isEmpty()){
for (Change_Request__c cru : [select Id, P_Send__c, P_Send__c, Opportunity_Name__c from Change_Request__c where id in :coppList.keyset()]){
Change_Request__c OppObj = coppList.get(cru.Id);
if(OppObj != null){
for ( OpportunityLineItem oli : [select Id, Product2.Id, Product2.Name, Quantity, UnitPrice, OpportunityID, Opportunity.CurrencyIsoCode from OpportunityLineItem where OpportunityId = :cru.Opportunity_Name__c]){
if (oli.Product2.Name == 'P30' && cru.P_Send__c != null){
Change_Request_Product__c crp = new Change_Request_Product__c(Change_Request__c=cru.id,Product__c=oli.Product2.Id, Quantity__c = cru.P_Send__c, Sales_Price__c = oli.UnitPrice, AP_Price__c=decimal.valueof('540'), CurrencyIsoCode = oli.Opportunity.CurrencyIsoCode);
CRUpdates.add(crp);
}
}
}
}
if (!CRUpdates.isEmpty()){
insert CRUpdates;
}
}
}
I'm attmepting to create a trigger on a custom object, which is a child of the opportunity. The triggers intention, is to check the oppotunity's Opportunity Line Items, and see if the product exists then create a custom object that is a child of the custom object the trigger is being initiated at.
I get the follwoing error, Error:Apex trigger ReturnSends caused an unexpected exception, contact your administrator: ReturnSends: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ReturnSends: maximum trigger depth exceeded... I'm trying to figure out how to avoid this error, I know why I'm getting just can't figure out how to correct so it doesn't happen again.
I've put my code below for any help. Thanks
trigger ReturnSends on Change_Request__c (after insert, after update) {
List<Change_Request_Product__c> CRUpdates = new List<Change_Request_Product__c>();
Map<ID, Change_Request__c> oppList = new Map<ID, Change_Request__c>();
List<ID> CRList = new List<ID>();
Map<ID, Change_Request__c> coppList = new Map<ID, Change_Request__c>();
for (Change_Request__c cr : Trigger.new){
if (cr.CR_State__c == 'Created'){
if (cr.OAP_Send_POE__c != null){
//oppList.put(cr.id,cr);
coppList.put(cr.id,cr);
//CRList.add(cr.Opportunity_Name__c);
}
}
}
if (!coppList.isEmpty()){
for (Change_Request__c cru : [select Id, P_Send__c, P_Send__c, Opportunity_Name__c from Change_Request__c where id in :coppList.keyset()]){
Change_Request__c OppObj = coppList.get(cru.Id);
if(OppObj != null){
for ( OpportunityLineItem oli : [select Id, Product2.Id, Product2.Name, Quantity, UnitPrice, OpportunityID, Opportunity.CurrencyIsoCode from OpportunityLineItem where OpportunityId = :cru.Opportunity_Name__c]){
if (oli.Product2.Name == 'P30' && cru.P_Send__c != null){
Change_Request_Product__c crp = new Change_Request_Product__c(Change_Request__c=cru.id,Product__c=oli.Product2.Id, Quantity__c = cru.P_Send__c, Sales_Price__c = oli.UnitPrice, AP_Price__c=decimal.valueof('540'), CurrencyIsoCode = oli.Opportunity.CurrencyIsoCode);
CRUpdates.add(crp);
}
}
}
}
if (!CRUpdates.isEmpty()){
insert CRUpdates;
}
}
}
It seems trigger called recursively. Please refer the below link
https://help.salesforce.com/articleView?id=000133752&language=en_US&r=https:%2F%2Fwww.google.com%2F&type=1
Regards,
Sudhir
Then replace your trigger with below
If that trigger depth reching error is in any other trigger put the following if condition in that trigger
if(checkRecursive.runOnce()) {
//code
}
Regards