Wednesday, January 9, 2019

Salesforce rest api integration to delete a record

Today we will trying to cover the topic Salesforce rest api integration by taking example of two Salesforce systems.

First let's see when to use rest api as we have lot of other Salesforce api.

Rest api is a simple and power webservice based on restfull principles and it use rest resource and HTTP methods to expose functionalities.It supports both XML and JSON .Rest api has light weight request and response frame work and it is good for mobile and web apps.
Let say I have two system as SYSTEM A and another system as SYSTEM B.

What we will doing today is we will try to delete a record in SYSTEM B using rest api from SYSTEM A.

REST API WEBSERVICE FROM SYSTEM B:

@RestResource(urlMapping='/getAccountOnExternalIdtodeleterecords/*')
   global with sharing class getAccounttodeleterecords {
     @Httpdelete
       global static string deleteAccount(){
     account obj=new account();
        RestRequest req = RestContext.request;
        RestResponse res = Restcontext.response;
         string accId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
      obj=[Select id , name from Account where id=:accId];
       delete obj;
        return 'success';
      }

   }

CONNECTED APP IMAGE FROM SYSTEM B:

Salesforce api

APEX CONTROLLER FROM SYSTEM A :

public class restApiClassSalesforceorgtodeleterecords{


private string cKey='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
private string cSecret='XXXXXXXXXXXXXXXXXXXXX';
private string uName='XXXXXXXXXXXXXXXXXXX';
private string passwd='Password+SecurityToken';
public static list<resultWrapper> listWrap{get;set;}
public restApiClassSalesforceorgtodeleterecords()
{
listWrap=new list<resultWrapper>();

}



public class responseWrapper{

public string id;
public string access_token;
public string instance_url;

}

public string getRequestToken()
{
string reqBody='grant_type=password&client_id='+cKey+'&client_secret='+cSecret+'&username='+uName+'&password='+passwd;                                      

Http h=new Http();
HttpRequest req=new HttpRequest();
req.setBody(reqBody);
req.setMethod('POST');
req.setEndpoint('https://login.salesforce.com/services/oauth2/token');

HttpResponse hresp=h.send(req);
responseWrapper wResp=(responseWrapper) JSON.deserialize(hresp.getBody(),responseWrapper.class);
system.debug('Instance url'+wResp.instance_url);
system.debug('session id'+wResp.access_token);
return wResp.access_token;


}

public void getConList()
{

list<account> accList1=new list<account>();
String accToken;
string responseBody;
string accIdToDelete='0017F00001XgQFy'; //Account Id from system B.
string endPoint='https://ap5.salesforce.com/services/apexrest/getAccountOnExternalIdtodeleterecords/' +accIdToDelete;
restApiClassSalesforceorgtodeleterecords obj=new restApiClassSalesforceorgtodeleterecords();
accToken=obj.getRequestToken();
system.debug('access token'+ accToken);
if(accToken!='')
{
Http h1=new Http();
HttpRequest req1=new HttpRequest();
req1.setHeader('Authorization','Bearer '+accToken);
req1.setHeader('Content-Type','application/json');
//req1.setHeader('Accept','application/json');
req1.setMethod('DELETE');
req1.setEndpoint(endPoint);
HttpResponse hresp1=h1.send(req1);

}



}

public class resultWrapper{

public string id{get;set;}
public string name{get;set;}


}


}

VISUALFORCE PAGE FROM SYSTEM A TO DISPLAY DATA:

<apex:page controller="restApiClassSalesforceorgtodeleterecords">
<apex:form >
<apex:pageBlock >

 <apex:pageblockButtons >
 <apex:commandButton value="Delete records" action="{!getConList}"/>

 </apex:pageblockButtons>

 </apex:pageBlock>
 </apex:form>
</apex:page>

REMOTE SITE STORED IN SYSTEM A:

Salesforce api
In this way we completed Salesforce rest api to delete a record from one system using rest api from another system.

Please comment below if you have any doubts.

YOU CAN VISIT OTHER POST FROM OUR BLOG AS WELL,

