Developers

Understanding How to Call Salesforce Web Service Using Postman

By Shubham Lashkan

In this article, we will learn how to call Salesforce REST web service using Postman. Having knowledge about writing Salesforce REST web service is a prerequisite because this article doesn’t teach how to write one. It also assumes that you have a basic knowledge of making API calls using Postman. The article will cover the following topics:

  1. Writing Apex web service
  2. Creating a connected app in Salesforce
  3. Calling web service using Postman

Let’s dive in!

1. Writing Apex Web Service

As this article is more focused on calling web service using Postman, we will not spend time writing an Apex web service from scratch. I recommend you go through the last unit of this Trailhead module for a quick revision.

Also, create the CaseManager class shown in the last unit or copy the code snippet below:

@RestResource(urlMapping='/Cases/*')
global with sharing class CaseManager {
    @HttpGet
    global static Case getCaseById() {
        RestRequest request = RestContext.request;
        // grab the caseId from the end of the URL
        String caseId = request.requestURI.substring(
          request.requestURI.lastIndexOf('/')+1);
        Case result =  [SELECT CaseNumber,Subject,Status,Origin,Priority
                        FROM Case
                        WHERE Id = :caseId];
        return result;
    }
    @HttpPost
    global static ID createCase(String subject, String status,
        String origin, String priority) {
        Case thisCase = new Case(
            Subject=subject,
            Status=status,
            Origin=origin,
            Priority=priority);
        insert thisCase;
        return thisCase.Id;
    }
    @HttpDelete
    global static void deleteCase() {
        RestRequest request = RestContext.request;
        String caseId = request.requestURI.substring(
            request.requestURI.lastIndexOf('/')+1);
        Case thisCase = [SELECT Id FROM Case WHERE Id = :caseId];
        delete thisCase;
    }
    @HttpPut
    global static ID upsertCase(String subject, String status,
        String origin, String priority, String id) {
        Case thisCase = new Case(
                Id=id,
                Subject=subject,
                Status=status,
                Origin=origin,
                Priority=priority);
        // Match case by Id, if present.
        // Otherwise, create new case.
        upsert thisCase;
        // Return the case ID.
        return thisCase.Id;
    }
    @HttpPatch
    global static ID updateCaseFields() {
        RestRequest request = RestContext.request;
        String caseId = request.requestURI.substring(
            request.requestURI.lastIndexOf('/')+1);
        Case thisCase = [SELECT Id FROM Case WHERE Id = :caseId];
        // Deserialize the JSON string into name-value pairs
        Map<String, Object> params = (Map<String, Object>)JSON.deserializeUntyped(request.requestbody.tostring());
        // Iterate through each parameter field and value
        for(String fieldName : params.keySet()) {
            // Set the field and value on the Case sObject
            thisCase.put(fieldName, params.get(fieldName));
        }
        update thisCase;
        return thisCase.Id;
    }
}

2. Creating a Connected App in Salesforce

Before creating a connected app, let’s have a quick recap of what a connected app is.

A connected app is a framework that enables an external application to integrate with Salesforce using APIs and standard protocols, such as SAML, OAuth, and OpenID Connect. Connected apps use these protocols to authenticate, authorize, and provide single sign-on (SSO) for external apps.

To create a connected app that will allow Postman to access the Salesforce web service, follow the below steps.

  • Go to Setup and search for “App Manager”.
  • Click on the “New Connected App”, as shown in the image above.
  • Enter basic information like the connected app name, API name, and contact email.
  • Select the Enable OAuth Settings checkbox and paste this callback URL: https://www.getpostman.com/oauth2/callback. From Available OAuth Scopes, select “Full access (full)” and “Perform requests at any time (refresh_token, offline_access)” and put them in Selected OAuth Scopes. Also, uncheck the Require Proof Key for Code Exchange (PKCE) Extension for Supported Authorization Flows checkbox and click “Save”.
  • You will see a page like the image below. Click “Continue”.
  • On the Manage Connected Apps page, click “Manage Consumer Details”.
  • You will be asked to verify your identity and enter the verification code shared via email.
  • Copy the consumer key and consumer secret and store them in a safe place, as it will be required while making calls from the Postman.

We are done with creating a connected app. If you face any issue in the next step while making a call to Salesforce using Postman, visit this section to cross-check your connected app.

3. Calling Web Service Using Postman

Now, let’s start calling web service, but before that, you need to copy the org base URL. Here is the example format for reference: https://empathetic-wolf-9fmeiq-dev-ed.trailblaze.lightning.force.com/

Now open the Postman and create a POST request for the URL. Paste the org URL and append “services/oauth2/token”. It will look something like this: https://empathetic-wolf-9fmeiq-dev-ed.trailblaze.lightning.force.com/services/oauth2/token

In the Params section, enter five keys: grant_type, client_id, client_secret, username, and password. For their corresponding values, enter the term “password”, consumer key, consumer secret, Salesforce username, and password.

Here is the screenshot for reference:

Click “Send” and you will see the following response. You need to copy the “access_token” value from the response JSON.

We are able to generate access tokens. Now, let’s make a request to create a case record in Salesforce from Postman.

Create a POST request. In the URL, enter the base URL and append the Apex web service endpoint. As an example, it will look something like this: https://empathetic-wolf-9fmeiq-dev-ed.trailblaze.my.salesforce.com/services/apexrest/Cases/

In the Headers section, enter “Content-Type” as the key and “application/json” as the value. Add one more key “Authorization” with the value “Authorization: Bearer [PASTE THE ACCESS TOKEN WHICH WE COPIED ABOVE FROM POSTMAN]”.

In the Body section, enter the details related to the case. For reference, check the below image:

Click “Send”. Case records will be created successfully, and you will see the case ID in response. Copy the case ID because we will see how to make a GET request as well.

Here is the screenshot of the case created in Salesforce:

Now, let’s make a GET request to fetch the details of the case which we have created.

Create a GET request with the following URL: https://empathetic-wolf-9fmeiq-dev-ed.trailblaze.my.salesforce.com/services/apexrest/Cases/5005i00000SonNBAAZ

Replace the case ID with the ID that you copied. Also, make sure the Headers section remains the same and the Body is empty. Click “Send”, and you will see the case details in response.

Summary

This is how you call Salesforce web service using Postman. Since it is a rather extensive article, I would recommend you to go through it once again to fully digest the information. I hope my explanation helps clarify how to make a Salesforce web service call using Postman.

References

The Author

Shubham Lashkan

Shubham is a Salesforce Developer at Advanz101 with three years of experience. He's worked on various Sales Cloud projects and integrations with platforms like Facebook, Jira, and Twitter. He regularly shares his knowledge through articles and videos on his blog, YouTube channel, and Apex Hours.

Leave a Reply