I tried the code which was posted in salesforce blogs
https://developer.salesforce.com/blogs/developer-relations/2015/07/using-apex-to-integrate-salesforce.html
I made some minor changes to trigger and the class to bulk load data between two different organizations.
Create Trigger on account object.
SendAccountUsingRESTAPI Apex Class
https://developer.salesforce.com/blogs/developer-relations/2015/07/using-apex-to-integrate-salesforce.html
I made some minor changes to trigger and the class to bulk load data between two different organizations.
Create Trigger on account object.
trigger SendAccount on Account(after insert)
{
String jsonstr = JSON.serializePretty(Trigger.new);
SendAccountUsingRESTAPI.callcreateAcc(jsonstr);
}
SendAccountUsingRESTAPI Apex Class
public class SendAccountUsingRESTAPI {
private final String clientId = 'Client Id From Connected App';
private final String clientSecret = 'clientSecret From Connected App';
private final String username = 'User Name';
private final String password = 'Password';
public class deserializeResponse
{
public String id;
public String access_token;
}
public String ReturnAccessToken (SendAccountUsingRESTAPI 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);
System.debug('\n Body Response:'+res.getbody());
deserializeResponse resp1 = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);
return resp1.access_token;
}
@future(callout=true)
public static void callcreateAcc(String jsonstr)
{
SendAccountUsingRESTAPI acount1 = new SendAccountUsingRESTAPI();
String accessToken;
accessToken = acount1.ReturnAccessToken (acount1);
if(accessToken != null){
String endPoint = 'https://ap1.salesforce.com/services/apexrest/v1/sampledemo/';
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);
String trimmedResponse = res1.getBody().unescapeCsv().remove('\\');
List<Account> accountsDeserialized = (List<Account>) JSON.deserialize(trimmedResponse, List<Account>.class);
List<Account> lstAccount=new List<Account>();
for(Account acc:accountsDeserialized){
Account ac=new Account(Id=acc.Customer_ID__c);
ac.Customer_ID__c=acc.Id;
lstAccount.add(ac);
}
if(!lstAccount.isEmpty()){
update lstAccount;
}
}
}
}
In the Other Salesforce Org we need to created a rest web service. I create a sampledemo class
@RestResource(urlMapping='/v1/sampledemo/*')
global with sharing class sampledemo {
@HttpPost
global static String createAccount(){
String requestBody = RestContext.request.requestBody.toString();
// List<Account> accountsDeserialized = (List<Account>) JSON.deserialize(requestBody, List<Account>.class);
//insert accountsDeserialized;
List<Account> accountsDeserialized=new List<Account>();
List<Object> m = (List<Object>)JSON.deserializeUntyped(requestBody);
for(Object obj:m){
Map<String,Object> requests = (Map<String,Object>)obj;
Account acc=new Account();
acc.Name=(String)requests.get('Name');
acc.Customer_Id__c=(Id)requests.get('Id');
accountsDeserialized.add(acc);
}
if(!accountsDeserialized.isEmpty()){
insert accountsDeserialized;
}
String returnResponse = JSON.serialize(accountsDeserialized);
return returnResponse;
}
}
I tried the below code to load 3 accounts. It made 2 callouts(one for authentication and the other for data sync).
List<Account> lstAccount=new List<Account>();
for(Integer i=0;i<3;i++){
Account acc=new Account();
acc.Name= 'Test Sample'+i;
lstAccount.add(acc);
}
insert lstAccount;
The response we get from the External org will be in JSON format. The response we are parsing and updating the customer id field.