REST API using Workbench

REST API using Workbench

We can use Workbench tool to test the REST API in any Salesforce org. 

Let us discuss some popular scenarios to play with using Workbench tool in our Org.
In order to play with Rest API, we need to login to our Salesforce org --> Then go to Workbench (https://workbench.developerforce.com)  and --> Go to menu Utilities --> Select the REST Explorer.


- After selecting the Rest Explorer, we would have so many HTTP methods to choose and to perform any action to our org.

- Depending upon the requirement, we might use different methods at different scenarios. Some of the important methods as mentioned below,

HTTP MethodDescription
GETRetrieve data identified by a URL.
POSTCreate a resource or post data to the server.
DELETEDelete a resource identified by a URL.
PUTCreate or replace the resource sent in the request body.
Scenario 1: 
Fetch the complete list of objects in a salesforce org.

- Just frame the URI as "/services/data/v47.0/sobjects"

We can customize the URI as "/services/data/v47.0/xxxx ", here "xxxx" deonotes any operation that we would like to perform in our org.

So this URI "/services/data/v47.0/sobjects" would give the complete sobjects that were present in the org.

- Here, the URI framing /services/data/v47.0/  represents, we are using the REST API services with version 47.0


Note:- If we click on "Show Raw Response", then we will get the complete response in raw format (similar to JSON format).

Scenario 2: 
Fetch the complete LIMITS in a salesforce org.

- Just frame the URI as "/services/data/v47.0/limits"

 This would give us the complete list of limits that were intended to our org.



Scenario 3: 
Fetch the available REST API versions in a salesforce org.

- Just frame the URI as "/services/data/"

Then we will get the available REST API versions in our Salesforce org.


Scenario 4: 
Retrieve the Metadata for an object.

- Just frame the URI as "/services/data/v47.0/sobjects/Account"

This would fetch the complete metadata for the Account object along with the actual data present inside the Account object.


Scenario 5: 
Retrieve the field and other metadata for an object.

- Just frame the URI as "/services/data/v47.0/sobjects/Account/describe"

This would all the metadata for an object including information about each field, URL's and child relationships.



Till now, we have seen the HTTP GET method functionality. Now, we will play  with HTTP methods like POST, PATCH, DELETE.

Scenario 6: 
Create a new record using the REST API to our Salesforce org.

- Please select the HTTP method as POSTand frame the URI as "/services/data/v47.0/sobjects/Account" and in the request body, please write the required JSON file to create an account as,

{
  "Name" : "Express Solutions"
}

Then execute the URI, we will get the response as success or failure with errors.



Scenario 7: 
Need to update an existing record using REST API.

- In order to achieve this scenario, we need to use the PATCH method. Because we are updating the existing record, so firstly we need to fetch the record that we wish to update and then we need to apply the patch with changes.

- In the workbench, please select the method PATCH and go to the Salesforce org, please fetch the record Id (ex:- account ID) and then we need to execute the URI.

- Just modify the URI as, "/services/data/v47.0/sobjects/Account/0012w000006i0ej"

- Here, the account ID 0012w000006i0ej  refers to the newly created account ID that we created earlier in scenario 6.

- We need to develop the json file like this,

{
    "BillingCity" : "San Francisco"
}

- The above mentioned JSON file contains the changes that we desired to make to the account record.

- This will fetches the account record first and then applies our updated value into it and then saves the record.


Scenario 8: 
Need to Delete an existing record using REST API.

- In order to achieve this scenario, we need to use the DELETE method. So that it needs to fetch the record and then it deletes the record from the org.

- For this, we need to frame our URI like,
/services/data/v47.0/sobjects/Account/0012w000006i0ej

- This will fetch the passed record ID and deletes it from the org.



Scenario 9: 
Get the field values from the record using the REST API.

- In order to achieve this, we need to pass the record id and append the desired field names. Based on this, it will fetch us the values for the field names. For this, we need to use the GET method, because we are just fetching the values from the org.

- Please frame the URI as, 
"/services/data/v47.0/sobjects/Account/0012w000005AWCxAAO?fields=Type,Industry,Ownership,Phone"

- The above mentioned URI denotes that, for the specific Account ID, we are trying to fetch the actual field values.



These are some examples to show how REST API is helping us to ease our work while working with Salesforce.

Happy Learning!

Comments

Popular posts from this blog

Apex Code Character Limit (Part I)

Deployment in Salesforce using Change sets

SFDX Developer Guide

Customize Salesforce Login Page