Salesforce rest api integration to fetch single record (Click here)

Salesforce rest api integration to fetch list of records (Click here)

Salesforce rest api integration to create a record (Click here)

Salesforce rest api integration to update a record (Click here)


Salesforce soap integration (Click here)

Salesforce rest api callouts (Click here)

**************************************************************************************************************
TAGS: Salesforce rest api integration between Salesforce and Salesforce,Salesforce api,Salesforce rest api,Salesforce rest api integration to delete a record.
**************************************************************************************************************

Salesforce rest api integration to update a record.

Today we will trying to cover the topic Salesforce rest api integration by taking example of two Salesforce systems.

First let's see when to use rest api as we have lot of other Salesforce api.

Rest api is a simple and power webservice based on restfull principles and it use rest resource and HTTP methods to expose functionalities.It supports both XML and JSON .Rest api has light weight request and response frame work and it is good for mobile and web apps.
Let say I have two system as SYSTEM A and another system as SYSTEM B.

What we will doing today is we will try to update a record in SYSTEM B using rest api from SYSTEM A.

REST API WEBSERVICE FROM SYSTEM B:

@RestResource(urlMapping='/updaterecord/*')
   global with sharing class updateAccount {
     @httpput
      global Static string fetchAccount(string Name,string NewAccountNumber,string OldAccountNumber){
      Account obj=new account();
      obj=[select id,name,AccountNumber from account where AccountNumber=:OldAccountNumber]; // fetching account by old account number.
      obj.AccountNumber=NewAccountNumber; // updating account with new account number.
      update obj;
        return 'Success';
      }
   }

CONNECTED APP IMAGE FROM SYSTEM B:

Salesforce api

Note down the Consumer key,Consumer secret(We need to provide it SYSTEM A for authentication purpose along with username,password,security token of user which SYSTEM A will be using for authentication).

APEX CONTROLLER FROM SYSTEM A :

public class restApiToUpdateRecords{


private string cKey='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
private string cSecret='XXXXXXXXXXXXXXXXXXXXX';
private string uName='XXXXXXXXXXXXXXXXXXX';
private string passwd='Password+SecurityToken';
public static list<resultWrapper> listWrap{get;set;}

public restApiToUpdateRecords()
{
listWrap=new list<resultWrapper>();

}



public class responseWrapper{

public string id;
public string access_token;
public string instance_url;

}

public string getRequestToken()
{
string reqBody='grant_type=password&client_id='+cKey+'&client_secret='+cSecret+'&username='+uName+'&password='+passwd;                                   

Http h=new Http();
HttpRequest req=new HttpRequest();
req.setBody(reqBody);
req.setMethod('POST');
req.setEndpoint('https://login.salesforce.com/services/oauth2/token');

HttpResponse hresp=h.send(req);
responseWrapper wResp=(responseWrapper) JSON.deserialize(hresp.getBody(),responseWrapper.class);
system.debug('Instance url'+wResp.instance_url);
system.debug('session id'+wResp.access_token);
return wResp.access_token;


}

public void getConList()
{

list<account> accList1=new list<account>();
String accToken;
string responseBody;
string Jsonstring='{"Name":"Acc test 1","NewAccountNumber":"456","OldAccountNumber":"123"}';
string accIdToupdate='0017F00001Xf1RO';
string endPoint='https://ap5.salesforce.com/services/apexrest/updaterecord/';
restApiToUpdateRecords obj=new restApiToUpdateRecords();
accToken=obj.getRequestToken();
system.debug('access token'+ accToken);
if(accToken!='')
{
Http h1=new Http();
HttpRequest req1=new HttpRequest();
req1.setHeader('Authorization','Bearer '+accToken);
req1.setHeader('Content-Type','application/json');
req1.setBody(Jsonstring);
req1.setMethod('PUT');
req1.setEndpoint(endPoint);
HttpResponse hresp1=h1.send(req1);


}



}

public class resultWrapper{

public string id{get;set;}
public string name{get;set;}


}


}


