Showing posts with label S2S. Show all posts
Showing posts with label S2S. Show all posts

Monday, July 22, 2013

Re-connecting records through S2S connection.

How to reconnect two records that where connected with S2S connection, and it was deleted? Or, for example, we have two records in two org, how to connect them with S2S without creation new ones? This is quick quid how to do it.

We have two orgs: Org1 and Org2, and two Accounts: "Salesforce.com Inc." from Org1 and "Salesforce.com Inc." from Org 2. We need to synchronize with S2S connection. We will share from Org1 to Org2.
  1. Going to Org2 "Connections" tab:
  2. Selecting needed connection;
  3. Press "Subscribe/Unsubscribe":
  4. Uncheck "Auto-Accept Accounts":
  5. Going to Org1 to needed Account and share it:
  6. Going to Org2 Account tab;
  7. In the bottom there is block "Accounts from Connections", select "All" and press "Go":
    Accounts from Connections
  8. You will see records list that were forwarded but not accepted:
  9. Press "Accept" near Account record sent from Org1;
  10. Go to "Create New or Link to Existing One" and select "Use Existing":
  11. Search you existing Account and press "Save".
Now Account from Org1 is synchronized with Account from Org2.

P.S.: Do not forget to go back to "Connection" tab in Org2 and check again "Auto-Accept" Accounts.

Sunday, December 23, 2012

Check if object is shareable through the S2S connection.

Today while building the page for S2S connection and record management, I have faced with issue of the necessity to filter object types that are available for sharing. It's inconvenient that PartnerNetworkConnection object does not have information what exact objects are published/subscribed in connection. While investigating this issues, I found out that the objects that can be shared have two similar fields: ConnectionSentId and ConnectionReceivedId. So to form a list of object types that are available for sharing we need to go through all objects and check up if their fieldset contains these fields. As in the example:
Map<String, Schema.SObjectType> globalDescrideMap = Schema.getGlobalDescribe();
List<Schema.SObjectType> objectsForConnectionList = new List<Schema.SObjectType>();

for( Schema.SObjectType objectItem : globalDescrideMap.values() ){
    if( objectItem.getDescribe().fields.getMap().get('ConnectionSentId') ) != null ){
        objectsForConnectionList.add( objectItem );
    }
}
But in our case we have more than 100 objects that are available for sharing and this code gets an error:
System.LimitException: Too many fields describes: 101
Thus we can create one example of the object and try to add these fields. If the object can be shared we will have the error that this field is not writeable:
System.SObjectException: Field ConnectionSentId is not editable
In case this object is not shareable the error will be:
System.SObjectException: Invalid field ConnectionSentId for Training_Class_Attendee__History
This is the code sample:
Map<String, Schema.SObjectType> globalDescrideMap = Schema.getGlobalDescribe();
List<Schema.SObjectType> objectsForConnectionList = new List<Schema.SObjectType>();

for( Schema.SObjectType objectItem : globalDescrideMap.values() ){
 try{
        SObject currentObject = objectItem.newSObject();
  currentObject.put( 'ConnectionSentId', null);
    }catch( SObjectException soExpt ){
        if( soExpt.getMessage() == 'Field ConnectionSentId is not editable' )
            objectsForConnectionList.add( objectItem );
    }
}
If you have any better solution for this issue, please comment.
Enjoy.

Sunday, October 28, 2012

Work with Salesforce To Salesforce (S2S) in APEX.

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:
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.

Issue with Logs and System.debug in Salseforce 2 Salseforce (S2S) connection.

The issue is that we cannot see any logs or System debugs when work is being done under S2S connection. For example we have Salseforce 2 Salesforce connection. In one of instances , lets call it a guest, we have trigger that fires before Object insert (or after). We add new object to main instance, and connection creates the same object on the guest. Salseforce does not let us see any debug logs while doing something under S2S connection. So what to do if we need to "debug" triggers and methods in connection? We tried to solve this issue with creating a temporary field in the Object. Something like "LogField" - Long Text Area. In triggers and classes add code e.g.:
Object.Log__c  = '@someVariable: '   someVariable   '@';
After the work is done we can see "logs" in LogField of the Object. Also if there were some errors while running the trigger or with creating the Object you can always see them on Connection page in Logs.

Enjoy.