Salesforce REST API: A Complete CRUD & Postman Testing Guide

Introduction to Salesforce REST API:

Salesforce is a powerful and widely used customer relationship management (CRM) platform that allows businesses to manage their sales, customer service, and marketing operations efficiently. One of the essential features of Salesforce is its REST API, which enables developers to interact with Salesforce data and perform various operations programmatically.

REST (Representational State Transfer) is an architectural style used to build web services that are lightweight, scalable, and platform-independent. Salesforce REST API follows RESTful principles and allows developers to access and manipulate data in Salesforce using HTTP methods like GET, POST, PUT, PATCH, and DELETE.

In this blog post, we will explore how to use Salesforce REST API to insert, update, upsert, and delete records. We will also provide a step-by-step guide to test the API using Postman, a popular API testing tool.

Prerequisites

Before we begin, make sure you have the following in place:

  • A Salesforce developer account with API access enabled.
  • Postman tool installed on your machine.

Authenticating Salesforce REST API:
Salesforce REST API requires authentication to access its resources. For this purpose, Salesforce uses OAuth 2.0 authentication flow. To obtain an access token, follow these steps:

Create a Connected App in Salesforce and note down the Consumer Key and Consumer Secret.

Setup > Home > App Manager > New Connected App

  1. Enter Connected App Name
  2. Contact Email
  3. Select / Check Enable OAuth Setting
  4. Enter Callback URL (you can put our salesforce instance base URL)
  5. Select the Selected OAtuth Scopes as per you need.
  6. Save
  7. Continue
  8. Click on Manage Consumer Details

Copy consumer key and secrete this will be used for OAuth 2.0 authentication.

Use the OAuth 2.0 authentication flow to obtain an access token using the Consumer Key and Secret.

Inserting Records using Salesforce REST API:
To insert a new record, you’ll need to make a POST request to the Salesforce REST API’s endpoint for the desired object. For example, to insert an Account record, the endpoint would be:

Example Request:

Updating Records using Salesforce REST API:
To update an existing record, make a PATCH request to the Salesforce REST API’s endpoint for the specific record. You need to provide the record ID in the URL and the updated fields in the request body.

Example Request:

Upserting Records using Salesforce REST API:
Upsert operation allows you to either insert a new record or update an existing one based on a specified external ID. The external ID field must be marked as an “External ID” in Salesforce.

Example Request:

Deleting Records using Salesforce REST API:
To delete a record, make a DELETE request to the Salesforce REST API’s endpoint for the specific record ID.

Example Request:

Testing Salesforce REST API with Postman:
Now that we have understood how to perform various CRUD operations using Salesforce REST API, let’s test these operations using Postman.

Step-by-step guide to test the API using Postman

Get the access token

Create a new request.

  1. Choose the appropriate HTTP method (GET, POST, PATCH, DELETE) for the operation you want to perform.
  2. Set the request URL to the Salesforce REST API endpoint for the desired object and operation.
  3. Add the “Authorization” header with the value “Bearer YOUR_ACCESS_TOKEN” to authenticate the request.
  4. If required, set the “Content-Type” header to “application/json” for requests with a request body (POST, PATCH).
  5. For POST and PATCH requests, add the JSON payload to the request body as shown in the examples above.
  6. Click the “Send” button to execute the request and see the API response.

Conclusion

Salesforce REST API is a powerful tool that allows developers to interact with Salesforce data programmatically. In this blog post, we covered how to insert, update, upsert, and delete records using Salesforce REST API, along with step-by-step instructions to test the API using Postman. With this knowledge, you can now leverage the capabilities of Salesforce REST API to build powerful integrations and applications that interact seamlessly with your Salesforce instance. Happy coding!

About the blog

SFDCLessons is a blog where you can find various Salesforce tutorials and tips that we have written to help beginners and experienced developers alike. we also share my experience and knowledge on Salesforce best practices, troubleshooting, and optimization. Don’t forget to follow us on:

Newsletter

Subscribe to our email newsletter to be notified when a new post is published.

Advertisements
Advertisements

Arun Kumar

Arun Kumar is a Salesforce Certified Platform Developer I with over 7+ years of experience working on the Salesforce platform. He specializes in developing custom applications, integrations, and reports to help customers streamline their business processes. Arun is passionate about helping businesses leverage the power of Salesforce to achieve their goals.

Leave a Reply