Salesforce REST API and Postman Testing

In Salesforce we have lots of standard APIs available. Salesforce also allows you to develop your own custom API and expose your Apex classes and methods so that external applications can access your code and your application through the REST architecture. Detailed documentation on how to create REST API can be referenced here.

In this blog we will see, how can we test our own custom API using Postman. Postman is an API platform for building and using APIs. Postman simplifies each step of the API lifecycle and streamlines collaboration.

Implementation:

  1. Salesforce:
    • Create a Salesforce REST Resource.
    • Create a Salesforce Connected App.
    • Create a Salesforce API User.
  2. Postman:
    • Authorize the Salesforce org and get the access token.
    • Call the APEX REST service using the access token. 
Now let's create a sample Salesforce REST Resource.

1. Create Salesforce REST Resource: Open developer console and create following REST Resource CaseRestResource which has a POST method to create a Case in Salesforce.

@RestResource(urlMapping='/Case/*')
global with sharing class CaseRestResource {
    @HttpPost
    global static String doPost(String subject, String description) {
        Case newCase = new Case();
        newCase.Subject = subject;
        newCase.Description = description;
        insert newCase;
        return newCase.Id;
    }
}

This REST resource creates a new Case and returns the id of the newly created Case back.

2. Create Salesforce Connected App: 
  • Log in to your DEV/Sandbox/Production environment and go to quick find box and search for App Manager.
  • Click on New Connected App.
  • Fill following details:
    • Connected App Name: <Enter any name of your choice>
    • Contact Email: <Enter your email address>
    • Enable OAuth Setting: Enable
      • Selected OAuth Scopes: Manage user data via APIs(api)
    • Require Secret for Web Server Flow: Enable
    • Require Secret for Refresh Token Flow: Enable
    • Click Save
  • Once you create the new Connected App, it take 5-10 min to get activated. Once activated, you will see the Consumer Key and Consumer Secret as shown below. Copy them as these will be  used in authorizing our REST API when consumed from Postman.
3. Create a Salesforce API User: Go to Setup and quick find box and enter Users and create a Salesforce API User which will be used for our REST API access. Keep the User Name and Password safe as we will be using them to authorize the user in Postman.
Make sure if you are using the Custom Profile, it has permissions to create Case records.

Consuming Salesforce REST API using Postman:

Now as we are done with all the Salesforce implementation part, let's focus on how to consume our Salesforce REST API using Postman.

There are two ways to use Postman.

  1. Download(Desktop App) Mode: Click here to download the Postman as a desktop app.
  2. Online Mode: Click here to use Postman online.
Process is same for both, so I will explain using the Online Mode.

1. Authorize your Salesforce Org and get the Access Token and Instance URL:
  1. Go to your Workspace in Postman
  2. Use https://login.salesforce.com/services/oauth2/token in the URL box, while connecting with the developer/production org. If you want to connect to your sandbox org, use https://test.salesforce.com/services/oauth2/token
  3. Choose POST method since it's a POST request to get access to your Salesforce org.
  4. Pass following parameters in the Body
    • Radio Group Options: form-data
    • username: <Enter user name for Salesforce API User created in step 3 above>
    • password: <Enter Password for Salesforce API User created in step 3 above along with the Salesforce Org Security Token>
    • grant_type: password
    • client_id: <Enter Consumer Key copied from the Connected App created in step 2 above>
    • client_secret: <Enter Consumer Secret copied from the Connected App created in step 2 above>
  5. Your request will look like below

  6. Click on Send and you will get the access token and others attributes in JSON in the response body.

  7. Copy and keep the access token and instance URL somewhere in the notepad from above response as we will be using them in calling our REST API.
2. Consume the Salesforce REST API using the above Access Token and Instance URL:
  1. Go to your Workspace in Postman.
  2. Enter following url: <instance_url from above>/services/apexrest/Case
  3. Go to Headers and pass the following 
    • Content-Type: application/json
    • Authorization: Bearer<SPACE><access_token from above>
  4. Choose POST method since we are going to create a Case record.
  5. Pass following parameters in the Body
    • Radio Group Options: raw
    • Enter the following JSON in the body:
      {
          "subject": "Test Rest API",
          "description": "This is a Rest API case creation testing."
      }
  6. Click on Send and you will be get the Case ID in the response body.

    If you like this blog content and find inciteful, please comment and let me know. 

Comments

  1. Very Informative Blog. Can you please write blog on Salesforce to Salesforce integration using Oauth 2.0 with names credentials?

    ReplyDelete
    Replies
    1. Thanks buddy and point taken, will write on Salesforce to Salesforce integration.

      Delete
    2. Here you go @Tushal
      https://inevitableyogendra.blogspot.com/2022/05/salesforce-to-salesforce-integration-using-named-credentials-and-authprovider.html

      Delete
  2. Great write up. Very informative!

    ReplyDelete
  3. Your article is very interesting and very helpful thanks for sharing.
    Rest api development company

    ReplyDelete
  4. Thanks for sharing this informative article on Salesforce REST API and Postman Testing. If you want to Salesforce Services for your project. Please visit us.

    ReplyDelete
  5. Postman is a popular tool for testing APIs that allows users to send requests, examine responses, and automate testing. To test an API with Postman you can create a new request, select the HTTP method (GET, POST, PUT, DELETE, etc.), enter the API endpoint URL, and configure any necessary headers or parameters. You can also write tests using JavaScript in the "Tests" tab to validate the response data, status codes, and other assertions. Postman also supports collections, enabling you to organise multiple requests and run them in sequence, which is particularly useful for testing workflows and automating repetitive tasks.

    ReplyDelete

Post a Comment