Wednesday, June 8, 2016

Sync data Salesforce To Salesforce

Abstract

Force.com is a multitenant platform, and as such multiple customers have their own environments (orgs) in a shared platform. Each environment contains data that the customers wants to store. Sometimes, however, customers may want to share data between themselves. For example, two trading partners may want to share relevant data records.
Salesforce to Salesforce can be used to create this data sharing relationship. Salesforce to Salesforce is a natively supported feature of the Force.com platform, and easily enables two trading partners to share relevant data records between orgs.
This tutorial introduces Salesforce to Salesforce, shows how you can enable it, how to create a connections between two different environments, and how to use the connection to share data.

Enabling Salesforce to Salesforce

Before you can use Salesforce to Salesforce (S2S), you have to enable it. This has to be done by both parties - on your org, as well as on the org with which you are going to share data. Administrators can enable Salesforce to Salesforce by accessing it in the setup menu. Note that once the preference is enabled, it cannot be disabled.
To enable, navigate to Setup -> App setup -> Customize -> Salesforce to Salesforce -> Settings. Hit Edit, select Enable, and then select Save.


Once Salesforce to Salesforce is enabled in your org, you can establish data sharing relationship with another  org by sending an invitation (we will cover sending invitations in a later section). You can  customize the invitation by modifying the from email address, display name and email templates used for sending the invitation emails.
        

Setting up a Connection


Let's assume that your company, as well as another company, have enabled Salesforce to Salesforce. Even though both orgs have S2S enabled, you have to establish a formal relationship between the orgs before sharing can take place. This is done by setting up a connection.
For the sake of a running example let's use two companies
  1. Toy Corp - this wil be the connection initiating company.
  2. Intelligence  - this will be the connection receiving company.
As a best practice, in Toy Corp, create an account representing the company that will receive the connection (Intelligence), and also create a contact with email address under that account. This contact will typically be used when sending the S2S connection invitation.
To begin using Salesforce to Salesforce, you need to use the Connections tab. If you don't have it in an application, add it by navigating to Setup -> My Personal Information-> Change my Display -> Customize My Tabs.
In new version we need to follow; Click on your name on right top corner -> My Settings -> Go to Display & Layout -> Customize My Tabs
On the Connections tab, click New to create a new connection with another org.



Select a contact under an account representing the other organization you want to share records with via S2S, and click "Save & Send Invite". The invitation email will be sent to the email address under the selected contact.
connection owner also needs to be defined. The connection owner will receive all email notifications including system notifications in case there is a functional error when the connection is inserting/updating a record. Additionally, the connection owner will be assigned as the default owner of any new records, though this may be overridden by any assignment or other rules that determine record ownership.
When you click "Save & Send Invite" in this example, the Intelligence system administrator (Joy Partner) will receive an email. Joe Partner will need to paste the URL in the browser address bar and login to initiate Salesforce to Salesforce connection. 

Once logged in the Intelligence org, the system administrator needs to hit Accept for the connection to start sharing records with Toy Corp.
Once the invitation is accepted, the connection to Toy Corp is established, no objects (standard or custom) are shared yet. Only data you choose to share will be shared - and that's that subject of the next section.

Publishing Objects

Once the connection is established, the next step is to publish and subscribe to objects. Both Acme and Appirio can publish and subscribe to each other's objects and fields.
To continue the tutorial, let's publish objects from Intelligence  Do this by clicking on Publish in the Connection tab, and selecting the object that you wish to publish. You can publish most standard objects and all custom objects. We've selected Account, Attachment, Case, Case Comment, Contact and Lead.

:- Select Edit on an object to choose the fields that you wish to publish to the other environment.

Subscribing to Published Objects

The receiving environment (Toy Org in our case) doesn't automatically have access to the data published from the Intelligence environment. Rather, the org must first subscribe to the objects.
Login to Toy Corp and subscribe to these two objects and map the fields. This is done by navigating to the Connections tab, selecting the Intelligence connection, and hitting "Subscribe". You'll now be presented with a list of the published objects to which you can subscribe:

Programmatic sharing records via Apex

Records can be shared for supported objects programmatically (via triggers, batch process, etc.). Programmatic sharing gives a higher level of control over how you share records and also makes it possible to carry over hierarchy/relationship to the target.
Here is basic code snippet to share a Lead record via S2S programmatically.

public class ConnectionHelper {
    public static Id getConnectionId(String connectionName) {
  
        List<PartnerNetworkConnection> partnerNetConList =
           [Select id from PartnerNetworkConnection where connectionStatus = 'Accepted' and connectionName = :connectionName];
      
        if ( partnerNetConList.size() != 0 ) {
            return partnerNetConList.get(0).Id;
        }
      
        return null;
    }
  
}

/This trigger is responsible for share new created and Updated Lead for one org to another org.
trigger LeadShareAnotherOrg on Lead (after insert,after update) {

    // Define connection id
    list<PartnerNetworkConnection> connID = [Select id,connectionName from PartnerNetworkConnection limit 1 ];
    Id networkId = ConnectionHelper.getConnectionId(connID[0].connectionName);
    system.debug('RRRRRRR' +networkId);
    Set<Id> localLeadIdSet = new Set<Id>();
    List<Lead> localLeads = new List<Lead>();
    Set<Id> sharedLeadSet = new Set<Id>();  
  
    for (Lead newLead : TRIGGER.new) {
  
        if (newLead.ConnectionReceivedId == null && newLead.Id != null) {
            localLeadIdSet.add(newLead.ID);
            localLeads.add(newLead);
          
        }       
    }
      List<PartnerNetworkRecordConnection> contactConnections =  new  List<PartnerNetworkRecordConnection>();
  
          for (Lead newLeads : localLeads) {
               PartnerNetworkRecordConnection newConnection =  new PartnerNetworkRecordConnection(
                                                                      ConnectionId = networkId,
                                                                      LocalRecordId = newLeads.Id,
                                                                      SendClosedTasks = false,
                                                                      SendOpenTasks = false,
                                                                      SendEmails = false  );
                                                                  
                 
                        
                   contactConnections.add(newConnection);
            }

            if (contactConnections.size() > 0 ) {
                  database.insert(contactConnections);
            }

}




@isTest
public class ConnectionHelperTest{
static testmethod void updateLead(){
 list<PartnerNetworkConnection> connID = [Select id,connectionName from PartnerNetworkConnection limit 1 ];
            Lead l = new Lead();
            l.FirstName = 'CRM Testing First';
            l.LastName = 'CRM Testing Last';
            l.Company = 'CRM Testing INCtest';
            l.description = 'Test descr';
            l.status = 'Qualified';
            l.email = 'test@testnetgear.com';
            l.website = 'www.testcrm.com';
            insert l;

 // Start Test
 test.startTest();

 // Create instence of class
 ConnectionHelper conHelper = new ConnectionHelper();
 ConnectionHelper.getConnectionId(connID[0].connectionName);

 test.stopTest();
 // Stop Test
}
}


2 comments:

  1. Hi Vishvendra,

    Thanks for providing this information, this is very helpful.

    ReplyDelete
  2. Hi it's really helpful.And I have one help which I want to check the opportunity products status is still active or not in target org using trigger or Apex class.how can i query these connection status fields in opportunity

    ReplyDelete

Common interview questions for Salesforce developers

Name three Governor Limits .                                                                                    Description Synchronous ...