Blog

Work with Salesforce To Salesforce (S2S) in APEX

March 11, 2015

In SF there are two objects responsible for S2S connection: PartnerNetworkConnection – keeps all information about current connection and PartnerNetworkRecordConnection – holds information about record that was forwarded. I will not write here how to create S2S connection, there is a lot of information about in internet. We will talk about forwarding objects that are already published and subscribed in both orgs. We can forward objects in two ways: manualy in page or in code. Below is sample of class to do this in code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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 Salesforce 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.

 

Tags

CoreValueSalesforceTechnical

Share


0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Articles

Part 2: Optimizing and scaling microservices. Organic growth of eco-systems.

October 11, 2017 | Nikola Krastev

A microservices approach is not a silver bullet for all software architecture problems. It introduces tradeoffs and challenges of its own. However, process gains and improvements in human performance have been considered to be worth the overhead in technology. Here are some general arguments against using sophisticated SOA. Server performance and overhead in communication By […]

CoreValue President at IT Arena 2017

September 27, 2017

We are pleased to announce that CoreValue president Igor Kruglyak will participate in IT Arena 2017, to be held in Lviv, Ukraine, September 29 – October 1, 2017.