function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Raghav TRaghav T 

How to Send/Share the Case with related attachments with ParnterNetworkConnection Apex code?

Hi there,
I have been trying to share records from one org to another through SF-To-SF connection, although i've been able to share the Case record but unable to share the related Attachments to  it.
I have also done the setup for that like Publish and Subscribe the Attachments on both the side. But still its not working. Is there any other way round to do this, or am i doing it incorrectly?
Code :
global class SFConnectionFromAToB implements Database.Batchable<sObject> {
    global Date fromDate= date.newInstance(2023,01,09);
    global Date toDate;
    global List <PartnerNetworkRecordConnection> prncList = new List<PartnerNetworkRecordConnection>();
    global Id connectionId = [Select Id, ConnectionStatus, ConnectionName From PartnerNetworkConnection Where ConnectionName = 'MyConnection' AND ConnectionStatus = 'Accepted'].Id;
    
    global Database.QueryLocator start(Database.BatchableContext BC){
        system.debug('inside start');
        String queryString = 'SELECT Id, (SELECT Id FROM Attachments), (SELECT Id FROM CaseComments), (SELECT Id FROM Events), (SELECT Id FROM Tasks) FROM CASE WHERE Id=\'5002i00000D67ybAAB\'';
        system.debug('inside start '+queryString);
      	return Database.getQueryLocator(queryString);
   }

   global void execute(Database.BatchableContext BC, List<sObject> scope){
       system.debug('inside exec '+scope);
       
       for(Case cs : (List<Case>)scope){
           prncList.add(new PartnerNetworkRecordConnection(
               ConnectionId = connectionId,
               LocalRecordId = cs.Id,               
               SendClosedTasks = true,
               SendOpenTasks = true,
               SendEmails = true));
           
       		if(!cs.Attachments.isEmpty()){
               for(Attachment att : cs.Attachments){
                   prncList.add(new PartnerNetworkRecordConnection(
                       ConnectionId = connectionId,
                       ParentRecordID = cs.Id,
                       LocalRecordId = att.Id
					   SendEmails = true));
               }
           }
       }
       
       if(!prncList.isEmpty()){

            try{
                system.debug('prncList : '+prncList);
                upsert prncList;
            }
            catch(System.Dmlexception dmlExceptionInstance){
                System.debug('Record Share Error:' + dmlExceptionInstance.getMessage());
            }
        }
   }

   global void finish(Database.BatchableContext BC){
       
   }
}
Img :
Publishsubscribe
 
SwethaSwetha (Salesforce Developers) 
HI Raghav,
Are you using the Pub/Sub API here? Do you have any information in the event logs for the failure of attachments?

Thanks