Salesforce REST API: How to Execute SOQL Queries using REST APIs


Looking for Salesforce Training & HandsOn Projects?



Salesforce REST API Framework has a well-defined architecture that offers a vast variety of operations to perform using REST Endpoints.

 

 “Query” resource can execute SOQL Query and return all the records in a single response, but it can return the records in multiple batches if need be. In the case of multiple batches, each response object will also contain the “nextRecordUrl” attribute that contains the Endpoint URL for the next batch of records that can be retrieved.

 

This is a very handy approach and can be used to design performance-critical solutions using Salesforce Programming Design paradigms or any Modern Web Technology of your choice. That’s the beauty of REST-based solutions and it’s been fun to play around with it.

 

In this article, we will discuss the implementation details of the “Query” resource that allows executing SOQL queries using REST API Endpoint. It supports querying both Salesforce Standard and Custom SObjects.

 

We will start this by launching Postman which is an amazing tool to test your REST APIs or even work with any SOAP or REST API Endpoints. Once Postman is up we can go through the following steps-


Request Object

 

Step-1: Since we are going to get (Read Operation) the records we need to set the request type as “GET”

 

Step-2: Then we append “query” resource to the Salesforce instance address. Query resource process the queries with following formats-

 

<Salesforce Instance Address>/services/data/v<API Version>/query?q=<Encoded SOQL Query>

 

In our case this query request would something like this:

 

https://na136.salesforce.com/services/data/v47.0/query?q= (Query as shown below)

 

Here it is important to note that query string parameter “q” can take any valid SOQL Query as an input

 

Step-3: Select “Headers” to add required headers to the request

 

Step-4: Include the “Authorization” header to the request, this head is required to authenticate this request by Salesforce REST API Endpoints. We have to specify the security token as its values.

 

If like to know more about security token, please refer to my earlier article Salesforce REST API Authentication: The Login Request

 

Step-5: With all this, our request is ready to hit the server. Click the “Send” button and launch the GET request


Before we proceed any further I would like to introduce one of my favorite online tool to Encode & Decode URLs which I have been using so frequently, and awesome part of the deal is, its’ free.

 

Step-6: To get to this tool, open your browser and type the address: http://url-encode-decode.com

 

Step-7: Enter your query to a text area on the left

 

Step-8: Click on “Encode Url” button

 

Step-9: And your encoded text is ready in the text area on the right

 

This is super easy to use and a real time saver. I would recommend this to add to your developer’s tool kit.



It is important to note that when we implement the same using a programming language let it be JavaScript or Apex, we always have to make use of available encoders exposed as functions.

This approach using an external tool for encoding is good for quick testing and some time for no-code solutions like using Postman or Salesforce Workbench.

 

Now let’s move back to our original request, don’t forget we are waiting for the response. It is time to analyze the response object that we received.


Response Object

 

Step-10: The very first thing we notice is the format of this response it JSON (favored by all modern Web Technologies)

 

Step-11: Next important fact is the size represented by the property “totalSize” of the response object that will tell us how many records are returned as the response

 

Step-12: Response object also contains SObject related properties like “type” property which holds the name of the SObject that is used to pull records from and “url” which holds the link to a specific record pointing a record identifier

 

Step-13: It also provides the details of the record, which includes the data on the fields that we queried in SOQL Query

 

“Name”: Represents the value of Name Field

“Customer__r”: Since we have Customer__c lookup (Account SObject) field on Invoice__c SObject, the response object returns the corresponding relationship object for the lookup field “__r” represents the lookup relationship.

 

We can access the related data for the lookup field using this relationship with properties like “type” holds the name of the SObject used as lookup source and “url” which holds the link to the Account record associated with this Invoice record, “Name” which holds the Account Name associated with the relationship.

 

“Amount__c”: which holds the amount value for the Invoice record



So with this, you can see how informative is the response object could be. I am sure you will always find more when u keep looking into data. Believe me, it is amazing.

 

We can have more options to play with, let explore them.

 

Step-14: We can add more request headers to modify the behavior of the request. For example “Accept”: “application/json” directs the REST API Endpoint to process the request and returns data in JSON format

 

Step-15: It shows that the data is returned in JSON format



Step-16: If we modify the request headers to “Accept”: “application/json” it will direct the REST API Endpoint to process the request and returns data in XML format

 

Step-17: It shows that the data is returned in XML format. If we analyze this response closely we can see it is the same data just in a different format (XML)



Conclusion


Salesforce REST API Framework is powerful and can be used to develop robust applications no matter they are Salesforce Applications hosting inside Salesforce Cloud or any Web Based hosting on an external server.

 

Hope you enjoyed this article. Please leave your comments to let me know how you do like the content and how you do find it helpful to learn the topic.

Comments