Apex: cannot_insert_update_activate_entity - What must I do in this scenario?

4.6K    Asked by AdityaYadav in Salesforce , Asked on Sep 23, 2022

 I added a new field on the Opportunity table to keep a count of records, as well as track the updates made on that record. This is needed for an external system we use. Opportunity table already has around 5000 records now. I would like to update the new field that I added with some sequence order initially. Say each record to have a unique sequence number like 1,2,3 etc(can't use the Id field for unique). The field "Identifier__c" was added for this purpose with data type number and unique constraint. For that I created a temporary class and trigger. But it is throwing an error.


Review all error messages below to correct your data. Apex trigger OpportunitySAPIdentifierHandler caused an unexpected exception, contact your administrator: OpportunitySAPIdentifierHandler: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 006E00000038FPSIA2; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, OpportunitySAPIdentifierHandler: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 006E00000038FPTIA2; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, OpportunitySAPIdentifierHandler: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 006E00000038FPSIA2; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 006E00000038FPS) is currently in trigger OpportunitySAPIdentifierHandler, therefore it cannot recursively update itself: [] Class.AutoNumber.OpportunityAutoNumber: line 76, column 1 Trigger.OpportunitySAPIdentifierHandler: line 4, column 1: [] Class.AutoNumber.OpportunityAutoNumber: line 76, column 1 Trigger.OpportunitySAPIdentifierHandler: line 4, column 1: []: Class.AutoNumber.OpportunityAutoNumber: line 76, column 1


Below is the trigger and class used.


trigger Opportuniwith sIdentifierHandler on Opportunity (before insert,before update) {
        If(Trigger.isInsert || Trigger.isUpdate){
            AutoNumber.OpportunityAutoNumber(Trigger.new);
        } 
}
The class is
public with sharing class AutoNumber {


    public static void OpportunityAutoNumber (List Opp) {


        if(Trigger.isUpdate || Trigger.isInsert) {


            for(Opportunity p: Opp) {


                List oppList = new List();

                oppList = [SELECT Id, createdDate, Identifier__c FROM Opportunity where Identifier__c = null and Id != :p.Id limit 100 FOR update];

                for (Opportunity oppL: oppList) {


                    if(oppL.Identifier__c == null) {


                        List OpportunityList = new List([select Identifier__c from Opportunity where Identifier__c != null order by Identifier__c desc limit 1]); 

                        if (!OpportunityList.isEmpty()) {  

                            Decimal maxvalue = OpportunityList.get(0).Identifier__c;

                            oppL.Identifier__c = maxval + 1;

                            update oppL;

                        }


                    }

                    // integer currentCounter = Integer.valueOf(opp.Identifier__c);

                    // oppr.Identifier__c = currentCounter + 1;

                    // update oppr;

                }

            }

        }   

    }


}

Answered by Al German

To prevent a recursive call - cannot_insert_update_activate_entity, you should make sure your trigger only executes one time. Add a class with a static boolean variable. In the trigger, have a condition that checks the value of the boolean. Once the trigger executes, change the value to false.


trigger Opportuniwith sIdentifierHandler on Opportunity (before insert,before update) {

    if(checkRecursion.runOnce()){

       If(Trigger.isInsert || Trigger.isUpdate){
                AutoNumber.OpportunityAutoNumber(Trigger.new);
       }
    }
    }
****Utility class****
public Class checkRecursion{
        private static boolean run = true;
        public static boolean runOnce(){
          if(run){
           run=false;
           return true;
          }else{
            return run;
          }
        }
    }


Your Answer

Answer (1)

The "Cannot_insert_update_activate_entity" error in Apex typically occurs when you're trying to perform an operation (insert, update, or activate) on a record or entity, but the operation is not allowed due to various reasons, such as validation rules, triggers, or data integrity constraints.


To resolve this error, here are some steps you can take:

Review Error Message: Carefully review the error message to understand the specific entity or record that is causing the issue and the context in which the error is occurring. This can provide valuable clues about what might be causing the problem.

Check Validation Rules: If you have validation rules defined for the object or entity you're trying to insert, update, or activate, review them to ensure they are not blocking the operation. Make any necessary adjustments to the validation rules to allow the operation to proceed if appropriate.

Inspect Triggers: If there are triggers defined on the object or entity, review them to see if they are causing the error. Triggers can contain logic that prevents certain operations from being performed based on specific conditions. Modify the trigger logic if necessary to allow the operation to proceed.

Consider Workflow Rules and Processes: Workflow rules, process builder processes, or flows can also affect record operations. Check if any of these are triggering actions that might interfere with the operation you're trying to perform. Adjust the workflow rules or processes as needed.

Data Integrity Constraints: Ensure that there are no data integrity constraints (e.g., parent-child relationship constraints) that are preventing the operation from completing successfully. Correct any data inconsistencies or constraints that are blocking the operation.

Permissions: Verify that the user performing the operation has the necessary permissions to insert, update, or activate the entity. Check both object-level and field-level security settings to ensure that the user has the appropriate access.

Debugging: Use debugging techniques such as system debug logs, checkpoints, or debugging statements to trace the execution of your code and identify where the error is occurring. This can help pinpoint the root cause of the issue.

Consult Documentation: Consult the Salesforce documentation, release notes, or community forums for any known issues or considerations related to the entity or operation you're trying to perform.

By carefully reviewing and addressing the potential causes outlined above, you should be able to resolve the "Cannot_insert_update_activate_entity" error in your Apex code.


6 Months

Interviews

Parent Categories