Work with Salesforce To Salesforce (S2S) in APEX

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


Recent Articles

Get 100% Code Coverage for Salesforce Custom Metadata Based Decisions

January 18, 2018 | Bohdan Dovhan

How to obtain a full coverage for code which uses Custom Metadata for strategy-like decision implementation? Introduction Many applications use configuration data. Configuration data might be relevant to the entire organization, or a subset of user, or even different for each user. For the purposes of this article, we will focus only on global configuration […]

Logging of Exceptions in Salesforce

January 11, 2018 | Mykola Senyk

Unpredicted behaviour in a custom code. Can we eliminate it? The ability to customize your Salesforce org code is not just a “nice to have.”  It greatly increases the capability and flexibility of Salesforce. However, custom code can also be tricky to use. It would be great if we could detect unpredicted behavior in our […]

© Copyright - CoreValue 2018
Salesforce, Sales Cloud, and others are trademarks of salesforce.com, inc., and are used here with permission.
Used with permission from Microsoft.