STEP 1) Enabling Salesforce to Salesforce
Enable the Salesforce to Salesforce in both the org. Follow below step to do that
Setup--> Salesforce to Salesforce --> Salesforce to Salesforce Setting.
Then click on Edit button checked the Enable checkbox.
NOTE:- you should have "Manage Connections" access on profile level.
Step 2) Connection Setup
To setup connection you need to open "Connection" Tab. Then Click on New Tab.
Then Select Account and Contact then click on "Save & Send Invite". You also need to set Owner. Owner will get notification if any error will come.
When you will click on "Save & Send Invite" button Contact (Amit Chaudhary) will get a email like below
Then copy above URL and login by other org user.
Then other org user need to accept the invitation.
Now Connection is established but no object is shared.
Step 3) Publishing Objects
Now we need to publish or subscribe the standard or custom object. To publish the object you need to go to first Org and click on "Publish/Unpublish" button.
Then select the object and click on save.
You can select the field from Publish Object related list. Select the object and click on Object name.
Step 4) Subscribing Objects
Now got back to receiving org and click on "Subscribed Objects" related list.
Then map the object.
Now we need to map the field. Go to "Subscribed Object" related list and select object and map field.
Now connection is setup.
Step 5) Using the Shared Connection
To share the record we have two way one is Manual and 2nd is by programming.
- Manual Sharing Records
Then select the connection and click on Save button.
Records will be automatically created in the target environment if auto-accept for the object is enabled
- Programmatic sharing records via Apex
Sample Code.
trigger ShareAccountS2S on Account (After update) {
Set <Id> setAcc = new Set<Id>();
for(account acc : Trigger.New){
if(acc.Active__c == 'Yes'){
setAcc.add(acc.id);
}
}
if(setAcc.size() >0) {
Id NetworkId ;
String connName ='ForBlog'; // Plz add connection Name
list<PartnerNetworkConnection> conns = [select id from PartnerNetworkConnection where ConnectionName = :connName ];
if(conns.size()>0) {
NetworkId = conns[0].id;
}
if( NetworkId != NULL ){
string relatedRecords = 'Contact' ;
list<PartnerNetworkRecordConnection> recordShares = new list<PartnerNetworkRecordConnection>();
for(id accId : setAcc )
{
recordShares.add(new PartnerNetworkRecordConnection(
ConnectionId = networkId,
LocalRecordId = accId,
RelatedRecords = relatedRecords
));
}
insert recordShares;
}
}
}
Set <Id> setAcc = new Set<Id>();
for(account acc : Trigger.New){
if(acc.Active__c == 'Yes'){
setAcc.add(acc.id);
}
}
if(setAcc.size() >0) {
Id NetworkId ;
String connName ='ForBlog'; // Plz add connection Name
list<PartnerNetworkConnection> conns = [select id from PartnerNetworkConnection where ConnectionName = :connName ];
if(conns.size()>0) {
NetworkId = conns[0].id;
}
if( NetworkId != NULL ){
string relatedRecords = 'Contact' ;
list<PartnerNetworkRecordConnection> recordShares = new list<PartnerNetworkRecordConnection>();
for(id accId : setAcc )
{
recordShares.add(new PartnerNetworkRecordConnection(
ConnectionId = networkId,
LocalRecordId = accId,
RelatedRecords = relatedRecords
));
}
insert recordShares;
}
}
}
Related Post
1) Salesforce to Salesforce Overview
2) https://developer.salesforce.com/page/Best_Practices_for_Salesforce_to_Salesforce
Thanks
Amit Chaudhary
@amit_sfdc
Hi Amit, Awesome Blog. But I have a question regarding this let suppose I have created one record in the Source Org and it also gets created in the Target Org. So is their any unique identifier between these records that salesforce is using to map both the records. As when I am updating the record in Source Org it's updating the record in Target also.
ReplyDeleteHello Amith,
ReplyDeleteI have quick question on sharing attachments programatically. I tried to share attachments programmatically between two orgs.
Obervations
>> I was able to share attachment which is .jpg format or .png format
>> When I tried sharing attachment which is of PDF Format , .Docx format and .xls format I am getting below error
System.DmlException: Insert failed. First exception on row 0; first error: INVALID_PARTNER_NETWORK_STATUS, invalid status for partner network operation: []
Any pointers here?
Hi Amit can u please tell me how to auto accept the shared record in the target org so that we dont have to accept it again and again
ReplyDeleteHi Amit, have you tried sharing the records which are not mapped in the connection configuration like files for eg.
ReplyDeleteI want to share attachments related to a record of a custom object that I am sharing but, the problem is the attachments are not stored in the attachment object itself they are stored in content document for my case.