VISUALFORCE PAGE FROM SYSTEM A TO DISPLAY DATA:

<apex:page controller="restApiToUpdateRecords">
<apex:form >
<apex:pageBlock >

 <apex:pageblockButtons >
 <apex:commandButton value="Update record" action="{!getConList}"/>

 </apex:pageblockButtons>

 </apex:pageBlock>
 </apex:form>
</apex:page>

REMOTE SITE STORED IN SYSTEM A:

Salesforce api
In this way we completed Salesforce rest api to update a record from one system to another by taking two Salesforce systems.

Please comment below if you have any doubts.

YOU CAN VISIT OTHER POST FROM OUR BLOG AS WELL,

Salesforce rest api integration to fetch single record (Click here)

Salesforce rest api integration to fetch list of records (Click here)

Salesforce rest api integration to create a record (Click here)


Salesforce rest api integration to delete a record (Click here)

Salesforce soap integration (Click here)

Salesforce rest api callouts (Click here)

**************************************************************************************************************
TAGS: Salesforce rest api integration between Salesforce and Salesforce,Salesforce api,Salesforce rest api,Salesforce rest api integration to update a record.
**************************************************************************************************************

Salesforce rest api integration to create a record

Today we will trying to cover the topic Salesforce rest api integration by taking example of two Salesforce systems.

First let's see when to use rest api as we have lot of other Salesforce api.

Rest api is a simple and power webservice based on restfull principles and it use rest resource and HTTP methods to expose functionalities.It supports both XML and JSON .Rest api has light weight request and response frame work and it is good for mobile and web apps.



Let say I have two system as SYSTEM A and another system as SYSTEM B.

What we will doing today is we will try to create a record in SYSTEM B using rest api from SYSTEM A.

REST API WEBSERVICE FROM SYSTEM B:

@RestResource(urlMapping='/getcreateAccount/*')
   global with sharing class getAccount {
     @HttpPost
      global Static string fetchAccount(string Name){
      Account obj=new account();
      obj.name=Name;
      Insert obj;
        
        return 'Success';
      }
   }

CONNECTED APP IMAGE FROM SYSTEM B:

Salesforce api

Note down the Consumer key,Consumer secret(We need to provide it SYSTEM A for authentication purpose along with username,password,security token of user which SYSTEM A will be using for authentication).

APEX CONTROLLER FROM SYSTEM A :

public class restApiClassSalesforceorg1{


private string cKey='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
private string cSecret='XXXXXXXXXXXXXXXXXXXXX';
private string uName='XXXXXXXXXXXXXXXXXXX';
private string passwd='Password+SecurityToken';
public static list<resultWrapper> listWrap{get;set;}


public restApiClassSalesforceorg1()
{
listWrap=new list<resultWrapper>();

}



public class responseWrapper{

public string id;
public string access_token;
public string instance_url;

}

public string getRequestToken()
{
string reqBody='grant_type=password&client_id='+cKey+'&client_secret='+cSecret+'&username='+uName+'&password='+passwd;                                      

Http h=new Http();
HttpRequest req=new HttpRequest();
req.setBody(reqBody);
req.setMethod('POST');
req.setEndpoint('https://login.salesforce.com/services/oauth2/token');

HttpResponse hresp=h.send(req);
responseWrapper wResp=(responseWrapper) JSON.deserialize(hresp.getBody(),responseWrapper.class);
system.debug('Instance url'+wResp.instance_url);
system.debug('session id'+wResp.access_token);
return wResp.access_token;


}

public void getConList()
{

list<account> accList1=new list<account>();
String accToken;
string responseBody;
string endPoint='https://ap5.salesforce.com/services/apexrest/getcreateAccount';
restApiClassSalesforceorg1 obj=new restApiClassSalesforceorg1();
accToken=obj.getRequestToken();
system.debug('access token'+ accToken);
if(accToken!='')
{
string Jsonstring='{"Name":"Acc testname 1"}';
Http h1=new Http();
HttpRequest req1=new HttpRequest();
req1.setHeader('Authorization','Bearer '+accToken);
req1.setHeader('Content-Type','application/json');
//req1.setHeader('accept','application/json');
req1.setMethod('POST');
req1.setBody(Jsonstring);
req1.setEndpoint(endPoint);
HttpResponse hresp1=h1.send(req1);
system.debug('hresp1'+ hresp1);


}


}


public class resultWrapper{

public string id{get;set;}
public string name{get;set;}


}


}

