Introducing Workbench
Workbench is not an official Salesforce.com product. But we will utilize Salesforce to perform the data manipulation operations like select, insert, upsert, update and delete by just logging into your Salesforce account (supports both Sandbox and Production). This is the official website to login Workbench with Salesforce: https://workbench.developerforce.com/login.php.
As of now, keep the API version as existing only and click on the “Login with Salesforce” button.
We need the REST Explorer. Navigate to the “utilities” tab and click on “REST Explorer”.
You will see the UI like in the following illustration. We need to opt GET to fetch the records from Salesforce in this entire guide. We need to specify the URI that fetches the Salesforce records and click the “Execute” button.
Retrieve Specific Record with Salesforce ID
Based on the Salesforce record ID, we can fetch Salesforce’s entire record. We need to set the URI as follows:
Here, the “objectAPIName” is the Salesforce Standard/Custom object and the “id” refers to the Salesforce ID.
Return:
You will get the HTTP/1.1 200 OK raw response in the JSON format like in the following:
"attributes" : {
"type" :
"url" :
},
"field" : Value,
...
}
Example:
In this example, we fetch the 5005i00000W4GM5AAN case record.
Result:
We can see that the response is generated in JSON format.
We can also view the results directly from here:
Retrieve Multiple Records with Query
It’s time to retrieve multiple records from the Salesforce object. Previously, we specified the sobjects in the URI. Here, we need to specify a query that takes the query as a parameter.
We need to use “+” as a delimiter to join the keywords in a query. It returns the totalSize and records in a folder. The folder name for each record is [Item 1],…[Item n].
Example 1:
Let’s return the records that include the CaseNumber, status, priority, and description from the Case object.
Result:
When you click on “Expand All”, you will see all the records with its attributes and values.
Let me show the first and last records:
Example 2:
Let’s return only three records with the same fields as seen in the first example.
Result:
The first two records that are present in the Case object are returned.
Example 3:
Let’s specify the WHERE condition in the query that selects the records with the “New” status.
Result:
Five records exist with the “New” status.
Custom Rest Resource in Apex
We can utilize the Salesforce Apex that returns the record from the Salesforce object by specifying the URI in the Workbench. To write REST in Apex, we have to utilize some annotations which access the REST API in your Apex class. Make sure that our Apex class must be globally static.
1. @RestResource Annotation
This annotation is used to enable which exposes an Apex class as a REST resource. It takes the urlMapping as a parameter that is used to locate the URI in the Workbench.
Syntax: @RestResource(urlMapping=’/Version/ApexClassName/’)
The “version” is your Workbench version like V56.0 and the “ApexClassName” is your Apex class where the Rest API resources are involved.
2. @HttpGet Annotation
This annotation is used to enable which exposes an Apex class as a REST resource. It is called when an HTTP GET request is sent to the server and returns the specified resource.
Syntax: @httpGet
Example 1: Single Param
Write the “RestApi_Get_Record.apxc” Apex class that involves the “Rest Get” method to return the id, CaseNumber, status, priority, and origin from case from the Case object.
global class RestApi_Get_Record{
// REST - Get Method
@httpGet
global static Case getCaseDetails(){
// Create object for Case object
Case case_obj = new Case();
Map<String,String> paramsMap = RestContext.request.params;
// Get the case id
String caseid =paramsMap.get('input_id');
// SOQL query that will return id,CaseNumber,Status,Priority,Origin from Case from
// the Case object
case_obj = [select id,CaseNumber,Status,Priority,Origin from Case where Id =:caseid];
return case_obj;
}
}
URI and Result:
Go to Workbench and navigate to the REST Explorer. Pass the id as 5002t00000Pdzr2AAB to the input_id param.
Explanation:
- Create an object for the “case_obj” case.
- Get the params using the RestContext.request.params.
- Get the case id from the param input_id and store this in the caseid variable.
- Write the SOQL query that returns the id, CaseNumber, status, priority, origin from case from the Case object of the “caseid” case.
- Return the case object (case_obj).
Example 2: Multiple Params
Utilize the previous Apex Class and get the “Status” param along with the id. Specify these two params in the Workbench URI that is separated by “&”.
global class RestApi_Get_Record{
// REST - Get Method
@httpGet
global static Case getCaseDetails(){
// Create object for Case object
Case case_obj = new Case();
Map<String,String> id_param = RestContext.request.params;
Map<String,String> status_param = RestContext.request.params;
// Get the id_param into the case_id
String case_id = id_param.get('input_id');
// Get the status_param into the case_status
String case_status =status_param.get('status');
case_obj = [select id,CaseNumber,Status,Priority,Origin from Case where Id =:case_id and Status =: case_status];
return case_obj;
}
}
URI and Result:
Go to Workbench and navigate to the REST Explorer. Pass the input_id as 5002t00000PdzqwAAB and the status as “Closed” in the URI.
Conclusion
We discussed three scenarios of retrieving the Salesforce records through Salesforce REST API using Workbench. To return a specific record, we need to specify the sObject by passing the id as parameter in the URI. Similarly, we pass the query parameters to get specific records. Using Apex, we can create our own “Get” method to select the record based on single/multiple params.