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';
}
}
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:
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;}
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>
<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:
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)
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.
**************************************************************************************************************
TAGS: Salesforce rest api integration between Salesforce and Salesforce,Salesforce api,Salesforce rest api,Salesforce rest api integration to delete a record.
**************************************************************************************************************
No comments:
Post a Comment