CASE 1: a trigger throws an unhandled exception.
What causes the error?
The CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY
happens when an object has a trigger, and creating or updating records cause that trigger to throw an unhandled exception. An unhandled exception is where the Apex code doesn't catch an exception.
Here's a simple example of an Apex trigger that throws an unhandled exception.
trigger ThrowException on Aardvark__c (before insert, before update) {
int x = 1 / 0;
}
This code throws an exception, and because we're not catching that exception we see the error.
How do I fix the error?
If you have access to the trigger's code, we can change the Apex code to catch exceptions.
trigger ThrowException on Aardvark__c (before insert, before update) {
for (Aardvark__c aardvark:trigger.new) {
try {
Integer x = 1 / 0;
} catch (Exception e) {
aardvark.adderror(e.getMessage());
}
}
}
This code will catch the exception and attach the exception message to the record, rather than throwing the unhandled exception. This will give us a FIELD_CUSTOM_VALIDATION_EXCEPTION
on the record. If we don't care about exceptions, we could silently ignore the exception.
If you don't have access to the trigger's code (for example, if the trigger is in a managed package) then you can avoid the error by:
disabling the trigger during the deployment (note: not all managed packages allow you to disable Apex triggers)
excluding specific records causing the exception using a filter
CASE 2: filter logic & picklist trigger errors
There's a specific case of this error that can happen on objects using Salesforce's filter logic (1 AND (2 OR 3)
). Examples of these objects are:
Price Rules & Price Conditions
Product Rules & Error Conditions
Approval Rules & Approval Conditions
What causes the error?
Each rule object has related conditions and a Conditions Met
field.
The conditions on the rule must match according to the Conditions Met
field.
All
- All conditions on the rule must match.Any
- At least one conditions on the rule must match.Custom
- The conditions must match using the filter logic in theAdvanced Condition
field.1 AND (2 OR 3)
means that condition 1 must match and at least one condition 2 or 3 must also match.
When we set Conditions Met
to Custom
, an Apex trigger enforces that the conditions used in the Advanced Condition
must exist. This leads to a circular dependency. We can't create the conditions if the rule doesn't exist. We can't create the rule with Conditions Met
set to Custom
if the conditions don't exist.
To break this circular dependency, Gearset creates the rule without setting the Conditions Met
field. Gearset updates Conditions Met
later in the deployment.
If there isn't a default value for Conditions Met
then the deployment will fail.
How do I fix the error?
From Salesforce, set the default value for the field Conditions Met
to All
or Any
, on the rule object (for example Product rule).
During the deployment, Gearset will create the rule using the default value. The conditions on the rule are then created. Finally, we update the rule to use the correct value for Conditions Met
.