VISUALFORCE PAGE FROM SYSTEM A TO DISPLAY DATA:

<apex:page controller="restApiClassSalesforceorg1">
<apex:form >
<apex:pageBlock >
 <apex:pageblockButtons >
 <apex:commandButton value="Create a record" action="{!getConList}"/>
 </apex:pageblockButtons>
 </apex:pageBlock>
 </apex:form>
</apex:page>

REMOTE SITE STORED IN SYSTEM A:

Salesforce api
In this way we completed Salesforce rest api to create a record from one system to another by taking two Salesforce systems.

Please comment below if you have any doubts.

YOU CAN VISIT OTHER POST FROM OUR BLOG AS WELL,

Salesforce rest api integration to fetch single record (Click here)

Salesforce rest api integration to fetch list of records (Click here)


Salesforce rest api integration to update a record (Click here)

Salesforce rest api integration to delete a record (Click here)

Salesforce soap integration (Click here)

Salesforce rest api callouts (Click here)

**************************************************************************************************************
TAGS: Salesforce rest api integration between Salesforce and Salesforce,Salesforce api,Salesforce rest api,Salesforce rest api integration to create a record.
**************************************************************************************************************

Sunday, January 6, 2019

Salesforce rest api integration to fetch list of records

Today we will trying to cover the topic Salesforce rest api integration by taking example of two Salesforce systems.

First let's see when to use rest api as we have lot of other Salesforce api.

Rest api is a simple and power webservice based on restfull principles and it use rest resource and HTTP methods to expose functionalities.It supports both XML and JSON .Rest api has light weight request and response frame work and it is good for mobile and web apps.


Let say I have two system as SYSTEM A and another system as SYSTEM B.

What we will doing today is we will try to fetch list of account from SYSTEM B and show that using Visualforce page in SYSTEM A.

REST API WEBSERVICE FROM SYSTEM B:

@RestResource(urlMapping='/getAccountOnExternalIdtofetchList/*')
   global with sharing class getAccounttofetchList {
     @Httpget
       global static list<Account> fetchAccount(){
      list<account> accList=new list<account>();
        RestRequest req = RestContext.request;
        RestResponse res = Restcontext.response;
     
      accList=[Select id , name from Account limit 10];
     
        return accList;
      }

   }

CONNECTED APP IMAGE FROM SYSTEM B:


Salesforce api


Note down the Consumer key,Consumer secret(We need to provide it SYSTEM A for authentication purpose along with username,password,security token of user which SYSTEM A will be using for authentication).

APEX CONTROLLER FROM SYSTEM A :

