Salesforce to Salesforce Integration using Rest API Callouts

Salesforce to Salesforce Integration using Rest API Callouts

“Integration” itself is a vast topic, though with a bit of understanding and practice one can master any skill what so ever. Answering the question posted here here is what I think is the best solution.

For the reason of integrating two Qrgs which in our case, between two separate Salesforce Orgs, we have to assume one Org as Client or Source and another as Server or Destination or Target, so here whenever a Account record is created in the Source Org, data automatically will be pushed in Account object at Destination Org .

Let’s start our journey now.


First we talk what we have to do in the in the Destination org:

Step 1.  Create a Connected app in Destination org giving the callback url with the details as mentioned in the image below.


Create a Connected App on Destination Org

Now don’t forget to copy the Consumer Key (Client id) and Consumer Secret (Client Secret) we’ll need these in Source org Rest Callout Class.

Step 2. Create a Rest Webservice with @httpPost & @httpGet verb, though it’s not required in our case.

Find the class below.


* Description - Apex REST service with GET and POST methods

* Author - AP

<u>Json format</u>


"name" : "Akash",

"phone" : "8826031286",

"website" : ""




global with sharing class REST_AccountService_V4 {


global static AccountWrapper doPost(String name, String phone, String website) {

RestRequest req = RestContext.request;

RestResponse res = RestContext.response;

AccountWrapper response = new AccountWrapper();

Account acct = new Account();

acct.Name = name;

acct.Phone = phone;

acct.Website = website;

insert acct;


response.status = 'Success';

response.message = 'Your Account was created successfully.';

return response;



global static AccountWrapper doGet() {

RestRequest req = RestContext.request;

RestResponse res = RestContext.response;

AccountWrapper response = new AccountWrapper();

String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);



searchAccounts(req, res, response);




findAccount(res, response, accountId);


return response;


// If the item to the right of the last forward slash is "accounts", the request went to


// Else the request went to v3/accounts/&lt;something&gt;, which is not a search, but a specific


private static boolean doSearch(String accountId) {

if(accountId == 'accounts') {

return true;


return false;


//If the request came to /v3/accounts, then we want to execute a search

Public static void searchAccounts(RestRequest req, RestResponse res,

AccountWrapper response) {

//Use the RestRequest's params to fetch the Name parameter

String searchTerm = req.params.get('Name');

if(searchTerm == null || searchTerm == '') {

response.status = 'Error';

response.message = 'You must provide a Name for your search term.';

res.StatusCode = 400;


else {

String searchText = '%'+searchTerm+'%';

List&lt;Account&gt; searchResults =

[SELECT Id, Name, Phone, Website FROM Account WHERE Name LIKE : searchText];

if(searchResults != null &amp;&amp; searchResults.size() &gt; 0) {

response.acctList = searchResults;

response.status = 'Success';

response.message = searchResults.size()

+ ' Accounts were found that matched your search term.';


else {

response.status = 'Error';

response.message =

'No Accounts were found based on that Name, please search again.';





//If the request came to v3/accounts/sometext then we want

//to find a specific account

Public static void findAccount(RestResponse res, AccountWrapper response,

String accountId) {

// Provided we recevied an External Id, perform the search and return the results

if(accountId != null &amp;&amp; accountId != '') {

List&lt;Account&gt; result = [SELECT Id, Name, Phone, Website FROM Account

WHERE External_Id__c =: accountId];

if(result != null &amp;&amp; result.size() &gt; 0) {


response.status = 'Success';


else {

response.status = 'Error';

response.message = 'This account could not be found, please try again.';

res.StatusCode = 404;



// If the request came to /v3/accounts/ (without an Account Id),

//return an error

else {

response.status = 'Error';

response.message = 'You must specify an External Id.';

res.StatusCode = 400;



global class AccountWrapper {

public List&lt;Account&gt; acctList;

public String status;

public String message;

public AccountWrapper(){

acctList = new List&lt;Account&gt;();




Note: Please ignore the Naming conventions as they may look funny.

Now is the time to configure the Source org:

Step 1. Create Remote site in Source like in my case I created the below one.

Remote Site

Create a Remote Site in Source Org

Step 2. Create a class for Rest Callout in the Source org.

Goto: Create==>Apex Class==>Click New


*Description: Rest Callout

*Owner: AP


public class SendAccount {

<strong>    //Use your Client Id </strong>
String clientId =3MVG9YDQS5WtC11o4mAP_I******************************     OoLrKY3FUrb6221Ms9hjkqhZo_oTNbjKu2LJBHN_tFmZ1.';

<strong> //Use your Client Secret</strong>
String clientsecret='314426******97831';

<strong>   //Your Destination org username</strong>
String username='';

<strong>//Your Destination org</strong><strong>Password+Security Token</strong>
String password='Qwerty@10064OMwL3ZNSh6WP5lVqRxapaHUK';

String accesstoken_url='';

String authurl='';

public class deserializeResponse {

public String id;

public String access_token;


public String ReturnAccessToken(SendAccount Acc){

String reqbody = 'grant_type=password&amp;client_id='+clientId+'&amp;client_secret='

// String reqbody='{"grant_type":"password","client_id":clientId,"client_secret":clientSecret,

Http h= new Http();

HttpRequest req= new HttpRequest();



<strong>       //Change "ap4" in url to your Destination Org Instance</strong>

HttpResponse res=h.send(req);


deserializeResponse resp1=


return resp1.access_token;



public static void createAccount(String Accname, String Phone, String Website){

SendAccount acc1= new SendAccount();

String accessToken=acc1.ReturnAccessToken(acc1);



<strong>           //Change "ap4" in url to your Destination Org</strong>
String endPoint=''; <strong> </strong>

String jsonstr='{"Name":"'+ Accname +'","Phone":"'+ Phone +'","Website":"'+ Website +'"}';

Http h2= new Http();

HttpRequest req2= new HttpRequest();

req2.setHeader('Authorization','Bearer ' + accessToken);






HttpResponse res2=h2.send(req2);


deserializeResponse deresp=(deserializeResponse)System.JSON.deserialize(res2.getBody(),deserializeResponse.class);

System.debug('### Deserialized Response ###'+deresp);




Now Create a Trigger to make callout when some data is inserted in the Account Object.

Try This:

Trigger SendMyAccount on Account (after insert)
{for(Account {SendAccount.createAccount(, a.Phone, a.Website); } }

Now save and start testing by creating a record on Account Object in Source Org. Hope you got it right.

Thanks everyone,

“Don’t let the tiny bit of Doubt today be a road-blocker Tomorrow”, so keep on asking friends.

“Happy Learning”


Leave a Reply


We're not around right now. But you can send us an email and we'll get back to you, asap.

Property of
Copyright 2017 Forcetalks. All Right Reserved.

Log in with your credentials


Forgot your details?