Tooling API:
We can accomplish following tasks using Tooling API.:
- Retrieve metadata about an object’s field.
- Retrieve metadata of custom and standard object properties.
- Get metadata information about Apex Classes, Visualforce Pages, Apex Triggers.
- Get Code coverage details of Apex classes and Trigger.
- Retrieve metadata information about ValidationRule and WorkflowRules.
- Query RecentItems used in your SFDC org.
- Etc...
In this blog, I am going to retrieve Metadata information about ValidationRule and WorkflowRule. I am using REST (GET) method here to make a callout.
HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID()); //Get user Session ID
req.setHeader('Content-Type', 'application/json');
String domainUrl=URL.getSalesforceBaseUrl().toExternalForm();
string query='Select+id,FullName,createdDate,TableEnumOrId,ValidationName,Metadata+from+ValidationRule';
//string query='Select+id,Name+from+WorkflowRule'; //use this query to get Workflow Rule
req.setEndpoint(domainUrl+'/services/data/v33.0/tooling/query/?q='+query);
req.setMethod('GET');
Http h = new Http();
HttpResponse response = h.send(req);
system.debug(response.getBody());
JSON Response:
{
"size": 1,
"totalSize": 1,
"done": true,
"queryLocator": null,
"entityTypeName": "ValidationRule",
"records": [
{
"attributes": {
"type": "ValidationRule",
"url": "/services/data/v33.0/tooling/sobjects/ValidationRule/03df40000002gPOAAY"
},
"Id": "03df40000002gPOAAY",
"FullName": "Account.Account_Name_not_starts_with_Test",
"CreatedDate": "2018-06-15T05:41:21.000+0000",
"TableEnumOrId": "Account",
"ValidationName": "Account_Name_not_starts_with_Test",
"Metadata": {
"description": null,
"errorConditionFormula": "CONTAINS(Name, 'Test')",
"errorDisplayField": null,
"errorMessage": "Account name should not contains Test",
"urls": null,
"active": true
}
}
]
}
We can parse this JSON response according to our requirements.
The Tooling API Objects. https://developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/api_tooling/reference_objects_list.htm
HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID()); //Get user Session ID
req.setHeader('Content-Type', 'application/json');
String domainUrl=URL.getSalesforceBaseUrl().toExternalForm();
string query='Select+id,FullName,createdDate,TableEnumOrId,ValidationName,Metadata+from+ValidationRule';
//string query='Select+id,Name+from+WorkflowRule'; //use this query to get Workflow Rule
req.setEndpoint(domainUrl+'/services/data/v33.0/tooling/query/?q='+query);
req.setMethod('GET');
Http h = new Http();
HttpResponse response = h.send(req);
system.debug(response.getBody());
JSON Response:
{
"size": 1,
"totalSize": 1,
"done": true,
"queryLocator": null,
"entityTypeName": "ValidationRule",
"records": [
{
"attributes": {
"type": "ValidationRule",
"url": "/services/data/v33.0/tooling/sobjects/ValidationRule/03df40000002gPOAAY"
},
"Id": "03df40000002gPOAAY",
"FullName": "Account.Account_Name_not_starts_with_Test",
"CreatedDate": "2018-06-15T05:41:21.000+0000",
"TableEnumOrId": "Account",
"ValidationName": "Account_Name_not_starts_with_Test",
"Metadata": {
"description": null,
"errorConditionFormula": "CONTAINS(Name, 'Test')",
"errorDisplayField": null,
"errorMessage": "Account name should not contains Test",
"urls": null,
"active": true
}
}
]
}
We can parse this JSON response according to our requirements.
It's so nice article thank you for sharing a valuable content. google cloud online course
ReplyDeleteAm getting [{"message":"This session is not valid for use with the REST API","errorCode":"INVALID_SESSION_ID"}] error
ReplyDeleteThank you so much for sharing this worth able content with us. The concept taken here will be useful for my future programs and i will surely implement them in my study. Keep blogging article like this.
ReplyDeleteBest CRM System