Wednesday, July 23, 2014

How to use Rest Api services in Salesforce

Ok so I am finding many post in which people want to learn Rest api or they already know the fundamental but are unable to create an example for some reason. So I will be showing you that how we can hit a REST GET service and retrieve data from salesforce.

We need the following things to do so:
  1. A rest api class definition in our saleforce org.  
  2. A rest callout tool.
  3. "Authorization" some kind of token so we can get in.
So first we need our Rest api class:


Now we need a rest callout tool so download a chrome extension called "Advance rest client" Link.

And now we need authorization so there are many ways for quick I will be showing the simplest way just to test it.
Open "https://login.salesforce.com/"> Login > Open Developer Console > Debug > Open Execute .. >
Type : system.debug('**'+userinfo.getSessionId()); > Execute.
Now search the debug log for "**" and copy the whole token eg: 00D90000000eCBG!AGHYT.BrtWEQdym9cDsUGcvPYEVKr4aEuNHSxqpMH_YQHS53.24x.HZxKDh1XGbNpjopkdylqvTv5tnFUOIUH.Epzxxxx

Now open the advance rest client tool and tupe this:
  • In the URL : https://<server>.salesforce.com/services/apexrest/showAcc?setquery=select Id,Name from Account limit 10
  • In the Header: Authorization: OAuth 00D90000000eCBG!AGHYT.BrtWEQdym9cDsUGcvPYEVKr4aEuNHSxqpMH_YQHS53.24x.HZxKDh1XGbNpjopkdylqvTv5tnFUOIUH.Epzxxxx
  • Click send.
That is it you will receive a response some thing like this:

Note: OAuth flow here: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_understanding_web_server_oauth_flow.htm


Hope this help you.

No comments:

Post a Comment