Wednesday, November 21, 2018

Salesforce rest api callouts

Today we will try to cover the topic REST API CALLOUTS IN SALESFORCE.

First let's understand what is api?

In simple term API is nothing but communication medium between various software's components.

Let's understand with a simple example,

Let say I am looking to buy a car, One of my friend is having contact with the car selling company.I will submit my request to my friend to inquire about the details of car from company, my friend will go will bring all details about car and will give it to me.

Here I submitted request to my friend.My friend act as a medium to carry request to company
and comes back with response, API is similar to my friend who takes request and bring response.

Now,What is rest api?

Rest api stands for representational state transfer.Through Rest API Websites,servers can share data.Most of the time the response is in JSON(Javascript object notation).Using REST API you can connect your system with some other system and perform operations like:

GET  // To get some information
DELETE // To delete some information
POST // create a resource or post data to server
PUT //create or replace the resource sent in  request body

Salesforce rest api callouts


Here in this example we will see how to get data using callouts, parsed it and display it through visualforce page.


<apex:page controller="RestApiController">
<apex:form >
<apex:pageBlock >
  <apex:pageBlockTable value="{!lst}" var="a">
  <apex:column value="{!a.id1}"/>





public class RestApiController{

public RestApiController()
Http http=new Http();

HttpRequest req=new HttpRequest();


req.setMethod('GET');     //  Get data

HttpResponse res=http.send(req);

string response1=res.getBody();

JSONParser j=JSON.createParser(response1);  // Parsing response

list<string> ids=new list<string>();

while(j.nextToken() != null){

  if((j.getCurrentToken() == JSONToken.FIELD_NAME)  &&  (j.getText()=='id')   )


lst=new list<wrapData>();
for(integer i=0;i<ids.size();i++)
obj1=new wrapData();



public wrapData obj1{get;set;}
public list<wrapData> lst{get;set;}
public class wrapData
public string id1{get;set;}




 TAGS:Salesforce rest api,Salesforce rest api callouts,what is rest api,rest api,rest api example,rest api salesforce.


No comments:

Post a Comment