Salesforce

Retrieve Records Using Rest API in Salesforce

In this guide, we will discuss how to retrieve the Salesforce records with REST API through Workbench in Salesforce. As part of this guide, we will discuss how to use Workbench, fetch specific records using sObject, and fetch multiple records from an object using the query and fetch records by writing the Apex custom REST API. We will utilize the Salesforce Standard Case object for demonstration. There is no need to create any case record in the Salesforce backend. We use the existing standard case record that is provided by Salesforce.

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:

/services/data/v56.0/sobjects/objectAPIName/id

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.

URI: /services/data/v56.0/sobjects/Case/5005i00000W4GM5AAN

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.

URI: services/data/v57.0/query/?q=SELECT+field1,field2,....+from+ObjectAPIName

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.

/services/data/v57.0/query/?q=SELECT+CaseNumber,Status,Priority,Description+from+Case

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.

/services/data/v57.0/query/?q=SELECT+CaseNumber,Status,Priority,Description+from+Case+limit+2

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.

/services/data/v57.0/query/?q=SELECT+CaseNumber,Status,Priority,Description+from+Case+where+Status='New'

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.

@RestResource(urlMapping='/v56.0/RestApi_Get_Record/')
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.

/services/apexrest/v56.0/RestApi_Get_Record/?input_id=5002t00000Pdzr2AAB

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 “&”.

@RestResource(urlMapping='/v56.0/RestApi_Get_Record/')
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.

/services/apexrest/v56.0/RestApi_Get_Record/?input_id=5002t00000PdzqwAAB&status=Closed

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.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain