Home > Blog > Rest API using Apex

Rest API using Apex

TOOLING API (REST API) We can use Tooling API to build and create custom development tools or app for creating Force.com Applications.  SOQL allow us to retrieve data in small amount. If you want to retrieve the org metadata, you have to use the  API ,that make the tooling API a better way for developing interactive applications. Below are the steps defined to create a connection between your Salesforce Org and http: Step1: Create connection between your org and the HTTP request Type : HttpRequest req = new HttpRequest(); req.setHeader(‘Authorization’, ‘Bearer ‘ + UserInfo.getSessionID()); req.setHeader(‘Content-Type’, ‘application/json’); Step 2: Add this piece of code to send request and get the body of response: Http h = new Http(); HttpResponse res = h.send(req); system.debug(res.getBody()); Step 3: To get all available objects: req.setEndpoint(‘http://na1.salesforce.com/services/data/v28.0/tooling/sobjects/’); req.setMethod(‘GET’);  Similarly you can query for other metadata components by using SOQL in Apex: ** 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 also use Workbench (workbench.developerforce.com/login.php). Rest Explorer to play it.     Let’s start up with some coding as well: Save this code and create an Apex class with the name:  DemoforToolingApi Public class DemoforToolingApi { public String returnValuestr ; public HttpResponse response; public Http http; public HttpRequest request; public DemoforToolingApi () { returnValuestr = httpCalloutMethod(‘/services/data/v36.0/sobjects/Account’); System.debug(‘returnValuestr –> ‘+returnValuestr ); } public String sToken=UserInfo.getSessionId(); public String httpCalloutMethod(String str) { try { http = new Http(); request = new HttpRequest(); String getORGURL=URL.getSalesforceBaseUrl().toExternalForm(); request.setEndpoint(getORGURL+str);  //Your URI request.setMethod(‘GET’); request.setHeader(‘Authorization’, ‘Bearer ‘ + sToken); //if the request and response for your REST call is JSON, use the code below. request.setHeader(‘Content-Type’, ‘application/json’); request.setHeader(‘accept’, ‘application/json’); response = http.send(request); return response.getBody(); } catch(Exception e) { System.debug(‘Error —-> ‘+e); return null; } } }   Open Developer console  : Go to debug Select : Open Execute Anonymous Window Copy and paste : DemoforToolingApi obj = new DemoforToolingApi(); See debug logs:    
Enquire Now
X

lets get over a cup of coffee and discuss!