Skip to content

How To Use Postman In Salesforce

API Testing Blog

Salesforce API Testing with Postman: A Comprehensive Guide

Postman is a powerful tool for testing APIs, and it can be particularly helpful when working with Salesforce. This guide will delve into how to effectively use Postman for Salesforce API testing, providing practical examples and step-by-step instructions to get you started.

Understanding Salesforce REST APIs

Salesforce provides a robust set of REST APIs (Representational State Transfer) which allow you to interact with data and functionality within your Salesforce organization. These APIs are the foundation for integrating Salesforce with external systems and for automating tasks.

Setting Up Your Postman Environment

Before you start testing, ensure your Postman is configured for Salesforce API integration:

  1. Install the Salesforce OAuth 2.0 Authentication plugin: You can find this plugin within the Postman app, under the “Manage Plugins” section.
  2. Create a Connected App in Salesforce: In your Salesforce organization, navigate to Setup, then search for and select Apps. Click New Connected App.
  3. Configure Connected App Settings:
    • API Name: Provide a descriptive name for your app.
    • Contact Email: Enter your email.
    • Enable OAuth Settings:
      • Callback URL: Enter a valid URL (e.g., https://www.getpostman.com/oauth2/callback). Ensure this matches your Postman configuration.
      • Selected OAuth Scopes: Choose the specific API permissions your app requires. For basic access, select “Access and manage your data”.
  4. ** Save your Connected App:** Note down the Consumer Key and Consumer Secret generated for your app. These will be used for authentication in Postman.

Requesting Data from Salesforce: A Practical Example

With your workspace set up, let’s make our first API call using Postman. We’ll retrieve the information about a specific Contact record in Salesforce. Follow these steps:

  1. Open a New Postman Request: In Postman, click on the “New” button and choose “Request.”
  2. Choose the HTTP Method: For retrieving data, use the ‘GET’ method.
  3. Set the Request URL: Insert the Salesforce API endpoint for retrieving a contact. For example: https://yourinstance.salesforce.com/services/data/v53.0/sobjects/Contact/003D000000u88z7IAA (Replace ‘yourinstance’ and the ID with your values)
  4. Add Auth Credentials: In the Authorization tab, select the “OAuth 2.0” authentication type. Configure the following:
    • Grant Type: Use ‘Authorization Code’
    • Auth URL: https://login.salesforce.com/services/oauth2/authorize
    • Token URL: https://login.salesforce.com/services/oauth2/token
    • Client ID: Enter the Consumer Key from your Connected App.
    • Client Secret: Enter the Consumer Secret from your Connected App.
    • Scope: Select the appropriate OAuth scopes for your API request. For retrieving data, ‘Access and manage your data’ should be sufficient.
  5. Run the Request: Click ‘Send’. If the request is successful, you will see the Contact record details in the response body.

Sending Data to Salesforce - Create a New Contact

Now, let’s explore how to create a new contact using the Salesforce API and Postman. This demonstrates the ‘POST’ request method.

  1. Open a New Postman Request: Create a new request in Postman.
  2. Choose the HTTP Method: Select ‘POST’ for creating new data.
  3. Set the Request URL: Enter the endpoint for creating a Contact: https://yourinstance.salesforce.com/services/data/v53.0/sobjects/Contact/
  4. Set the Request Body:
    • Content-Type: Set the header to “application/json”.
    • JSON body: Enter the contact data as a JSON object. For example:
{
"FirstName": "Jane",
"LastName": "Doe",
"Email": "jane.doe@example.com"
}
  1. Authentication: Configure OAuth 2.0 authentication, following the instructions described in the previous example.
  2. Run the Request: Click ‘Send’. If the request is successful, Salesforce will create the new contact and return a confirmation message.

Working with Salesforce SOQL Queries in Postman

Salesforce Object Query Language (SOQL) is used to retrieve data from Salesforce. You can incorporate SOQL queries as part of your Postman test requests.

  1. Construct a SOQL Query: For example: SELECT Id, FirstName, LastName FROM Contact.
  2. Build the Request URL: Embed your SOQL query within the request URL structure: https://yourinstance.salesforce.com/services/data/v53.0/query/?q={SOQL_QUERY}
  3. Run the Request: Ensure you’ve configured OAuth 2.0 authentication. Click ‘Send’ to execute your SOQL query.

Testing Salesforce Web Services Using Postman

Salesforce provides a variety of Web Services which are methods that perform specific actions within your organization. You can test these services using Postman as well.

  1. Identify the Web Service Endpoint: You’ll find the endpoint details for specific web services within the Salesforce documentation.
  2. Construct the Request: Ensure the correct HTTP method (e.g., GET for retrieving information, POST for updating an object) and set any required parameters.
  3. Call the Web Service: Execute your request, confirming the authentication setup for your Connected App.

Building Robust Salesforce API Tests with Postman Collections

Postman Collections offer a great way to manage and organize your API tests for Salesforce. Here’s how to benefit from these features:

  1. Create a Postman Collection: Name your collection and define it as the container for your Salesforce API tests.
  2. Add Requests: Organize your individual requests within the collection (e.g., GET for a Contact, POST for a new Opportunity).
  3. Include Tests: Add scripts to each request that help verify responses and validate the API functionality you are testing.
    • Validation Example:
pm.test("Status Code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Response Body contains ID", function () {
pm.expect(pm.response.text()).to.include("id");
});
pm.test("Validation for Success", function () {
pm.expect(pm.response.json().success).to.be.true;
});
  1. Run Tests and Analyze Results: Execute your entire collection to run all your test cases against Salesforce APIs and analyze the results within Postman.

Additional Postman Tips for Salesforce API Testing

  • Environment Variables: Use Postman Environments to manage different Salesforce instances (Sandbox, Production, etc.) and API versions easily.
  • Test Data Management: Create reusable test data within Postman for consistency in your API tests.
  • Error Handling: Implement error handling in your test scripts to handle unexpected responses and ensure your tests are robust.
  • Documentation: Integrate the Postman documentation feature to create comprehensive documentation for your API tests.

By following this comprehensive guide, you’ll be equipped to leverage Postman’s capabilities for effective and efficient testing of Salesforce APIs. Remember that these are the essential building blocks; explore Postman further to unlock even more powerful testing strategies for your Salesforce integrations.

API Testing Blog