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
Rocks_SFDCRocks_SFDC 

How to Query Metadata Components By using SOQL in Apex

Hi Folks,

I wanted to query metadata components( Fields, Objects, Validation Rules, Workflow Rules, Approval Process..etc) by using SOQL query in Apex. So, Could you please anyone check let me know do we have any workaround for this.

i'm just curious about whether is it possible by using Query or not.

Thanks,
Anil
Best Answer chosen by Rocks_SFDC
SKolakanSKolakan
Here are some examples to get you started

** All Apex classes and triggers
/services/data/v36.0/tooling/apexManifest

** For the list of custom objects 
/services/data/v36.0/tooling/query?q=Select+DeveloperName+From+CustomObject

** For all fields of an object
/services/data/v36.0/tooling/query?q=Select+DeveloperName+From+FieldDefinition+where+EntityDefinition.DeveloperName='Account'

**List of validation rules on account
/services/data/v36.0/tooling/query?q=Select+Id,Active,Description,EntityDefinition.DeveloperName,ErrorDisplayField,+ErrorMessage+From+ValidationRule+where+EntityDefinition.DeveloperName='Account'

**for all workflow rules (Including inactive )
/services/data/v36.0/tooling/query?q=Select+Id,Name,TableEnumOrId+From+WorkflowRule

**for all workflow rules by object (For ex: Account)
/services/data/v36.0/tooling/query?q=Select+Name,TableEnumOrId+From+WorkflowRule+where+TableEnumOrId='Account'

You can use Workbench (workbench.developerforce.com/login.php) Rest Explorer to play it.

For approval process, you can query it using SOQL
SELECT Id,Name,DeveloperName,Type,Description,TableEnumOrId,State,CreatedDate FROM ProcessDefinition
Where TableEnumOrId = 'Opportunity'

 

All Answers

SujilaSujila
You can use Schema Class to get Object and Field Metadata details from Salesforce. No query needed.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_schema.htm#apex_System_Schema_getGlobalDescribe

I am not sure how to query for the rest of the things you asked
SKolakanSKolakan
@Rocks_SFDC
You need to use tooling API for that. Here is the documentation: https://developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/api_tooling/
Andrew wrote an APEX wrapper to it which is readily available to use here: https://github.com/afawcett/apex-toolingapi
 
Rocks_SFDCRocks_SFDC
@Sreeni Kolakan,
Thank you for repsonse. Could you please provide me REST API call for achieving this, that will be great helpful.

Thanks,
Anil
Rocks_SFDCRocks_SFDC
@Sreeni Kolakan,
Thank you for repsonse. Could you please provide me REST API call for achieving this, that will be great helpful.

Thanks,
Anil
SKolakanSKolakan
Here are some examples to get you started

** All Apex classes and triggers
/services/data/v36.0/tooling/apexManifest

** For the list of custom objects 
/services/data/v36.0/tooling/query?q=Select+DeveloperName+From+CustomObject

** For all fields of an object
/services/data/v36.0/tooling/query?q=Select+DeveloperName+From+FieldDefinition+where+EntityDefinition.DeveloperName='Account'

**List of validation rules on account
/services/data/v36.0/tooling/query?q=Select+Id,Active,Description,EntityDefinition.DeveloperName,ErrorDisplayField,+ErrorMessage+From+ValidationRule+where+EntityDefinition.DeveloperName='Account'

**for all workflow rules (Including inactive )
/services/data/v36.0/tooling/query?q=Select+Id,Name,TableEnumOrId+From+WorkflowRule

**for all workflow rules by object (For ex: Account)
/services/data/v36.0/tooling/query?q=Select+Name,TableEnumOrId+From+WorkflowRule+where+TableEnumOrId='Account'

You can use Workbench (workbench.developerforce.com/login.php) Rest Explorer to play it.

For approval process, you can query it using SOQL
SELECT Id,Name,DeveloperName,Type,Description,TableEnumOrId,State,CreatedDate FROM ProcessDefinition
Where TableEnumOrId = 'Opportunity'

 
This was selected as the best answer
Rocks_SFDCRocks_SFDC
@Sreeni

This was the great start for us by using REST API call.

When we try to fetch the fullname of the component based on LastModifiedDate in Where Condition for Custom Objects, Validation Rules or Custom Fields then facing an following error in Query.

"When retrieving results with Metadata or FullName fields, the query qualificatio​ns must specify no more than one row for retrieval"

Sample Query: /services/data/v36.0/tooling/query?q=Select+Id,Active,Description,EntityDefinition.DeveloperName,FullName,ErrorDisplayField,+ErrorMessage+From+ValidationRule+Where+LastModifiedDate>2014-09-25T22:44:32.000Z

Could you please let me know do we have any limitations on these queries.

Thanks,
Anil
SKolakanSKolakan
@Rocks_SFDC
If you query Metadata, you should have your criteria such that it returns only one record 
https://developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/api_tooling/tooling_api_objects_validationrule.htm

Here is what documentation says
"Validation rule metadata.
Query this field only if the query result contains no more than one record. Otherwise, an error is returned. If more than one record exists, use multiple queries to retrieve the records. This limit protects performance."

So, you can get all validation rules with Id and query each using Id to get it's complete metadata.

I recommend you to go through the documentation - https://developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/api_tooling/
and also look at apex wrapper written on top of tooling api: https://github.com/afawcett/apex-toolingapi
 
Rocks_SFDCRocks_SFDC
@Sreeni

Thank you very much for your inputs.

I'm not much familar with SOAP API, Could you please let me know how we can execute the code in (https://github.com/afawcett/apex-toolingapi ) github and how to see the output.

Thanks,
Anil
SKolakanSKolakan
@Rocks_SFDC

You can install code by directly clicking on deploy button on git hub. Since it is written in APEX, you can use it like any other class. Here is a blog post on how to use it: https://andyinthecloud.com/category/tooling-api/
Darrell GallegosDarrell Gallegos
Is there a method to pull information in regards to Email Alerts? Total count, active, inactive, times used? Trying to clean up.
Rocks_SFDCRocks_SFDC
Hi Darrell,

Please use the following query in workbench.

/services/data/v36.0/tooling/query?q=Select+DeveloperName+From+WorkflowAlert

We can also add where conditions to the above query.

Thanks,
Anil
Darrell GallegosDarrell Gallegos
@Rocks_SFDC

Thank you!!!
jaw999jaw999
Can one use this to query the entry criteria of an approval process?
Can one use this query the assignees in approval steps ? I don't mean open approvals, I mean the steps that will assign an approval to someone when invoked. thanks!
frasuyetfrasuyet
The samples above are a great starting point. If you need a one time dump of the result set from the raw response, I've used https://json-csv.com/ to generate a simple .csv output.