Tuesday 19 July 2011

Salesforce To Salesforce

Hai Friends, Now i came with the topic "Salesforce to Salesforce",
Introduction :- Salesforce to Salesforce enables you to share records among the business partners.
Step1:-
To establish connection from one organization to another salesforce organization ,we need to enable settings first in both organizations.
Set up--> AppSetup-->Customize-->Salesforce to Salesforce--> Settings.
After enabling settings we need to add another salesforce organization Name and it's Email as Contact in to parent salesforce organization.
Step2:-
Now you can find Tab 'Connections', add that Tab to Tabs bar.
Step3:-  Sending Invitation to another organization.
Now click on 'Connection Tab',click on New--> Select  the contact ,that we have created as per in earlier step--> Click on Save&Send Invite.
Then Email will send to Email,which is associated in that contact.
Step4:-
In that Email,we can get an Link,Click on link and give the login details of client (add contact  sales force organization login details).Now you can see the connection invitation in child salesforce organization.
Click on Accept,Now this allows you to share lead, opportunity, account, contact, task, product, opportunity product, case, case comment, attachment, or custom object records with your business partners. With Salesforce to Salesforce, you can share records with one or more connections, and each connection can accept records you share with them - even if other connections have accepted the same record.
Step 5:- I want to share Accounts
First publish this object from parent connection,for that
Click connection detail page,there you can find the Related list "Publish Object",there you can find button 'publish/unpublish'.Click on that button ,then in new window you can see all the available objects for connection.select Account click on Save.
Now goto business partner Organization,on Connection Detail page you can find related list for Subscribe object, click on button 'subscribe/unsubscribe',click on that button,select object from pick list and click on save. Now to share account records goto parent Salesforce organization ,select Account records from list view of Account records and click on 'Forward to Connections', there you can find list of  Available connections in your organization,select organization and click on save.
Then that Account record will be created in Account object of the business partner.
In this way we can share account records from parent org to business partner organization.
Step 5:- I want to share  contact and opportunities associated with an Account record
We can achieve this by publishing contacts and opportunity objects to business partner and he must subscribe to that objects also.Now send the account record which is associated with above contact and opportunity objects as we discuss in earlier step.

Questions
1. My organization Having 15000 records in Accounts object, can we transfer all the records in one step?
---> Yes, you can achieve this by writing  Apex class. I am giving code for this please go through once you can under stand easily.
Controller:-

