Wednesday, August 16, 2017

Creating Apex Rest Services and Testing it with POSTMAN

In this post, I am going to explain how to validate Apex Rest Service created in SFDC using POSTMAN (google chrome plugin).

First we will create a very basic Apex Rest Service.


After creating this class, now we can access this API by using below endpoint URL:

https://xxxx.salesforce.com/services/apexrest/RestAPIDemo
where xxxx is your domain URL or say instance URL like na15, cs30 etc.

Here we have created Rest API which accepts GET & POST request.Whenever we will perform GET request, API will return Account records based on search string passed as parameter in endpoint URL.

Now I will explain how to test this API Service by using POSTMAN.

In order to invoke Apex REST API, we need to have access token. So first we will use Oauth 2.0 to get access token from SFDC in order to perform API calls.

Steps to get access token from SFDC:

  • Create a connected App in SFDC.
  • Perform POST request from POSTMAN to below mentioned endPoint URL:

https://login.salesforce.com/services/oauth2/token?grant_type=password&client_id=CLIENTID&client_secret=CLIENTSECRET&
username=SFDCUSERNAME&password=SFDCPASSWORD_SECURITY_TOKEN

Where 
       CLIENTID = consumer key from created connected app
       CLIENTSECRET= consumer secret from created connected app
       SFDCUSERNAME = SFDC username
       SFDCPASSWORD_SECURITY_TOKEN = password + security_token

While performing POST request, specify header in which key will be "Content-Type" and value will be "application/x-www-form-urlencoded"



You will recieve JSON response in which access_token will be specified. Copy and save it.

Note:
If you have enabled my domain then you can use domain URL as base URL in endpoint URL. If you are not using domain, then in JSON response you will get instance_URL which you can use base URL whenever you call APEX REST API's.


How to perform GET request from POSTMAN to fetch data from SFDC
  • First create Endpoint URL(GET request).
          https:// BASE_URL/services/apexrest/RestAPIDemo?searchString=test
          where 
          BASE_URL is instance URL returned in JSON while requesting Access token.
          RestAPIDemo is REST Resource we specify while creating class.
          searchString : parameter passed to REST API which will return matched accounts
  • Specify Below headers
          [{"key":"Content-Type","value":"application/json"}]
          [{"key":"Authorization","value":"Authorization: Bearer Access_token"}]
  • Perform Get Request from POSTMAN


How to perform POST request from POSTMAN to create new record

  • Create a JSON through which you send account information.
       {
  "accName":"SKtest By POSTMAN",
   "aType":"New Customer",
   "aIndustry":"IT"
        }   
  • Specify endpoint URL
       https://xxxx.salesforce.com/services/apexrest/RestAPIDemo
       where xxxx is domain of your org
  • Specify headers
          [{"key":"Content-Type","value":"application/json"}]
          [{"key":"Authorization","value":"Authorization: Bearer Access_token"}]
  • Send POST request through POSTMAN.

Note:

If your POST method contains parameter, then you have to pass JSON in different ways. For example POST method is:

@HttpPost   
    global static String createNewAccount(accountWrapper accDetails) {
        string returnString='';
        try{
            Account acc=new Account();
            acc.Name= accDetails.accName;
            acc.Type = accDetails.aType;
            acc.Industry =accDetails.aIndustry;
            insert acc;
            returnString = 'Account created successfully with record Id:'+acc.id; 
        }catch(exception ex){
            returnString = 'Request got failed: Error details-'+ex.getmessage();
        }
         return returnString;
    }

In this scenario, JSON which needs to sent should be in below format:

{
"accDetails":{
"accName":"SKtest By POSTMAN2",
"aType":"New Customer",
"aIndustry":"IT"
}
}

Hope this will help!!!!

More Blogs>>: 
USING DATABASE.UPSERT WITH EXTERNAL ID   
DYNAMIC APEX IN SALESFORCE   
SOQL INJECTION IN SOQL   
CUSTOM METADATA AND CUSTOM SETTINGS IMPLEMENTATION TRICKS   
SMART TABLE USING ANGULARJS IN VISUALFORCE PAGE   
REST API TUTORIAL FOR SALESFORCE   
VISUALFORCE COMPONENT FOR RECORD STATUS BAR   
DYNAMICALLY CREATING AND DESTROYING LIGHTNING COMPONENTS    
RAISING AND HANDLING CUSTOM EVENTS IN sALESFORCE lIGHTNING    
WHY TO USE DESIGN RESOURCE AND HOW TO ADD DYNAMIC OPTION TO DATASOURCE    
PASSING INNER WRAPPER CLASS TO LIGHTNING COMPONENT    
LIGHTNING COMPONENT FOR RECORDTYPE SELECTION FOR ANY SOBJECT    
FETCHING FILE FROM EXTERNAL/PUBLIC URL AND STORING IT IN SALESFORCE   

5 comments:

  1. A nice one .. simple example thank you

    ReplyDelete
  2. Thanks for the post .It helped :)

    ReplyDelete
  3. I am hoping the same best work from you in the future as well thanks for sharing.
    SalesForce Online Training

    ReplyDelete
  4. "Great blog created by you. I read your blog, its best and useful information. You have done a great work. Super blogging and keep it up.php jobs in hyderabad.
    "

    ReplyDelete



  5. I have read your blog and I gathered some needful information from your blog. Keep update your blog. Awaiting for your next update.
    Angularjs course in chennai

    ReplyDelete