Showing posts with label LimitException. Show all posts
Showing posts with label LimitException. Show all posts

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.