global class SendOldRecord
{
        public void sendrec(list<Account> ls)
        {
            //This list declaration for PartnerNetworkrecords
            List<PartnerNetworkRecordConnection> lstShareRecords = new List<PartnerNetworkRecordConnection>();
            //This list declaration for the Connections with this Organization.
            List<PartnerNetworkConnection> connMap=[select Id, ConnectionStatus, ConnectionName from PartnerNetworkConnection
                    where ConnectionStatus = 'Accepted' and ConnectionName='Appshark Software Pvt Ltd'];
            //This for is to loop all the connections
                    for(PartnerNetworkConnection network : connMap) 
                    {    
                        for(Account acc:ls)
                        {
                        //object declaration for PartnerNetwork
                        PartnerNetworkRecordConnection newrecord = new PartnerNetworkRecordConnection();
                        system.debug('Second for loop');
                        newrecord.ConnectionId = network.Id;
                        newrecord.LocalRecordId = acc.id;  
                        newrecord.RelatedRecords = 'Contact';
                        newrecord.SendClosedTasks = true;
                        newrecord.SendOpenTasks = true;
                        newrecord.SendEmails = true;
                        //All the Records are added to the PartnerNetwork
                        lstShareRecords.add(newrecord);
                        }
                   }
              system.debug('List\n'+lstShareRecords);
             //These Records are inserted into Partnernetwork
             //using lstShareRecords.
              insert lstShareRecords;
        }
}
in the case of bulk records , we need to handle them in Batch process,for i have implemented following batch class and pass to list of accounts at every run,
Batch Controller:-
global class RecordBatchApex implements Database.Batchable<Account>
{
global list<Account> start(Database.BatchableContext bc)
{       
    list<account> lstAcc = [select Id from Account];
    return lstAcc;
}

global void execute(Database.BatchableContext bc,List<Account> lstAccount)
{        
system.debug('*******************Execute of Batch Apex***********');
system.debug(lstAccount);
sendoldrecord od=new sendoldrecord();
od.sendrec(lstAccount);
}
//BatchApex Completes
// execution with this finish method
global void finish(Database.BatchableContext BC)
{
system.debug('****Finished*****');
}
Once run that code even from System log,then you can find all the records in business partner login.

2. I want to  insert newly created Account in to partner's organization.
In this Scenario we will go for Apex trigger to create newly inserted account into partner's Organization.

Trigger autoforwardAccount on Account(after insert)
{
String UserName = UserInfo.getName();
String orgName = UserInfo.getOrganizationName();
List<PartnerNetworkConnection> connMap = new List<PartnerNetworkConnection>(
[select Id, ConnectionStatus, ConnectionName from PartnerNetworkConnection where ConnectionStatus = 'Accepted']
);
System.debug('Size of connection map: '+connMap.size());
List<PartnerNetworkRecordConnection> prncList = new List<PartnerNetworkRecordConnection>();
for(Integer i =0; i< Trigger.size; i++)
{
Account acc = Trigger.new[i];
String acId = acc.Id;
System.debug('Value of AccountId: '+acId);
for(PartnerNetworkConnection network : connMap)
{
String cid = network.Id;
String status = network.ConnectionStatus;
String connName = network.ConnectionName;
String AccountName = acc.Name;
System.debug('Connectin Details.......Cid:::'+cid+'Status:::'+Status+'ConnName:::'+connName+'AccountName:::'+AccountName);
if(AccountName !=Null)
{
PartnerNetworkRecordConnection newrecord = new PartnerNetworkRecordConnection();
newrecord.ConnectionId = cid;
newrecord.LocalRecordId = acId;
newrecord.SendClosedTasks = true;
newrecord.SendOpenTasks = true;
newrecord.SendEmails = true;
System.debug('Inserting New Record'+newrecord);
insert newrecord;
}
}
}
}

3. Can we insert Contact or Opportunity related for an account,while inserting contact or opportunity ?


Yes, We can, for that we need to trigger on Contact/Opportunity ,here we are inserting contact/opportunity related to an account for that we need to specify account Id also. 
            We PartnerNetworkRecordConnection object for sharing records between salesforce to salesforce.
It contains filed 'ParentRecordId',to that filed we need to assign that account id.I have written trigger on conatct , i am giving code for that,you can implement same thing for opportunity also.
               
 Trigger autoforwardContact on Contact(after Insert)
{  
String  UserName = UserInfo.getName(); 
 String orgName = UserInfo.getOrganizationName();  
List<PartnerNetworkConnection> connMap = new List<PartnerNetworkConnection>(   [select Id, ConnectionStatus, ConnectionName from PartnerNetworkConnection where ConnectionStatus = 'Accepted']  ); 
System.debug('Size of connection map: '+connMap.size());
 List<PartnerNetworkRecordConnection> prncList = new List<PartnerNetworkRecordConnection>(); 
for(Integer i =0; i< Trigger.size; i++) 

Contact con = Trigger.new[i]; 
String conId = con.Id; 
System.debug('Value of ContactId: '+conId);
for(PartnerNetworkConnection network : connMap)

String cid = network.Id;
String status = network.ConnectionStatus; 
String connName = network.ConnectionName;
String ContactName = con.LastName; 
String Accountid = con.Accountid; 
System.debug('Connectin Details.......Cid:::'+cid+'Status:::'+Status+'ConnName:::'+connName+'ContactName:::'+COntactName);
System.debug('Account ID************'+Accountid);
if(ContactName!= NULL)
{  
System.debug('INSIDE IF');  
 PartnerNetworkRecordConnection newrecord = new PartnerNetworkRecordConnection();  
 newrecord.ConnectionId = cid;  
 newrecord.LocalRecordId = ConId;  
 newrecord.ParentRecordId= Accountid; // here we are specifying Account ID  
 newrecord.SendClosedTasks = true;   
newrecord.SendOpenTasks = true; 
  newrecord.SendEmails = true;   
System.debug('Inserting New Record'+newrecord);
 insert newrecord; 
}


}        
Using Above Trigger we can Send newly inserted record in to Partner's organization.
I think This may help to any one of us.

2 comments:

  1. Can we pull record into partner org using 'SELECT'?

    ReplyDelete
  2. Hi Hari Bro
    I want sync Contact_Id__c Field using Partnernetworkconnection for CustomActivity Object can u plaese tell me how to do
    this Is my Batch
    /**
    *
    * @author : BICS GLOBAL
    * Description : Batch class for retrive Custom Activity through ParnterNetworkConnnection Id
    * @date 18-07-2022
    * Copyright (c) $2022 Johnson & Johnson
    **/

    public with sharing class BatchtoSync_custom_activity_PROFG implements Database.Batchable, Schedulable, Database.AllowsCallouts
    {
    public final String RUSSIAN = 'RU';
    public final String UKRAINE = 'UA';
    public void execute(SchedulableContext SC)
    {
    Database.executeBatch(new BatchtoSync_custom_activity_PROFG());
    }
    public Database.QueryLocator start(Database.BatchableContext BC)
    {

    //return Database.getQueryLocator([select Id, Name, First_Name__c, Last_Name__c From Custom_Activity__c where Type__c = 'Subscription']);
    return Database.getQueryLocator([SELECT Id, Name, First_Name__c, Last_Name__c,Contact_Id__c FROM Custom_activity__c where Type__c = 'Subscription' AND (Country__c = 'RU' OR Country__c = 'UA') AND LastModifiedDate = Today]);

    }

    public void execute(Database.BatchableContext BC, List scope)
    {
    List networkconnectionList = ([SELECT Id FROM PartnerNetworkConnection WHERE ConnectionStatus = 'Accepted' LIMIT 1]);

    List pnrcInsertList = new List();
    if(!networkconnectionList.isEmpty())
    {
    for(Custom_Activity__c CustActvt : scope)
    {
    pnrcInsertList.add(new PartnerNetworkRecordConnection( ConnectionId = networkconnectionList[0].Id,
    LocalRecordId = CustActvt.Id,
    // ParentRecordId = 'Contact_Id__r.Id',
    SendClosedTasks = FALSE,
    SendOpenTasks = FALSE,
    SendEmails = FALSE));
    }
    }

    if(!pnrcInsertList.isEmpty())
    {
    Insert pnrcInsertList;
    }
    }

    public void finish(Database.BatchableContext BC)
    {
    }
    }

    ReplyDelete