Salesforce

Demystifying ‘field custom validation’ errors in Salesforce

Written by
Pablo Gonzalez
Business Engineering Architect
March 15, 2022
4
min read

Table of Contents

View all guides

Heading

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique. Duis cursus, mi quis viverra ornare, eros dolor interdum nulla, ut commodo diam libero vitae erat. Aenean faucibus nibh et justo cursus id rutrum lorem imperdiet. Nunc ut sem vitae risus tristique posuere.

This is some text inside of a div block.

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:

Get started with Salto

Track critical changes to your Salesforce orgs

Try Salto 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.

Understand the impact of every change in your org

Try Salto 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
Pablo Gonzalez

Pablo is a Business Engineering Architect at Salto. He is the developer of HappySoup.io and has 11 years of development experience in all things Salesforce.

Written by