function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mariana D'AbreuMariana D'Abreu 

Query Validation Rules associated with an Object using REST API

I need to retrieve all the validation rules associated with a given object, using the REST API.

Looking in doc/forums, I've found that I could retrieve validation rules associated with an object using the following query:

https:///services/data/v35.0/tooling/query?q=Select Id,Active,Description,ErrorDisplayField, ErrorMessage From ValidationRule Where EntityDefinitionId = 'xxxxx'

The problem is that I don't have the object'ID needed to filter, I only have its name. Is there any way to filter using the object's name?

Another alternative seems to be getting the object'ID, but it seems that Metadata REST API (i.e. describe) does not return it.

Any help on this is very appreciated.

Mariana
Best Answer chosen by Mariana D'Abreu
pconpcon
You can filter by name by using EntityDefinition.DeveloperName
 
/services/data/v35.0/tooling/query?q=Select Id,Active,Description,EntityDefinition.DeveloperName,ErrorDisplayField, ErrorMessage From ValidationRule where EntityDefinition.DeveloperName = 'Account'

I did find that the developer name for custom objects were not what I expected, so you might want to verify the value by not having your where first.

All Answers

pconpcon
You can filter by name by using EntityDefinition.DeveloperName
 
/services/data/v35.0/tooling/query?q=Select Id,Active,Description,EntityDefinition.DeveloperName,ErrorDisplayField, ErrorMessage From ValidationRule where EntityDefinition.DeveloperName = 'Account'

I did find that the developer name for custom objects were not what I expected, so you might want to verify the value by not having your where first.
This was selected as the best answer
Mariana D'AbreuMariana D'Abreu
Great, it worked! 

Based on your answer, I finally used the following filter:

/services/data/v35.0/tooling/query?q=
Select Id,Active,Description,ErrorDisplayField, ErrorMessage From ValidationRule Where EntityDefinition.QualifiedApiName = 'SVMXC__Parts_Request__c'

Thank you very much!
vaishali sharmavaishali sharma
Hi, Can I achieve it in apex class? Please guide me on this.
pconpcon
The only way to do this is to make a callout to the ToolingAPI via apex.  You can do this directly or you can use a wrapper like this one [1]

[1] https://github.com/afawcett/apex-toolingapi/tree/apex-toolingapi-rest
vaishali sharmavaishali sharma
Thank you pcon.
 
vaishali sharmavaishali sharma
Hi pcon, I would be thankful to you if you will tell me the direct way of achieving this functionality like as you said '"You can do this directly or you can use a wrapper like this one [1]" .
 
pconpcon
Please, follow the links in the git hub [1] [2] they have examples of how to use the project.  If this library will work you will want to call the query method with the data above.  If the the github library does not work, you will have to make a custom REST callout via apex.  For that I would recommend reviewing the Trailhead on on Apex REST Callouts [3].  If you have any SPECIFIC questions, please create a new post with those.

[1] https://andyinthecloud.com/2014/01/05/querying-custom-object-and-field-ids-via-tooling-api/
[2] https://andyinthecloud.com/2014/03/17/going-native-with-the-apex-uml-tool-and-tooling-api/
[3] https://trailhead.salesforce.com/modules/apex_integration_services/units/apex_integration_rest_callouts
__vic____vic__

Hey pcon,

How can I retrieve 'errorConditionFormula' of ValidationRule?
I tried following code in workbench.

/services/data/v41.0/tooling/query?q=Select+Id,Active,Description,EntityDefinition.DeveloperName,ErrorDisplayField,Metadata,ErrorMessage+From+ValidationRule+where+EntityDefinition.DeveloperName='Position__c'
I have total 3 ValidationRules for `Position__c` object, but 0 results were returned.
pconpcon
The problem is the contents of EntityDefinition.DeveloperName is not the object name.  You want to use the QualifiedApiName for that
 
/services/data/v41.0/tooling/query?q=Select+Id,Active,Description,EntityDefinition.DeveloperName,EntityDefinition.QualifiedApiName,ErrorDisplayField,Metadata,ErrorMessage+From+ValidationRule+where+EntityDefinition.QualifiedApiName='Position__c'