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
Rajan Vegesna 3Rajan Vegesna 3 

we have 2 orgs and i need share records automatically from one org (partner object) to another org (partner object),when i testing by creating records manually i am getting below error, can any one help with this please.

Error: 
Apex trigger partnersync caused an unexpected exception, contact your administrator: partnersync: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: INVALID_PARTNER_NETWORK_STATUS, invalid status for partner network operation: []: Trigger.partnersync: line 19, column 1

Code:
trigger partnersync on Partner__c (after insert,after Update) {
    Id networkId = ConnectionHelper.getConnectionId('Academy of Financial Trading 2nd Org');
    List<PartnerNetworkRecordConnection> partnerConnections =  new  List<PartnerNetworkRecordConnection>();
    
    for (Partner__c pl : trigger.new) {
        
        PartnerNetworkRecordConnection newConnection = new PartnerNetworkRecordConnection(
            
            ConnectionId = networkId,
            LocalRecordId = pl.Id,
            SendClosedTasks = false,
            SendOpenTasks = false,
            SendEmails = false,
            ParentRecordId = pl.Id);
        partnerConnections.add(newConnection);
    }
    if (partnerConnections.size() > 0 ) {
        database.insert(partnerConnections);
        
    }
    
}
Rajan Vegesna 3Rajan Vegesna 3
This is working public class S2SService { public static final String CONNTECTION_STATUS_ACCEPTED = 'Accepted'; // Method return Id of current connection. public static Id getConnectionId( String connectionName ) { return [SELECT Id FROM PartnerNetworkConnection WHERE connectionStatus = : CONNTECTION_STATUS_ACCEPTED AND connectionName = : connectionName LIMIT 1].Id; } /* @param: objectList - list of objects you want to forward; @param: parentRecordIdMap - Map map of Parent records; @param: relatedRecordsMap - Map map with objects API names for whom forwarding record is parent and you want to forward them too; @param: connectionName - name of connection you want to use; */ public static void sendToPartnerOrg( List objectList, Map parentRecordIdMap, Map relatedRecordsMap, String connectionName ){ if ( objectList != null && !objectList.isEmpty() ){ // First thing we need to have connection name Id connectionId = getConnectionId( connectionName ); // Creating list of PartnerNetworkRecordConnection that will be inserted List objectConnections = new List(); // Going throught list of recordth we want to share for ( sObject obj: objectList ) // Adding new PartnerNetworkRecordConnection to list objectConnections.add( new PartnerNetworkRecordConnection( // ConnectionId - setting our PartnerNetworkConnection Id ConnectionId = connectionId, // LocalRecordId - Id of record we want to forward LocalRecordId = obj.Id, // SendClosedTasks and SendOpenTasks false because we do not wont to forward tham too SendClosedTasks = false, SendOpenTasks = false, SendEmails = true, // ParentRecordId - parent of forwarding record ParentRecordId = parentRecordIdMap != null ? parentRecordIdMap.get( obj.Id ): null, // RelatedRecords - child objects you what to forward too RelatedRecords = relatedRecordsMap != null ? relatedRecordsMap.get( obj.Id ): null ) ); // Upserting of PartnerNetworkRecordConnection list with linking error to the object // we are forwarding if ( !objectConnections.isEmpty() ) { try{ database.upsert( objectConnections ); }catch( DMLException dmlEx ){ for ( Integer i = 0; i < dmlEx.getNumDml(); i++ ) if( dmlEx.getDmlStatusCode( i ) == 'INVALID_PARTNER_NETWORK_STATUS' ) for( PartnerNetworkRecordConnection connectionItem: objectConnections ) if( dmlEx.getDmlId( i ) == connectionItem.Id ) for( sObject obj : objectList ) if( connectionItem.LocalRecordId == obj.Id ) obj.addError( 'Incorrect partner network status.' ); } } } } }
Tomeka WrayTomeka Wray
Rajan,
Are you saying that the code that you posted above is working? You can now share the record using salesforce to salesforce automatically?
Rajan Vegesna 3Rajan Vegesna 3
Use this class to share records from salesforce to sakesforce

public class S2SService {
    public static final String CONNTECTION_STATUS_ACCEPTED = 'Accepted';
    // Method return Id of current connection. 
    public static Id getConnectionId( String connectionName ) {
        return [SELECT Id 
                FROM PartnerNetworkConnection 
                WHERE connectionStatus = : CONNTECTION_STATUS_ACCEPTED 
                AND connectionName = : connectionName 
                LIMIT 1].Id; 
    } 
    /* 
@param: objectList - list of objects you want to forward; 
@param: parentRecordIdMap - Map<Record Id; Parent Id> map of Parent records;
@param: relatedRecordsMap - Map<Record Id; String with related records String>
map with objects API names for whom forwarding record
is parent and you want to forward them too; sObject
@param: connectionName - name of connection you want to use; 
*/
    public static void sendToPartnerOrg( List<sObject> objectList,
                                        Map<Id,Id> parentRecordIdMap, 
                                        Map<Id,String> relatedRecordsMap,
                                        String connectionName ){ 
                                            if ( objectList != null && !objectList.isEmpty() ){ 
                                                // First thing we need to have connection name
                                                Id connectionId = getConnectionId( connectionName ); 
                                                // Creating list of PartnerNetworkRecordConnection that will be inserted
                                                List<PartnerNetworkRecordConnection> objectConnections 
                                                    =  new  List<PartnerNetworkRecordConnection>(); 
                                                // Going throught list of recordth we want to share            
                                                for ( sObject obj: objectList ) 
                                                    // Adding new PartnerNetworkRecordConnection to list
                                                    objectConnections.add(  
                                                        new PartnerNetworkRecordConnection( 
                                                            // ConnectionId - setting our PartnerNetworkConnection Id
                                                            ConnectionId = connectionId, 
                                                            // LocalRecordId - Id of record we want to forward
                                                            LocalRecordId = obj.Id, 
                                                            // SendClosedTasks and SendOpenTasks false because we do not wont to forward tham too
                                                            SendClosedTasks = false,
                                                            SendOpenTasks = false,
                                                            SendEmails = true, 
                                                            // ParentRecordId - parent of forwarding record
                                                            ParentRecordId = parentRecordIdMap != null ?
                                                            parentRecordIdMap.get( obj.Id ): 
                                                            null,
                                                            // RelatedRecords - child objects you what to forward too
                                                            RelatedRecords = relatedRecordsMap != null ?
                                                            relatedRecordsMap.get( obj.Id ): 
                                                            null )
                                                    ); 
                                                // Upserting of PartnerNetworkRecordConnection list with linking error to the object
                                                // we are forwarding 
                                                if ( !objectConnections.isEmpty() ) {
                                                    try{ 
                                                        database.upsert( objectConnections ); 
                                                    }catch( DMLException dmlEx ){ 
                                                        for ( Integer i = 0; i < dmlEx.getNumDml(); i++ )
                                                            if( dmlEx.getDmlStatusCode( i ) == 'INVALID_PARTNER_NETWORK_STATUS' ) 
                                                            for( PartnerNetworkRecordConnection connectionItem: objectConnections ) 
                                                            if( dmlEx.getDmlId( i ) == connectionItem.Id ) 
                                                            for( sObject obj : objectList ) 
                                                            if( connectionItem.LocalRecordId == obj.Id ) 
                                                            obj.addError( 'Incorrect partner network status.' );
                                                    } 
                                                } 
                                            }  
                                        } 
}


below trigger to call above class

trigger partnersync on Partner__c (after insert,after Update) {
   List<sObject> sobj = new List<sObject>();
   sobj.add(new Partner__c());
   Map<Id,Id> parentMap = new Map<Id,Id>();
   Map<Id, String> childMap = new Map<Id, String>();
   S2SService.sendToPartnerOrg(Trigger.new,parentMap,childMap,'give your connection name here');
}


 
RAHUL MISHRA 86RAHUL MISHRA 86
Hi,
Were you able to solve this issue. Pleas provide pointers if you were. I just ran the following script in Execute Anonymous and got the error. WHat am i doing wrong?

List<PartnerNetworkRecordConnection> objectConnections =  new  List<PartnerNetworkRecordConnection>();   
            objectConnections.add( 
                new PartnerNetworkRecordConnection(
                    ConnectionId = '04PN000000000MMMAY', //Connection Id
                    LocalRecordId = '500N000000CqWr6IAF', //Case Id
                    SendClosedTasks = false,
                    SendOpenTasks = false,
                    SendEmails = true,
                    ParentRecordId = '003N000001Ko9ckIAB', //Contact Id
                    RelatedRecords = null ));

upsert objectConnections;
 
 
Line: 12, Column: 1
System.DmlException: Upsert failed. First exception on row 0; first error: INVALID_PARTNER_NETWORK_STATUS, invalid status for partner network operation: []