If you are looking how to build Rest resource to access Salesforce data in your application, look no further. This blog demonstrates how to fetch account records from a Salesforce org via a Rest API. 

A connected app integrates an application with Salesforce using APIs. It provide tokens for use with Salesforce APIs and defines an entry point into the platform.

Create a Connected App

From Setup, enter Apps in the Quick Find box, then select Apps (under Build | Create). Under Connected Apps, click New.

  1. Specify basic information about your app in “Basic Information” section, including the app name, logo, and contact information.
  2. In “API (Enable OAuth Settings)section fill the details as shown below:
  • Enable OAuth Settings: Checked
  • Callback URL: https://www.getpostman.com/oauth2/callback

Note: This is typically the URL that a user’s browser is redirected to after successful authentication. In this blog we will be using  postman tool to test our rest resource. So, callback URL is set to this.

  • Selected OAuth Scopes: Access and manage your data(api)

Note: Select the OAuth scope as per your requirement. In this example, we just need to access the data via API so we selected “Access and manage your data(api)”.

  • Require Secret for Web Server Flow: Checked.

Once you’re done entering the information, click Save. Saving your app gives you Consumer Key and Consumer Secret values that the app uses to communicate with Salesforce.

Note: It takes 2-10 minutes for your changes to take effect on the server before using the connected app.

Below is the code snippet of the rest resource that returns account records:

@RestResource(urlMapping='/AccountsList')
global with sharing class getAccounts {
    @HttpGet
    global static List<Account> getListOfAccounts() {
        List<Account> lstAccounts = new List<Account>([ SELECT Id, Name, OwnerId FROM Account ]);
        return lstAccounts;
    }
}

To test the above rest resource using postman tool, follow the steps given below:

Link to download Postman Tool: https://www.getpostman.com/

  1. Under Authorization tab, select Type=”OAuth 2.0″.
  2. Click on “Get New Access Token” button and enter the values as shown below:

Note: Auth URL (Sandbox):  https://test.salesforce.com/services/oauth2/authorize

Note: Access Token URL(Sandbox): https://test.salesforce.com/services/oauth2/token

  • Client ID: Enter the consumer access key generated from the connected app.
  • Client Secret: Enter the consumer secret key generated from the connected app.
  • Scope: Can be left blank.
  • State: Can be left blank.
  • Client Authentication: Send client credential in body

  1. On click of “Request Token” button, you will be directed to Salesforce login Page. Enter the credentials of the Org in which you created Connected App. On successful login, access token will be generated and following pop up will appear:

  1. Click on “Use Token” button. It will automatically add the token to header.
  2. Select GET method and enter your “instance_url” followed by “/services/apexrest/{Rest Resource URL}” in Request URL. (Example Request URL: https://xxxx.salesforce.com/services/apexrest/AccountsList)

On click of Send button, you can see the response in the body section.

Happy Coding!