public class restApiClassSalesforceorgTofetchlist{


private string cKey='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
private string cSecret='XXXXXXXXXXXXXXXXXXXXX';
private string uName='XXXXXXXXXXXXXXXXXXX';
private string passwd='Password+SecurityToken';
public static list<resultWrapper> listWrap{get;set;}


public restApiClassSalesforceorgTofetchlist()
{
listWrap=new list<resultWrapper>();

}



public class responseWrapper{

public string id;
public string access_token;
public string instance_url;

}

public string getRequestToken()
{
string reqBody='grant_type=password&client_id='+cKey+'&client_secret='+cSecret+'&username='+uName+'&password='+passwd;                                   

Http h=new Http();
HttpRequest req=new HttpRequest();
req.setBody(reqBody);
req.setMethod('POST');
req.setEndpoint('https://login.salesforce.com/services/oauth2/token');

HttpResponse hresp=h.send(req);
responseWrapper wResp=(responseWrapper) JSON.deserialize(hresp.getBody(),responseWrapper.class);
system.debug('Instance url'+wResp.instance_url);
system.debug('session id'+wResp.access_token);
return wResp.access_token;


}

public void getConList()
{

list<account> accList1=new list<account>();
String accToken;
string responseBody;
string endPoint='https://ap5.salesforce.com/services/apexrest/getAccountOnExternalIdtofetchList/';
restApiClassSalesforceorg1 obj=new restApiClassSalesforceorg1();
accToken=obj.getRequestToken();
system.debug('access token'+ accToken);
if(accToken!='')
{
Http h1=new Http();
HttpRequest req1=new HttpRequest();
req1.setHeader('Authorization','Bearer '+accToken);
req1.setHeader('Content-Type','application/json');
//req1.setHeader('Accept','application/json');
req1.setMethod('GET');
req1.setEndpoint(endPoint);
HttpResponse hresp1=h1.send(req1);
listWrap=(list<resultWrapper>) JSON.deserialize(hresp1.getBody(),list<resultWrapper>.class);

}

}

public class resultWrapper{

public string id{get;set;}
public string name{get;set;}


}


}
VISUALFORCE PAGE FROM SYSTEM A TO DISPLAY DATA:

<apex:page controller="restApiClassSalesforceorgTofetchlist">
<apex:form >
<apex:pageBlock >

 <apex:pageblockButtons >
 <apex:commandButton value="TEST" action="{!getConList}"/>

 </apex:pageblockButtons>
  <apex:pageblocktable value="{!listWrap}" var="a" >

<apex:column value="{!a.name}"/>
</apex:pageBlockTable>

 </apex:pageBlock>
 </apex:form>
</apex:page>

REMOTE SITE STORED IN SYSTEM A:

Salesforce api
RESULT IMAGE AFTER PREVIEWING VISUALFORCE PAGE:

Salesforce rest api
In this way we completed Salesforce rest api by taking two Salesforce systems.

Please comment below if you have any doubts.

YOU CAN VISIT OTHER POST FROM OUR BLOG AS WELL,

Salesforce rest api integration to fetch single record (Click here)

Salesforce rest api integration to create a record (Click here)

Salesforce rest api integration to update a record (Click here)

Salesforce rest api integration to delete a record (Click here)

Salesforce soap integration (Click here)

Salesforce rest api callouts (Click here)

**************************************************************************************************************
TAGS: Salesforce rest api integration between Salesforce and Salesforce,Salesforce api,Salesforce rest api,Salesforce rest api integration to fetch list of records.
**************************************************************************************************************

Salesforce rest api integration to fetch single record

Following are the most commonly used Salesforce api:

1)Rest api
2)Soap api
3)Bulk api
4)Streaming api

Among the above Salesforce api, today we will trying to cover the topic Salesforce rest api integration by taking example of two Salesforce systems.

First let's see when to use rest api as we have lot of other Salesforce api.

Rest api is a simple and power webservice based on restfull principles and it use rest resource and HTTP methods to expose functionalities.It supports both XML and JSON .Rest api has light weight request and response frame work and it is good for mobile and web apps.


Let say I have two system as SYSTEM A and another system as SYSTEM B.

What we will doing today is we will try to fetch account from SYSTEM B and show that using Visualforce page in SYSTEM A.

Let's start,

ACTIVITIES WE NEED TO DO IN SYSTEM B.

1)Create a connected app.
2)Rest api webservice to fetch data based on requirement of SYSTEM A and send it back to SYSTEM A.

1) WHAT IS CONNECTED APP IN SALESFORCE?

For external application that needs to authenticate with Salesforce we need to create a connected app
so as to inform Salesforce about the new authentication entry point.Connected app use standard OAuth 2.0 protocol to authenticate.


Salesforce api


Note down the Consumer key,Consumer secret(We need to provide it SYSTEM A for authentication purpose along with username,password,security token of user which SYSTEM A will be using for authentication).

2) REST API WEBSERVICE:

