This is new version of method that I mentioned in my previous topics: 'Apex, check object by Id' and 'Apex, check object by Id. Update.'. Earlier, if Id.valueOf( objectIdString ) == null we had an error 'FATAL_ERROR System.StringException: Invalid id'. To avoid this error I use 'instanceof'. So now will not have any errors, even if we do not have such object in system like 'objectTypeNameString' or it will be null.
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:
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 update to my previous topic 'Apex, check object by Id'. In Apex version 26 I found a better solution than go though the List of Schema. Id now have method called getSObjectType(). We can use it to get name of the object. And compare it to existing one. getSObjectType() gives us Schema.SObjectType.
public static Boolean isIdsObjectTypeEqualToStringObjectType( String objectIdString,
String objectTypeNameString ){
Boolean isEqual = false;
if( Id.valueOf( objectIdString ).getSObjectType().getDescribe().getName().equals( objectTypeNameString ) ){
// getSObjectType().getDescribe().getName() gives us String that we can compare to incoming object type.
isEqual = true;
}
return isEqual;
}
Everytime when we write test classes, we need to a create a big number of data to apply them to the methods that we are testing. We create a lot of methods leading to the creation of these or those objects. I have created the method leading to the creation of any type objects we need:
public with sharing class Utilities {
/*
Method for creating any Object
*/
public static List<sObject> createObject(Integer numberToCreate,
Boolean doInsert,
Schema.SObjectType objectType,
Map<String, String> fieldsMap){
List<sObject> objectsList = new List<sObject>();
List<String> objectFields = new List<String>();
List<String> fieldsList = new List<String>();
objectFields.addAll(objectType.getDescribe().fields.getMap().keySet());
fieldsList.addAll(fieldsMap.keySet());
if(numberToCreate > 0){
for( Integer countOfObjects = 0; countOfObjects < numberToCreate; countOfObjects++ ){
SObject currentObject = objectType.newSObject();
for(String fieldFromList : fieldsList){
for(String fieldFromObject : objectFields){
if(fieldFromObject == fieldFromList){
currentObject.put(fieldFromList , fieldsMap.get(fieldFromList));
}
}
}
objectsList.add(currentObject);
}
if ( doInsert ) insert objectsList;
}
return objectsList;
}
}
Also I have added test class for it:
@isTest
private class TestUtilities {
private static final Integer TEST_RECORD_COUNT = 10;
static Map<String, String> fieldsMap;
static Schema.SObjectType objectType;
static List<String> objectFields = new List<String>();
static String query;
private static void setup(){
objectType = Account.getsObjectType();
objectFields.addAll(objectType.getDescribe().fields.getMap().keySet());
fieldsMap = new Map<String, String>{'Name' => 'Test', //Fill in fields
'BillingCountry' => 'Usa', //IMPORTANT You need to fill in requaired fields
'AccountSource' => 'Web'};
}
private static testmethod void testCreateObject(){
List<sObject> testCreatedObjects;
List<sObject> objectsFromDB;
List<String> fieldsFromBD = new List<String>();
setup();
Test.startTest();
testCreatedObjects = Utilities.createObject(TEST_RECORD_COUNT,
true,
objectType,
fieldsMap);
Test.stopTest();
query = 'SELECT Id';
fieldsFromBD.addAll(fieldsMap.keySet());
for(String fieldDB : fieldsFromBD){
for(String fieldObject : objectFields){
if(fieldDB == fieldObject){
query += ', ' + fieldDB;
}
}
}
query += ' FROM ' + objectType + ' WHERE Id IN : ' + 'testCreatedObjects' + ' ';
objectsFromDB = Database.query(query);
System.assertEquals( TEST_RECORD_COUNT , objectsFromDB.size() );
}
}