Salto for
Salesforce
Articles
SHARE
Pablo Gonzalez
March 15, 2022
4
min read
If you've been working with Apex or have done data uploads in Salesforce, chances are you've seen the FIELD_CUSTOM_VALIDATION_EXCEPTION error a few times.
In this article, I will explain what this error means and how we can get rid of it.
FIELD_CUSTOM_VALIDATION_EXCEPTION
Before going into details about when exactly this error occurs, let's spend a few minutes understanding the actual error name.
Generally speaking, we see this error when inserting or updating a record, and the operation fails to meet the rule of a validation rule.
However, the error says "custom validation exception." What is a custom validation? Are there standard validations?
It turns out there are two types of validations in Salesforce.
Custom validation rules
Custom validation rules are the ones that you create through the setup menu, such as this one, which prevents the account name from being the string "invalid":
System validation rules
The Salesforce order of execution tells out there are system validations. This is seen in step two:
And then again in step five:
So system validation rules are defined by Salesforce and by the attributes of the metadata in question, like how many characters a field can hold, layout-specific rules, etc.
Now we know this error occurs when a record is inserted or updated, and it fails to meet the condition of a custom validation rule, not a system one.
Let's see two examples based on the validation rule I showed earlier:
Inserting/Updating data
I'm going to use the Salesforce Inspector Chrome extension to update an existing account's name to the string "invalid":
As expected, I see the FIELD_CUSTOM_VALIDATION_EXCEPTION error:
Apex DML
Let's do the same update but using Apex in the developer console:
Now we get the same error message but wrapped in a DmlException
addError() in Apex Triggers
You can use the addError() method to add a custom error message to an sObject instance, for example:
This code takes the first account in the trigger and adds an error to it. In the real world, you would only do this once you confirm that the record is in an invalid state, and thus, you need to throw an error.
In any case, with this in place, we will get an error whenever we try to insert an account:
Notice that this is the same error we would for custom validation rules. So in this context, Apex "doesn't know" whether the error is from the addError() method or a custom validation rule.
How you solve this problem depends on many factors. For example, perhaps the validation rule is correct, and all you need to do is fix the data you are importing to satisfy the validation rule condition.
Or maybe it is an old validation rule that should've been deactivated long ago, in which case you can simply deactivate it and continue your data loading.
Or perhaps, the validation rule is valid, but you want to temporarily bypass it (though that's not really recommended). In that case, you have two options:
The second option is probably the best one. This is known as a "bypass mechanism." Here's an example of what a validation rule looks like with a custom metadata type that can decide whether the rule should run or not:
So that’s it! Hopefully, this helped you get rid of this error.
Until the next post.
Salto for
Salesforce
Salesforce
SHARE
Pablo Gonzalez
March 15, 2022
4
min read
If you've been working with Apex or have done data uploads in Salesforce, chances are you've seen the FIELD_CUSTOM_VALIDATION_EXCEPTION error a few times.
In this article, I will explain what this error means and how we can get rid of it.
FIELD_CUSTOM_VALIDATION_EXCEPTION
Before going into details about when exactly this error occurs, let's spend a few minutes understanding the actual error name.
Generally speaking, we see this error when inserting or updating a record, and the operation fails to meet the rule of a validation rule.
However, the error says "custom validation exception." What is a custom validation? Are there standard validations?
It turns out there are two types of validations in Salesforce.
Custom validation rules
Custom validation rules are the ones that you create through the setup menu, such as this one, which prevents the account name from being the string "invalid":
System validation rules
The Salesforce order of execution tells out there are system validations. This is seen in step two:
And then again in step five:
So system validation rules are defined by Salesforce and by the attributes of the metadata in question, like how many characters a field can hold, layout-specific rules, etc.
Now we know this error occurs when a record is inserted or updated, and it fails to meet the condition of a custom validation rule, not a system one.
Let's see two examples based on the validation rule I showed earlier:
Inserting/Updating data
I'm going to use the Salesforce Inspector Chrome extension to update an existing account's name to the string "invalid":
As expected, I see the FIELD_CUSTOM_VALIDATION_EXCEPTION error:
Apex DML
Let's do the same update but using Apex in the developer console:
Now we get the same error message but wrapped in a DmlException
addError() in Apex Triggers
You can use the addError() method to add a custom error message to an sObject instance, for example:
This code takes the first account in the trigger and adds an error to it. In the real world, you would only do this once you confirm that the record is in an invalid state, and thus, you need to throw an error.
In any case, with this in place, we will get an error whenever we try to insert an account:
Notice that this is the same error we would for custom validation rules. So in this context, Apex "doesn't know" whether the error is from the addError() method or a custom validation rule.
How you solve this problem depends on many factors. For example, perhaps the validation rule is correct, and all you need to do is fix the data you are importing to satisfy the validation rule condition.
Or maybe it is an old validation rule that should've been deactivated long ago, in which case you can simply deactivate it and continue your data loading.
Or perhaps, the validation rule is valid, but you want to temporarily bypass it (though that's not really recommended). In that case, you have two options:
The second option is probably the best one. This is known as a "bypass mechanism." Here's an example of what a validation rule looks like with a custom metadata type that can decide whether the rule should run or not:
So that’s it! Hopefully, this helped you get rid of this error.
Until the next post.