Tuesday, January 28, 2014

Salesforce to Salesforce Integration using Apex Code

Hello Friends
This time is to integrate salesforce to salesforce  using custom code. First of all let's have a brief  idea of Salesforce to Salesforce integration. Let if any salesforce org wish to share their records with another salesforce, then we need to make a connection between both org. This phenomena to make connection between  any two salesforce org is called the Salesforce to Salesforce integration. To make the connection you need to follow some steps like following :-

1. Go set up(top corner of right side).
2. Customize -> Salesforce to Salesforce
3. Enable salesforce to salesforce
4. Next go to Connection tab
5. Create a new connection with another salesforce org.
    (This link may be used to just create a new connection                                                                                   http://wiki.developerforce.com/page/An_Introduction_to_Salesforce_to_Salesforce)

If we are using the customize way to integrate salesforce to saleforce , then it may be a bit confusion  that which record has been send to the connected org. To make a separation among records like sent or not, we will use  Apex Controller, VF page, Custom Field(checkbox) , Custom detail page button(on record which you want to share with another org.).
Just for a understanding we are using Lead sObject to share.

*************************Apex Controller******************************

public class ConnectionController
{
    String id;
    public ConnectionController(ApexPages.StandardController controller)
    {
        id = (String)controller.getRecord().id;
    }
    public void sendLead()
    {
        boolean flag=false;
        List<PartnerNetworkConnection> connMap = new List<PartnerNetworkConnection>();
        connMap=[select Id, ConnectionStatus, ConnectionName from PartnerNetworkConnection   where              ConnectionName = 'TechnoMile'] ;
        Lead ld =[Select id, Send_To_Other_ORG__c From Lead where id =: id];
        for(PartnerNetworkConnection network : connMap) {
             PartnerNetworkRecordConnection newrecord = new PartnerNetworkRecordConnection();
            newrecord.ConnectionId = network.Id;
            newrecord.LocalRecordId = ld.id;
            newrecord.SendClosedTasks = true;
            newrecord.SendOpenTasks = true;
            newrecord.SendEmails = true;
            insert newrecord;
            if(newrecord.id != null)
                flag=true;
        }
        if(flag)
        {
            ld.Send_To_Other_ORG__c = true;
            update ld;
        }
       
    }
   
}



Here Send_To_Other_ORG__c is custom field of chekbox type. If this is true mean , this record has been sent to the connected org otherwise not.


***************************VF Page********************************************

<apex:page standardController="Lead" extensions="ConnectionController" action="{!sendLead}">
 
</apex:page>

This vf page will be called by a detail page button. That button will exist on the detail page of the lead.



No comments:

Post a Comment