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
SF7SF7 

S2S Trigger Issue

HI , I have a trigger on Opportunity for Sharing Records using Salesforce to Salesforce Connection.

When a opportunity is created Trigger fires and shares the Opportunity and Account to the target Org. Everything Works , i have  some triggers in Target Org to re-establish  Account and Opportunity As Salesforce to salesforce does not support lookup relationship.

Problem is My opportunities are cretaed by Lead Conversion and at that time my trigger fires and Opportunity Gets Created in Target Org and Related Account is also cretaed but the LookUp relation is not established. I believe the reson behind it is as Opportunity and Account are cretaed at the same time during lead Conversion trigger is not able to find the account to relate to Opportunity . how to handl this

Trigger autoforwardOpportunity on Opportunity(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(Opportunity opp: trigger.new){
   
        String acId = Opp.Id;
        System.debug('Value of OpportunityId: '+acId);
        for(PartnerNetworkConnection network : connMap) {
            String cid = network.Id;
            String status = network.ConnectionStatus;
            String connName = network.ConnectionName;
            String AccountName = Opp.Accountid;
            String AssignedBusinessUnit = Opp.Assigned_Business_Unit__c;
            System.debug('Connectin Details.......Cid:::'+cid+'Status:::'+Status+'ConnName:::'+connName+','+AssignedBusinessUnit);
            if(AssignedBusinessUnit!=Null && (AssignedBusinessUnit.equalsIgnoreCase('IT') || AssignedBusinessUnit.equalsIgnoreCase('Proservia'))) {    
                // Send account to IT instance
                PartnerNetworkRecordConnection newAccount = new PartnerNetworkRecordConnection();
                newAccount.ConnectionId = cid;
                newAccount.LocalRecordId = Opp.AccountId;
                newAccount.SendClosedTasks = true;
                newAccount.SendOpenTasks = true;
                newAccount.SendEmails = true;
                newAccount.RelatedRecords = 'Contact';
                System.debug('Inserting New Record'+newAccount);
                insert newAccount;
               
                // Send opportunity to IT instance
                PartnerNetworkRecordConnection newrecord = new PartnerNetworkRecordConnection();
                newrecord.ConnectionId = cid;
                newrecord.LocalRecordId = acId;
                newrecord.SendClosedTasks = true;
                newrecord.SendOpenTasks = true;
                newrecord.SendEmails = true;
                //newrecord.RelatedRecords = 'Contact';
               // newrecord.ParentRecordId = Opp.AccountId;                             If i use this trigger throws an error saying invalid status as there is no account yet.
                System.debug('Inserting New Record'+newrecord);
                insert newrecord;
            }
        }
    }
}

Dima.SmirnovDima.Smirnov
As for me here are several ways of solving the problem. In any case first what you need to do is remove all inserts from for loop.
Create one list for Account PNRC and insert it. After that connections need some time ( from 4 seconds up to 1 min ) to get connected. So I propose to create Opportunity PNRC list and insert it in @future method. If this will not help I propose to create batch that will share Opportunities after some period of time.