function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vamsi Reddy 36Vamsi Reddy 36 

Is it possible to write a trigger for inserting a record in org1 so that the record should be created in Org2 In INTEGRATion

Angello Camacho DragoAngello Camacho Drago
You need a trigger that when you insert a record it call a batch or future method than send the data to the other org through web service.
Vamsi Reddy 36Vamsi Reddy 36
Hi Angello,
Can you explain in breifly.I have no idea how to achieve
Anurag VermaAnurag Verma
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. 
Angello Camacho DragoAngello Camacho Drago
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(;
         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();
    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 + '');
        request.setHeader('Content-Type', 'text/xml;charset=UTF-8');
        request.setHeader('SOAPAction', '""');
        request.setBody('<Envelope xmlns=""><Header/><Body><login xmlns=""><username>' + userName+ '</username><password>' + pwd+ST+ '</password></login></Body></Envelope>');
        HttpResponse response = (new Http()).send(request);
        Dom.XmlNode resultElmt = response.getBodyDocument().getRootElement()
          .getChildElement('Body', '')
          .getChildElement('loginResponse', '')
          .getChildElement('result', '');
        SERVER_URL = resultElmt.getChildElement('serverUrl', '') .getText().split('/services')[0];
        SESSION_ID = resultElmt.getChildElement('sessionId', '') .getText();
    public void sendAccounts(List<Account> accs){
              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
            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);}
                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);}
                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);}
            String jsonBody = gen.getAsString();
            HttpRequest request = new HttpRequest();
            request.setHeader('Content-Type', 'application/json');
            request.setHeader('Authorization', 'OAuth ' + SESSION_ID);
            HttpResponse response = (new Http()).send(request);   

All this are custom fields, I hope this will usefull to you.