Today, I will explain to you , how we will integrate one salesforce org data into another one using REST API. For this, you will need the following things -
Here is two Salesforce org-
1. Source Org
2. Target Org
STEP 1- Create a connected App using, Setup-> Create->Apps OR Search “Apps” in Quick Find Box.
Connect App : It is an application used to connects salesforce org with an external application over Identity and other data APIs. It used standard OAuth 2.0 protocol.
Click on New Button & fill the mandatory details and your callback URL looks like this-
https://<domain>.my.salesforce.com/services/oauth2/callback
After clicking on Save Button you will see the Consumer ID and Consumer Secret key(Save that, we will used it for Integration).
Step 2- Create an Apex Class in Target Org to retrieve Accounts from Target Org.
@RestResource(urlMapping='/v1/getOrgAccounts/*')
global with sharing class OrgAccount{
@HttpGet
global static list<account> OrgAccount(){
RestRequest req = RestContext.request;
RestResponse res = Restcontext.response;
List<account> listAccount =[Select Id , Name from Account LIMIT 50];
return listAccount ;
}
}
Step 3 – Create a Remote Setting on Source Org. Search “Remote Site Setting” in Quick find Box & Enter Remote Site URL of your Target org, format is-
https://<domain>.my.salesforce.com
Step 4- Create an Apex Class in your Source Org, which will callout Apex class of your Target Org and return you Accounts.
Replace your -
client ID
clientSecret
username
password
public class GetOrgAccountByRESTAPI {
private final String clientId = 'XXXXX';
private final String clientSecret = 'XXXXXX';
private final String username = 'usernamexxxxx';
private final String password = 'Passwordxxxxx';
public class deserializeResponse
{
public String id;
public String access_token;
}
public String ReturnAccessToken (GetOrgAccountByRESTAPI acount)
{
String reqbody = 'grant_type=password&client_id='
+clientId+'&client_secret='
+clientSecret+'&username='
+username+'&password='+password;
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setBody(reqbody);
req.setMethod('POST');
req.setEndpoint('https://login.salesforce.com/services/oauth2/token');
HttpResponse res = h.send(req);
deserializeResponse response = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);
system.debug('Access Token' + response );
return response.access_token;
}
public static list<account> callGetOrgAccounts()
{
GetOrgAccountByRESTAPI acount1 = new GetOrgAccountByRESTAPI ();
String accessToken;
accessToken = acount1.ReturnAccessToken (acount1);
list<account> ListAccount=new List<account>();
if(accessToken != null){
String endPoint = 'https://mkthe-dev-ed.my.salesforce.com/services/apexrest/v1/getOrgAccounts/';
Http h2 = new Http();
HttpRequest req1 = new HttpRequest();
req1.setHeader('Authorization','Bearer ' + accessToken);
req1.setHeader('Content-Type','application/json');
req1.setHeader('accept','application/json');
req1.setMethod('GET');
req1.setEndpoint(endPoint);
HttpResponse res1 = h2.send(req1);
String trimmedResponse = res1.getBody().unescapeCsv().remove('\\');
system.debug('RESPONSE' + trimmedResponse);
JSONParser parser = JSON.createParser(res1.getBody());
set<account> accList=new set<account>();
while (parser.nextToken() != null) {
if((parser.getCurrentToken() == JSONToken.FIELD_NAME) ){
Account acc;
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'Id')) {
parser.nextToken();
String sId= parser.getText();
acc=new Account();
acc.Id=sId;
system.debug('Id' + sId);
parser.nextToken();
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
(parser.getText() == 'Name')) {
parser.nextToken();
string sName= parser.getText();
acc.Name=sName;
system.debug('Name' + sName );
}
}
accList.add(acc);
}
accList.remove(null);
}
ListAccount.AddAll(accList);
system.debug('AccountList'+Json.serialize(ListAccount));
}
return ListAccount;
}
}
Comments
Post a Comment