Thursday 10 December 2015

How to test a Restful web services in Salesforce by using Advanced Rest Client chrome app

      The below article will help you to create a basic Rest API class to insert an account record in salesforce by creating a method with @HttpPost annotation. Also this guide will help you to test the functionality by using using Advanced Rest Client chrome app. 

Step 1: 

Create a REST API class with @HttpPost annotation method. 

  1. @RestResource(urlMapping='/AccountMapping/*')
  2. global with sharing class RESTAccountController {

  3. @HttpPost  
  4.   global static String createNewAccount(String accountName, String accountNumber) {
  5.      System.debug('Account Name: '+accountName);
  6.      System.debug('Account Number: '+accountNumber);
  7.      try
  8.      {
  9.          Account acc = new Account();
  10.          acc.Name = accountName;
  11.          acc.AccountNumber = accountNumber;
  12.          insert acc;
  13.          return 'Account has been inserted succesfully: '+acc.Id;
  14.      }
  15.      catch(DMLException de)
  16.      {
  17.         return de.getDmlMessage(0);
  18.      }
  19.                
  20.      return null;
  21.   }
  22. }

Note:
The @RestResource annotation is used at the class level and enables you to expose an Apex class as a REST resource.

1. The URL mapping is relative to https://instance.salesforce.com/services/apexrest/
2. URL mapping is case-sensitive

Read this documentation well before setting up urlMapping

Step 2: 

Install Advanced Rest Client from the below link,


Note: You can also use Postman tool, if you like.

Step 3:

Create a "Connected App" by referring this documentation.

After you created the "Connected App", Go to Create --> Apps --> Connected App --> Select your connected app which you created.

Your connected app looks like below,


After you created a connected app ensure the app permission is given at profile level. Also Copy the "Consumer Key" and "Consumer Secret" code for authentication in Advance Rest Client.

Step 4:

Open Advanced REST Client app in Chrome and follow the below steps.

Steps to obtain an access token:

URL:
https://login.salesforce.com/services/oauth2/token

METHOD:
POST

BODY:
grant_type=password&client_id=3MVG9Ad93Bn17huFzcp2LseGauyeShr6X9TmixGMGQ5MuS2hoPxlEHAvyVQzZVzdhClK6WUFdMY8FzXqNxc4&client_secret=2536602822064617993&username=testuser@force.com&password=psmu97VQzZVzdhClK6WUFdMY8FzXqNxc

Client_id and Client_secret: Copied from connected app.
Username: Your salesforce user name.
Password: Your salesforce account password with security token.
(If you are in White list IP ranges, then you don’t need to add a security token with password).

CONTENT TYPE:
application/x-www-form-urlencoded

Sending Request:


Access Token:

After you send a request your will get an access token. This will looks like below,



Copy the access token from the response.

CALLING REST SERVICE:

REST URL:
https://na15.salesforce.com/services/apexrest/AccountMapping

Adding access token:

Authorization  OAuth "Your access token"

JSON Body:

{"accountName":"Arunkumar",
"accountNumber":"128393"}

Response From Sever:


Thanks for reading this post...!

5 comments:

  1. thank you very much! your tutorial helped me a lot after lot of hours spent on tries

    ReplyDelete
  2. why is the Callback URL "www.facebook.com"???????

    ReplyDelete
  3. I have a Task which is, update the information(which i already in Salesforce Account object) from external application using Rest API
    Problem: What i am facing is, can able to retrieve only 10 record per apex callout but when i am testing the API using ARC or Postman where i am getting actual records arround exactly 800 record.

    Solution as Per my understand from this blog, can we update SF account object records using ARC or Postman

    ReplyDelete
  4. Already you people know about Oracle Huge growth. PLSQL is also one of the parts of oracle already. PLSQL students are moving to Advanced PL SQL now because they know about the importance and value of this course. Wanna be part of this PLSQL training in Chennai for more enquiries call 7502633633.Oracle Training with Placement | Infycle Technologies

    ReplyDelete
  5. Thanks for sharing this informative article on How to test a Restful web services in Salesforce by using Advanced Rest Client chrome app. If you want to Salesforce Services for your project. Please visit us.

    ReplyDelete

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...