REST service to call own org metadata components like Validation rule, workflows etc

Some times If you want validation rules for some APEX logic then you can use the following code. You should need a RESTFULL call to get the data.

Response type is JSON.

Used session ID of the user so that it will not required to set username and password.

Before running this script make sure you have remote site created for the org URL otherwise service will not recognize code.

APEX CODE:
public static void ValidationRule(){
    String salesforceHost = System.Url.getSalesforceBaseURL().toExternalForm();

    String url =   salesforceHost + '/services/data/v30.0/tooling/query/?q=Select+fullname+,metadata+from+ValidationRule';
   
    HttpRequest req = new HttpRequest();
   
    req.setMethod('GET');
    req.setEndpoint(url);
    req.setHeader('Content-type', 'application/json');
    req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
    Http http = new Http();
   
    HTTPResponse res = http.send(req);
    System.debug(res.getBody());

    //Do your stuff
}
   


Comments