@RestResource(urlMapping='/getAccountOnExternalIdtofetchsinglerecord/*')
   global with sharing class getAccounttoSingleRecord {
 
        @Httpget
      global static Account fetchAccount(){
        Account obj=new Account();
        RestRequest req = RestContext.request;

        RestResponse res = Restcontext.response;

        string accId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);

      obj=[Select id , name from Account where id=:accId];

   

        return obj;

      }

   }

ACTIVITIES WE NEED TO DO IN SYSTEM A.

1)Create a apex controller.
2)Create a visualforce page.
3)Create a remote site setting for URL of System B.

1) CREATE A APEX CONTROLLER:
public class restApiTofetchSingleRecord{


private string cKey='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
private string cSecret='XXXXXXXXXXXXXXXXXXXXX';
private string uName='XXXXXXXXXXXXXXXXXXX';
private string passwd='Password+SecurityToken';
public static list<resultWrapper> listWrap{get;set;}


public restApiTofetchSingleRecord()
{
listWrap=new list<resultWrapper>();

}



public class responseWrapper{

public string id;
public string access_token;
public string instance_url;

}

public string getRequestToken()
{
string reqBody='grant_type=password&client_id='+cKey+'&client_secret='+cSecret+'&username='+uName+'&password='+passwd;                                   

Http h=new Http();
HttpRequest req=new HttpRequest();
req.setBody(reqBody);
req.setMethod('POST');
req.setEndpoint('https://login.salesforce.com/services/oauth2/token');

HttpResponse hresp=h.send(req);
responseWrapper wResp=(responseWrapper) JSON.deserialize(hresp.getBody(),responseWrapper.class);
system.debug('Instance url'+wResp.instance_url);
system.debug('session id'+wResp.access_token);
return wResp.access_token;


}

public void getConList()
{

list<account> accList1=new list<account>();
String accToken;
string responseBody;
string externalAccId='0017F00001Xg9LH'; // Id of account from SYSTEM B
string endPoint='https://ap5.salesforce.com/services/apexrest/getAccountOnExternalIdtofetchsinglerecord/' +externalAccId;
restApiTofetchSingleRecord obj=new restApiTofetchSingleRecord();
accToken=obj.getRequestToken();
system.debug('access token'+ accToken);
if(accToken!='')
{
Http h1=new Http();
HttpRequest req1=new HttpRequest();
req1.setHeader('Authorization','Bearer '+accToken);
req1.setHeader('Content-Type','application/json');
//req1.setHeader('Accept','application/json');
req1.setMethod('GET');
req1.setEndpoint(endPoint);
HttpResponse hresp1=h1.send(req1);
resultWrapper wResp1=(resultWrapper) JSON.deserialize(hresp1.getBody(),resultWrapper.class);

listWrap.add(wResp1);

}



}

public class resultWrapper{

public string id{get;set;}
public string name{get;set;}


}



}
2)CREATE A VISUALFORCE PAGE:

<apex:page controller="restApiTofetchSingleRecord">
<apex:form >
<apex:pageBlock >

 <apex:pageblockButtons >
 <apex:commandButton value="TEST" action="{!getConList}"/>

 </apex:pageblockButtons>
  <apex:pageblocktable value="{!listWrap}" var="a" >

<apex:column value="{!a.name}"/>
</apex:pageBlockTable>

 </apex:pageBlock>
 </apex:form>
</apex:page>

3)Remote site URL:

Salesforce api


RESULT IMAGE after previewing Visualforce page:


salesforce api

In this way we completed Salesforce rest api by taking two Salesforce systems.

Please comment below if you have any doubts.

YOU CAN VISIT OTHER POST FROM OUR BLOG AS WELL,

Salesforce rest api integration to fetch list of records (Click here)

Salesforce rest api integration to create a record (Click here)

Salesforce rest api integration to update a record (Click here)

Salesforce rest api integration to delete a record (Click here)

Salesforce soap integration (Click here)

Salesforce rest api callouts (Click here)

**************************************************************************************************************
TAGS: Salesforce rest api integration between Salesforce and Salesforce,Salesforce api,Salesforce rest api,Salesforce rest api integration to fetch a record.
**************************************************************************************************************