In this post we will learn about how to integrate one salesforce org to another salesforce org using REST service.
Let's suppose we have 2 different Salesforce Org's and call it Source(where we create record) and Destination(create a record here when a record is created in Source).
Now let's divide this process into 3 steps.
- Creating Connected App
In this step, let us create a connected app in destination
org which is required to connect from Source.
-
Go to Setup and search for App Manager and click on it
- Click on New Connected App
- Fill out Basic Information
- Next Fill API (Enable OAuth Settings)
For now Call Back URL is not required.
So just give some dummy value.
- Click on Save.
- Once you save, it takes 2 minutes to host a connected application
- Consumer Key and Consumer Secret will be generated after 2 minutes.
2.
Create a Trigger and Helper Class in Source Org
Now let's jump into Source Org and create a
connection between 2 Salesforce Org's.
Click on the Gear Icon and Open Developer Console.
- Click on File then New and Choose Apex Trigger and give your Trigger Class Name and select the object you want to Create a Trigger on.
- In this example, we will write a trigger on Account and Name Trigger as accountTrigger and invoke it after the record is inserted.
- Now create a field in Account Object in Source Org to store the record ID that has been created in Target Org and label it External Id.
- Now we will create a helper class(sendAccountDetails) which receives data from Trigger Class(accountTrigger).
public class sendAccountDetails {
private final String clientId = 'consumerKey'; //Consumer Key from Connected App created in Target Org
private final String clientSecret = 'customerSecret'; //Consumer Secret from Connected App created in Target Org
private final String username = 'test@gmail.com';
private final String password = 'password&SecurityToken'; //Replace your Target Org Password and securityToken
public class deserializeResponse{
public String id;
public String access_token;
}
public String ReturnAccessToken (sendAccountDetails account){
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://targetOrgDomain.my.salesforce.com/services/oauth2/token');
HttpResponse res = h.send(req);
deserializeResponse resp1 = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);
return resp1.access_token;
}
@future(callout=true)
public static void createAccount(String accName, String accId){
sendAccountDetails account = new sendAccountDetails();
String accessToken = account.ReturnAccessToken(account);
System.debug('accessToken '+accessToken);
if(accessToken != null){
String endPoint = 'https://targetOrgDomain.my.salesforce.com/services/data/v32.0/sobjects/Account/';
String jsonstr = '{"Name" : "' + accName + '"}';
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.setBody(jsonstr);
req1.setMethod('POST');
req1.setEndpoint(endPoint);
HttpResponse res1 = h2.send(req1);
deserializeResponse resp2 = (deserializeResponse)JSON.deserialize(res1.getbody(),deserializeResponse.class);
Account a = [SELECT Id FROM Account WHERE Id = :accId];
a.External_Id__c = resp2.id;
update a;
}
}
}
- Invoke the helper class created above from Trigger that was created on Account (accountTrigger)
trigger accountTrigger on Account (after insert) {
for(Account a : Trigger.new){
sendAccountDetails.createAccount(a.Name, a.Id);
}
}
3.
Creating a class in Target org and Exposing it as REST Service
- Click on the Gear Icon and Open Developer Console.
- Click on File then New and Choose Apex Class and give your Class Name as createAccountFromOtherSystem
- Enter the code given below.
@RestResource(urlMapping='/v1/createAccountFromOtherSystem/*')
global with sharing class createAccountFromOtherSystem {
@HttpPost
global static String createAccount(String AccName)
{
Account a = new Account();
a.Name = AccName;
insert a;
String returnResponse = JSON.serialize(a);
return returnResponse;
}
}
No comments:
Post a Comment