Hi Vamsi, You can write a trigger on insert of a record in org1 that will call a webservice (callout). This webservice will insert a record in org2. Callout is a ASynchronous process where as Trigger is Dynamic / Synchrinous. That means it is not directly possible to do a webservice callout from a triiger. But using @Future annotation we can convert the Trigger into a Asynchrinous Class and we can use a Callout method.
Hi Vamsi, I have some example for Account object, this is the trigger I use:
trigger SendAccounts on Account (after insert, after update){
BatchSendAccount BSA = new BatchSendAccount(trigger.new);
id batch = DataBase.executeBatch(BSA,50);
}
I recommend to use a Batch because Salesforce has limits about the number of callouts and if you are passing more than 100 records you reach the limit and you will get an error, this is the batch I use:
global class BatchSendAccount implements Database.Batchable<sObject>, Database.AllowsCallouts, Database.Stateful{
global List<Account> ListAccount;
global BatchSendAccount(List<Account__c> ListAccounts){
this.ListAccount = ListAccounts;
}
global List<Account__c> start(Database.BatchableContext BC){
return this.ListAccount;
}
global void execute(Database.BatchableContext BC, List<Account> scope){
SendHandler SH = new SendHandler();
SH.sendAccounts(scope);
}
global void finish(Database.BatchableContext BC) {
}
}
and this is the class where I make the login to the other org, created the JSON and finally send the data to the other org:
public class SendHandler {
String LOGIN_DOMAIN = 'test'; //sandbox environment
String ST = '*********************'; //security token
String pwd = '*******';//password
String userName = '******@*********';//user
String SERVER_URL;
String SESSION_ID;
public void login(){
HttpRequest request = new HttpRequest();
request.setEndpoint('https://' + LOGIN_DOMAIN + '.salesforce.com/services/Soap/u/22.0');
request.setMethod('POST');
request.setHeader('Content-Type', 'text/xml;charset=UTF-8');
request.setHeader('SOAPAction', '""');
request.setBody('<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Header/><Body><login xmlns="urn:partner.soap.sforce.com"><username>' + userName+ '</username><password>' + pwd+ST+ '</password></login></Body></Envelope>');
HttpResponse response = (new Http()).send(request);
Dom.XmlNode resultElmt = response.getBodyDocument().getRootElement()
.getChildElement('Body', 'http://schemas.xmlsoap.org/soap/envelope/')
.getChildElement('loginResponse', 'urn:partner.soap.sforce.com')
.getChildElement('result', 'urn:partner.soap.sforce.com');
SERVER_URL = resultElmt.getChildElement('serverUrl', 'urn:partner.soap.sforce.com') .getText().split('/services')[0];
SESSION_ID = resultElmt.getChildElement('sessionId', 'urn:partner.soap.sforce.com') .getText();
}
public void sendAccounts(List<Account> accs){
login();
for(Account acc : accs){
final PageReference theUrl = new PageReference(SERVER_URL + '/services/data/v25.0/sobjects/Account/External_ID__c/'+acc.External_ID__c+'?_HttpMethod=PATCH');//I have a external ID field so with this field I make a upsert the external ID is External_ID__c
JSONGenerator gen = JSON.createGenerator(true);//I create the JSON
gen.writeStartObject();
if(acc.Name!=null && acc.Name!=''){
gen.writeStringField('Name', acc.Name);}
if(acc.Access_Code__c!=null && acc.Access_Code__c!=''){
gen.writeStringField('Access_Code__c', acc.Access_Code__c);}
if(acc.Access_Hours__c!=null && acc.Access_Hours__c!=''){
gen.writeStringField('Access_Hours__c', acc.Access_Hours__c);}
//gen.writeStringField('Account__c', acc.Account__c);
if(acc.Account_Number__c!=null && acc.Account_Number__c!=''){
gen.writeStringField('Account_Number__c', acc.Account_Number__c);}
if(acc.Account_Site__c!=null && acc.Account_Site__c!=''){
gen.writeStringField('Account_Site__c', acc.Account_Site__c);}
if(acc.Account_Source__c!=null && acc.Account_Source__c!=''){
gen.writeStringField('Account_Source__c', acc.Account_Source__c);}
if(acc.Account_Tier_Value__c!=null){
gen.writeNumberField('Account_Tier_Value__c', acc.Account_Tier_Value__c);}
if(acc.Account_Owner__c!=null && acc.Account_Owner__c!=''){
gen.writeStringField('Account_Owner__c', acc.Account_Owner__c);}
if(acc.Addtl_Notes__c!=null && acc.Addtl_Notes__c!=''){
gen.writeStringField('Addtl_Notes__c', acc.Addtl_Notes__c);}
if(acc.Annual_Revenue__c != null){
gen.writeNumberField('Annual_Revenue__c', acc.Annual_Revenue__c);}
if(acc.Available_Capital__c!=null){
gen.writeNumberField('Available_Capital__c', acc.Available_Capital__c);}
if(acc.Billing_City__c!=null && acc.Billing_City__c!=''){
gen.writeStringField('Billing_City__c', acc.Billing_City__c);}
if(acc.Billing_Country__c!=null && acc.Billing_Country__c!=''){
gen.writeStringField('Billing_Country__c', acc.Billing_Country__c);}
if(acc.Billing_Postal_Code__c!=null && acc.Billing_Postal_Code__c!=''){
gen.writeStringField('Billing_Postal_Code__c', acc.Billing_Postal_Code__c);}
if(acc.Billing_State__c!=null && acc.Billing_State__c!= ''){
gen.writeStringField('Billing_State__c', acc.Billing_State__c);}
if(acc.Billing_Street__c!=null && acc.Billing_Street__c!=''){
gen.writeStringField('Billing_Street__c', acc.Billing_Street__c);}
gen.writeEndObject();
String jsonBody = gen.getAsString();
HttpRequest request = new HttpRequest();
request.setEndpoint(theUrl.getUrl());
request.setHeader('Content-Type', 'application/json');
request.setHeader('Authorization', 'OAuth ' + SESSION_ID);
request.setMethod('POST');
request.setBody(jsonBody);
HttpResponse response = (new Http()).send(request);
}
}
All this are custom fields, I hope this will usefull to you.
Can you explain in breifly.I have no idea how to achieve
You can write a trigger on insert of a record in org1 that will call a webservice (callout). This webservice will insert a record in org2.
Callout is a ASynchronous process where as Trigger is Dynamic / Synchrinous. That means it is not directly possible to do a webservice callout from a triiger.
But using @Future annotation we can convert the Trigger into a Asynchrinous Class and we can use a Callout method.
I recommend to use a Batch because Salesforce has limits about the number of callouts and if you are passing more than 100 records you reach the limit and you will get an error, this is the batch I use:
and this is the class where I make the login to the other org, created the JSON and finally send the data to the other org:
All this are custom fields, I hope this will usefull to you.