0

I have established a connection between two salesforce org using S2S - https://developer.salesforce.com/page/An_Introduction_to_Salesforce_to_Salesforce

I am trying to use a programatic approach to share the records between the two org. I am running the below code in execute anonymous of the source org to share a record of an object named Asset__c. THis is just to test the connect whether the records get updated in the target org automatically.

Asset__c a = [select id from Asset__c where id='a0046000001iciD'];

PartnerNetworkConnection network = [select Id, ConnectionStatus, ConnectionName from PartnerNetworkConnection
where ConnectionStatus = 'Accepted'];

PartnerNetworkRecordConnection newConnection =
                    new PartnerNetworkRecordConnection(
                        ConnectionId = network.Id,
                        LocalRecordId = a.id,
                        SendClosedTasks = false,
                        SendOpenTasks = false,
                        SendEmails = false);
upsert newConnection;

But my above code makes no change to the record in the target org. Am I missing something here or is it not the right way to execute the code?

Any suggestions?

6
  • Did you query PartnerNetworkRecordConnection with LocalId = 'a0046000001iciD'? What status does it have?
    – EvAzi
    Commented Mar 22, 2017 at 17:38
  • I believe a0046000001iciD is the id of the object which I am trying to send to target object. Connection status is accepted for the PartnerNetworkConnection . Is that what you are asking?
    – SfdcBat
    Commented Mar 22, 2017 at 17:41
  • No, please query this: SELECT Id, Status FROM PartnerNetworkRecordConnection WHERE LocalRecordId = 'a0046000001iciD'
    – EvAzi
    Commented Mar 22, 2017 at 17:44
  • The Status shows as Sent. Does my code look correct to send the record?
    – SfdcBat
    Commented Mar 22, 2017 at 17:48
  • Your code looks correct, but it should have Active (sent) status in order for partner organization to receive the record. Could you check if there's erros on partner organization on connection record?
    – EvAzi
    Commented Mar 22, 2017 at 17:59

2 Answers 2

2

Here are two scenarios

Let's take an example - You are sharing Account (Dollar Tree - 001B000000LvK1D) and an Opportunity (Dollar Tree - 006B0000001f6Dr).

Now there are two things possible

1. You have already shared parent record last week, and sharing opportunity today
2. You are sharing both parent and child in the same transactions

Let's discuss code for the 1st scenario, to auto relate child with parent record (that is already shared add ParentRecordId = '001B000000LvK1D' field in your code). Use query to get parent id in source org

Opportunity a = [select id from Opportunity where id='006B0000001f6Dr'];

PartnerNetworkConnection network = [select Id, ConnectionStatus, ConnectionName from PartnerNetworkConnection where ConnectionStatus = 'Accepted'];

PartnerNetworkRecordConnection newConnection = new PartnerNetworkRecordConnection( ConnectionId = network.Id, LocalRecordId = a.id, SendClosedTasks = false, SendOpenTasks = false, SendEmails = false, ParentRecordId = '001B000000LvK1D'); upsert newConnection;

Let's discuss code for the 2nd scenario, where you want to share the child records while sharing the parent record to do this add RelatedRecords field in your code.

where RelatedRecords is nothing but "A comma-separated list of API names for child records to be shared with a parent record."

RelatedRecords = 'Contact,Opportunity,Orders__c';

6
  • Thank you for a detail response. In the fields scenerio what if there are many parents for that child record. How can I specify all the ParentRecordID in the code?
    – SfdcBat
    Commented Mar 22, 2017 at 20:36
  • Use a global list to store the multiple PartnerNetworkRecordConnection connection for each parents
    – J Doe
    Commented Mar 22, 2017 at 21:17
  • Thank you! Is there a way I can get the details of what records were sent from source to target system. What happens if there is an error inserting the records? Is there any logs present?
    – SfdcBat
    Commented Mar 22, 2017 at 23:43
  • 1
    refer PartnerNetworkSyncLog object. It represents the Org Sync Log tab in Salesforce, where Salesforce administrators can track the replication of record inserts and updates being performed in Organization Sync. The Connection Detail page for the replication connection also displays the Org Sync Log’s twenty most recent entries, and provides a link to the log.
    – J Doe
    Commented Mar 22, 2017 at 23:50
  • Appreciate your effort in providing detail information.
    – SfdcBat
    Commented Mar 23, 2017 at 17:29
1

Make sure that you have setup S-2-S connection correctly

  1. For Asset__c this, from source org-publish it and subscribe it from target org.

  2. Repeat the above steps from target to source org.

  3. Last make sure that auto accept the records from connection.

7
  • Thanks. It works fine when I do not use programmatic approach. But with running the code it does not work the way expected. I am using the code approach only to update/Insert the lookup field as well. Since lookup field cannot be mapped if code approach is not used.
    – SfdcBat
    Commented Mar 22, 2017 at 18:16
  • The above code is working for me. To verify it go to the Target org and create a list (Last modifiedDate = Toda)
    – J Doe
    Commented Mar 22, 2017 at 18:30
  • are you able to map a lookup field without using code? I believe you cannot update a lookup field with out using the code. I am not sure how I can refer to the parent record when I am inserting a child record
    – SfdcBat
    Commented Mar 22, 2017 at 18:32
  • It means you are sharing both parent and child records?
    – J Doe
    Commented Mar 22, 2017 at 18:41
  • Yes. The target has subscribed to both child and parent. But when a new child is created in target it does not have that lookup field filled with parent.
    – SfdcBat
    Commented Mar 22, 2017 at 18:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .