Sunday, 10 June 2012

Salesforce to Salesforce Attachments Documents and Tasks

Ive blogged about Salesforce to Salesforce before and how to programmatically pass records from 1 org to another, but I wanted to blog about specifically how to pass over certain records, such as Attachments and Tasks etc

        list<PartnerNetworkRecordConnection> newrecords = new list<PartnerNetworkRecordConnection>();
        List<PartnerNetworkConnection> connMap = new List<PartnerNetworkConnection>();
        connMap=[select Id, ConnectionStatus, ConnectionName from PartnerNetworkConnection where ConnectionStatus = 'Accepted' and ConnectionName=<<connection>> LIMIT 1] ;
Account[] acc =[Select id From Account where Name = <<account>> limit 1];
Attachment[] attaches =[Select id,IsPartnerShared,Name  From Attachment where Parentid=: acc[0].id and IsPartnerShared=false limit 1];

               if (attaches.size() > 0){
                   Set<id> newfunctions = new Set<id>();
                   for(Attachment thisattach: attaches) {
            PartnerNetworkRecordConnection newrecord = SearchFunctionUtils.addPartnerNetworkRecordConnection(connMap);
                       if (!Test.isRunningTest()){
                        newrecord.LocalRecordId = acc[0].id ;
newrecord.RelatedRecords = 'Attachment';
                       newfunctions.add(;//Attachments to be uploaded
                   insert newrecords;

The key difference is you have to set RelatedRecords and the LocalRecordId  is not the id of the Attachment but Id of the Account. There are also other standard objects which also require to use this method, which I have found are poorly documented. The best way of identifying which objects is probably by trial and error, but the Document object is another such object.

For Tasks SendClosedTasks and SendOpenTasks properties are important.

Seefor more details


  1. Hi Steve, we are having a problem that an attachment does not always attach to a record. It is if the record has not been "commited" by salesforce and then it wants to add the attachment to a unknown record. Do you know how we can solve this issue?We where thinking of adding a delay to add the attachement.

  2. Glad to help but I usually only engage with people that are regular people on my blog so would be good if you sign up to my blog first

  3. Please sign up to my blog and I'll happily help you

  4. This comment has been removed by a blog administrator.

  5. This comment has been removed by a blog administrator.


Note: only a member of this blog may post a comment.