Sort by Topics, Resources
Clear
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Salto for

Salesforce

Articles

SHARE

Demystifying ‘field custom validation’ errors in Salesforce

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:

Experience the Ease & Confidence of NetSuite Customizations with Salto

Automate the way you migrate Jira configurations from sandbox to production

STAY UP TO DATE

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Get started with Salto

Everything you need for error-free Salesforce deployments

Try it free

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.

When this occurs

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.

STAY UP TO DATE

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

The org comparison tool every admin needs

Have you ever seen XML-free org comparison tool?

Try it free

How to get rid of it

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:

  • Deactivate all the automation, either manually or via a Metadata API script. Insert the records, and turn everything back on.
  • Modify all their automation to reference a custom setting or custom metadata type with a boolean field. The automation would be configured not to run if the boolean field is false.

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.

WRITTEN BY OUR EXPERT

Pablo Gonzalez

Business Engineering Architect

Pablo is the developer of HappySoup.io and has 11 years of development experience in all things Salesforce.

Sort by Topics, Resources
Clear
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Salto for

Salesforce

Salesforce

SHARE

Demystifying ‘field custom validation’ errors in Salesforce

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:

What if Zendesk was 4x less work?

Request a Demo Get started with Salto

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.

When this occurs

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.

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

How to get rid of it

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:

  • Deactivate all the automation, either manually or via a Metadata API script. Insert the records, and turn everything back on.
  • Modify all their automation to reference a custom setting or custom metadata type with a boolean field. The automation would be configured not to run if the boolean field is false.

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.

WRITTEN BY OUR EXPERT

Pablo Gonzalez

Business Engineering Architect

Pablo is the developer of HappySoup.io and has 11 years of development experience in all things Salesforce.