You need to sign in to do that
Don't have an account?
Rocks_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
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
** 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
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
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
Thank you for repsonse. Could you please provide me REST API call for achieving this, that will be great helpful.
Thanks,
Anil
Thank you for repsonse. Could you please provide me REST API call for achieving this, that will be great helpful.
Thanks,
Anil
** 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 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 qualifications 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
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
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
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/
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
Thank you!!!
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!