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;
@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.' );
}
}
}
}
}
Note: if there will be an error, in upserting one the record, all bunch will not be forwarded.
Also we do not need to forget about SF Governors and Limits. Because if we have triggers and some logic on forwarded records, in partner org, we can hit them. In this case we will not get error on Connection history, but all upserted PartnerNetworkRecordConnections will have status "Pending (sent)" and will not be forwarded.
To be able to forward records again we need to change status of PartnerNetworkRecordConnection from "Pending (sent)" to "Inactive". To do this we need to delete PartnerNetworkRecordConnection, but it will not be deleted, it will just change status to "Pending (sent)". After this we can insert tham again. If there will not be any errors, status will change to "Active (sent)" or "Connected" in several seconds. So if we need to change something in PartnerNetworkRecordConnection, we need to delete it and insert/upsert it again. This is strange way to work with, but it is only way.
One more thing. When we are forwarding records and filling in field ParentRecordId, we need to be sure that parent record is already forwarded, in other case we will have an error.
Useful links:
Enjoy.