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
helpo12helpo12 

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;
}
}
}
Sudhir.kSudhir.k
Hi,
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
EldonEldon
Hi just check if you have any other triggers defined on Change_Request_Product__c or  Change_Request__c. If yes try disabling them and try gaing. If you cant disable any other trigger then you can add a helper class for avoidiing recursive calling fo your trigger like below
 
public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}

Then replace your trigger with below
 
trigger ReturnSends on Change_Request__c (after insert, after update) {

if(checkRecursive.runOnce())
    {

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;
}
}
}
}

If that trigger depth reching error is in any other trigger put the following  if condition in that trigger
if(checkRecursive.runOnce()) {
//code
}


Regards