How to Query Validation Rule in Salesforce?

In Salesforce, validation rules ensure data security by restricting certain data entries or specific business logic when users try to create or update records. In Salesforce, while working with validation rules, we often need to get the list of validation rules to clean up data or to fetch information regarding the validation rules.

In this Salesforce tutorial, I will explain how to query the Validation rules in Salesforce with the help of examples.

Query Validation Rules in Salesforce

To query the Salesforce validation Rules, navigate to the Salesforce developer console and enter the code below in the query editor.

Ensure that you have selected the checkbox Use Tooling API.

To query all the validation rules in your org, execute the query below in the developer console’s query editor.

SELECT Id, ValidationName, Active, Description, EntityDefinition.DeveloperName, 
ErrorDisplayField, ErrorMessage,LastModifiedDate,CreatedDate FROM ValidationRule

After executing the above query in the query editor, we will see the existing validation rules in the output.

SOQL Validation Rules in Salesforce

This way, we can query the validation rules in Salesforce using SOQL.

When you need to query details like metadata error conditions, you can only query one result by providing the ID of that specific validation rule. Otherwise, it will return the Malinformed_Query error.

SOQL to query error condition of validation rule

The correct way to query the error condition of the validation rule is mentioned in the below SOQL query, where I have entered the ID of the specific validation rule.

SELECT Id, ValidationName, Metadata, Description, EntityDefinition.DeveloperName, ErrorDisplayField, ErrorMessage 
FROM ValidationRule
WHERE Id = '03dJ3000000GsKdIAK'

Output:

SOQL error condition of validation rule in Salesforce

When we need to query the validation of any standard or custom Salesforce object, then we will use EntityDefination.DeveloperName with the WHERE clause in the SOQL query.

For example, to query the validation rules of the “Account” object, we will execute the SOQL query in the following way.

SELECT Id, ValidationName, Description, EntityDefinition.DeveloperName, ErrorDisplayField, ErrorMessage 
FROM ValidationRule
WHERE EntityDefinition.DeveloperName = 'Account'

Output:

Query Validation rule for Specific object in Salesforce

This way, we can query the validation rules for the specific objects in Salesforce by following the above method. Just make sure that you have enabled the checkbox Using Tooling API.

You may also like to read:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.