Sharing is caring!
Businesses may have multiple Salesforce org to serve different purposes or to partner with another business that uses Salesforce. So, there is a need to collaborate or share records across various orgs. A Salesforce-to-Salesforce connection will help them set up a connection between two orgs and then share records.
Only a small percentage of Salesforce customers may need to think about the Salesforce to Salesforce connection. Some companies may need numerous Salesforce org within a single company, while others may just need to exchange records with business partners with whom they collaborate.
Through this blog, I will show you how you can integrate one Salesforce org to another Salesforce Org using REST API. You will need the following things for this:
- Source Org (Salesforce Org)
- Target Org (Salesforce Org)
Org A (Source Org)
- You need to set the endpoint URL of the Target Org in the Remote Site setting (setup >> Remote Site Setting).
- Then you will write the a) Future callout class. b) Trigger, which calls Future Class on Insertion.
Org B (Target Org)
- Create a connected app and obtain clientId, clientSecret, username, and password (a combination of password and security token) from Org B (Target Org).
- Write Webservice class that consumes JSON and does the operation in target org.
Step-by-step Integration Of Source & Target Salesforce Org
Step 1: Create a Connected App in Target Org.
Salesforce Connected App
“Connected App” is an application that connects Salesforce org with an external application. It uses OAuth protocols to authenticate and provide tokens to use with Salesforce APIs.
- Go to Setup->App->App Manager
- Click on the ‘New Connected App’ Button in the “Connected App” section, Below screen will be shown
- Enter the Name of the Application.
- Enter your Email and any further information suitable for your application.
- Enable OAuth settings in the API section.
- Enter a Callback URL. With this URL, the user’s browser gets redirected after successful authentication. Here I’m giving “https://login.salesforce.com/services/oauth2/callback” as a callback URL. You can enter multiple callback URLs, but you have to ensure that the URL must be secured, and if multiple URLs are used, they should be separated by line breaks.
- Add Selected OAuth Scopes. Here I’m giving “Full access(full).”
- Click on the ‘Save’ button.
- Click on ‘Continue’
- You will be redirected to your Connected App’s Page
After creating the Connected App, here are the steps you need to follow:
- Click on Manage Consumer Details.
- You will receive an OTP in the Mail for verification.
- Get the “Consumer Key “ and “Consumer secret,” as these details are needed to authenticate the external application.
- Click on cancel and Click Manage.
- Click on Edit policies.
- Click on Ip Relaxation and select Relax IP Restrictions and Click save.
After completing this step, we will see how to call and integrate Salesforce.
Step 2 – Create an Apex Class in Target Org to Create Account Record Same as source Org.
@RestResource(urlMapping=’/createAccount/*’)
global with sharing class createAccount
{
@HttpPost
global static String createAccount(String AccName)
{
Account a = new Account();
a.Name = AccName;
insert a;
String returnResponse = JSON.serialize(a);
return returnResponse;
}
}
Step 3 – Create a Remote Setting on Source Org.
Search “Remote Site Setting” in the Quick Find Box and Enter the Remote Site URL of your Target org; the format is like that:
Step 4 – Create a Trigger to make an asynchronous call from source org to target org.
Trigger on account Object:
trigger AccountCreation on Account(after insert){
for(Account a : Trigger.new){
AccountCreatedFromSource.createAccountinTargetOrg(a.Name, a.Id);
}
}
Step 5 – Apex Class to make a callout – Replace the client id, secret, username, and password of your org.
public class AccountCreatedFromSource {
public class deserializeResponse{
public String id;
public String access_token;
}
@future(callout=true)
public static void createAccountinTargetOrg(String accName, String accId){
String clientId = ‘3MVG9fe4g9fhX0E4sI3b4IhA.5w5tiL561flLx_BAQ9AFQzHfSmpVzpESc0bro5UXQVS6HA9HpTt9ifqhCcwj’;//your consumer Key
String clientSecret = ’10B55107DF04867B65F3E83A95CB296AFFD5FF6DC9C02B3C86633451DB5CB9FB’;//your consumer secret
String username = ‘your username’;
String password =’your Password;
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://cloudanalogy268-dev-ed.my.salesforce.com/services/oauth2/token’);
HttpResponse res = h.send(req);
deserializeResponse resp1 = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);
if(resp1.access_token != null){
String endPoint = ‘https://cloudanalogy268-dev-ed.my.salesforce.com/services/apexrest/createAccount/’;
String jsonstr = ‘{“AccName” : “‘ + accName + ‘”}’;
Http h2 = new Http();
HttpRequest req1 = new HttpRequest();
req1.setHeader(‘Authorization’,’Bearer ‘ + resp1.access_token);
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);
}
}
}
Conclusion
Businesses may have multiple Salesforce org for various reasons. So, a need arises to collaborate or share records across different orgs. A Salesforce-to-Salesforce connection helps them to set up a connection between two orgs and share records. When you need to connect two Salesforce org for any reason, you should carefully consider why it is needed, what data needs to be synced between them, how often it needs to be synced, and how to craft a plan (and contingency plan) to ensure there are no unpleasant surprises when you’re setting up the connection.
Cloud Analogy is one of the top-notch Salesforce Consulting companies with trust in the Salesforce platform’s capabilities that can help businesses run more efficiently and effectively. We assure you that world-class, cost-efficient Salesforce Consulting services will transform and grow your business in various ways and act as your trusted Salesforce consulting partner. Reach out to our expert and certified team at Cloud Analogy and start your project today.
Sachin Arora
Scrum Master and Principal Solutions Architect
Sachin, a renowned Scrum Master and Principal Solutions Architect at Cloud Analogy, has rich experience when it comes to working on process improvement in a fast-paced environment maintaining high level of quality in all deliverables. Sachin's expertise lies in varied hardware and software environments including Cloud technologies such as Salesforce, AWS, Cloud Foundry & Google App Engine and Mobile.Hire the best Salesforce Development Company. Choose certified Salesforce Developers from Cloud Analogy now.