Apex Triggers

Trigger Error: Cannot_insert_update_activate_entity Salesforce

One typical error which a developer can face to is Cannot_insert_update_activate_entity Trigger error. This could be fixed checking the logs on the Developer Console but lets see what it is, how to resolve it and avoid it.

When does this occur?

If an object has a trigger, sometimes creating or updating records can cause that trigger to throw an unhandled exception.

This error message means there was a problem while trying to insert or update a Salesforce record, and as a result, the record was not saved to the database.


How to Avoid/Fix Cannot_insert_update_activate_entity Error

Here are a few steps that will help you to resolve the issue.

Debug the Logs

Debugging Apex classes is a best practice for quickly identifying and resolving issues within the code using the Developer Console that Salesforce provides us.

The trigger will generate a log on the Developer Console. If you go to it and filter by FATAL_ERROR, you can see the line in which the error is happening.

Deactivate Trigger in Salesforce

Check the trigger's code

If you have access to the trigger's code, review it to identify the cause of the error. This trigger is trying to divide by 0. Of course, that isn't possible, so that we will get an error.

trigger AccountTrigger on Account (before insert) {
    Integer num = 5/0;
    for (Account acc : Trigger.new) {
		acc.Name = 'Resolve trigger error';
Divide by 0 in Apex code Salesforce

Turn off the trigger

If you cannot access the trigger's code, for instance, if it is included in a managed package, you can prevent the error by turning off the trigger. However, it is essential to note that not all managed packages allow for disabling of Apex triggers.

To deactivate it, go to Setup > Object Manager > Object > Triggers. There, you select your trigger, click on edit and deactivate it.

Deactivate Trigger in Salesforce

Exclude specific records

If you can't turn off the trigger, you can exclude specific records causing the exception using a filter.

You can skip records using the continue keyword to move to the next record.

trigger AccountTrigger on Account (before insert) {
    for (Account acc : Trigger.new) {
        if (acc.Industry == 'Finance') continue;
        acc.Name = 'Account Test Name';

Prevent recursive calls

To avoid triggering a recursive call, ensuring that your trigger is executed only once is crucial.

To achieve this, you can add a boolean variable to your trigger and use it to check if it has already executed. You can execute the trigger logic if it is false and set the variable to true at the end.

This will prevent the trigger from executing multiple times and causing a recursive call.

trigger AccountTrigger on Account (before insert) {
    private static Boolean firstRun = true;
    if (firstRun) {
        for (Account acc : Trigger.new) {
            // TRIGGER CODE

        firstRun = false;