public static Boolean isIdsObjectTypeEqualToStringObjectType( String objectIdString,
String objectTypeNameString ){
Map<String, Schema.SObjectType> globalDescrideMap = Schema.getGlobalDescribe();
Boolean isEqual = false;
Map<String,String> keyPrefixMap = new Map<String,String>{};
for( String objectItem : globalDescrideMap.keySet() ){
Schema.DescribeSObjectResult objectDescribe = globalDescrideMap.get(objectItem).getDescribe();
keyPrefixMap.put( objectDescribe.getKeyPrefix(), objectDescribe.getName() );
}
if( keyPrefixMap.get( objectIdString.subString( 0, 3 ) ) == objectTypeNameString ) isEqual = true;
return isEqual;
}
Recommend to to move initialization of globalDescrideMap to top of class. And of course test:
@isTest( seeAllData = false ) private static void testIsIdsObjectTypeEqualToStringObjectType(){
Integer NUMBER_TO_INSERT = 5;
List<Account> testAccountList = new List<Account>();
List<Boolean> testEqualsList = new List<Boolean>();
List<Boolean> testNotEqualsList = new List<Boolean>();
String accountObjectType = Account.getsObjectType().getDescribe().getName();
for( Integer i = 0; NUMBER_TO_INSERT > i; i++ )
testAccountList.add( TestingUtils.createTestAccount( null ) );
insert testAccountList;
Test.startTest();
for( Account accountItem : testAccountList )
testEqualsList.add( BluewolfDevUtils.isIdsObjectTypeEqualToStringObjectType( ( String )accountItem.Id, accountObjectType ) );
for( Account accountItem : testAccountList )
testNotEqualsList.add( BluewolfDevUtils.isIdsObjectTypeEqualToStringObjectType( ( String )accountItem.Id, 'Account__c' ) );
Test.stopTest();
for( Boolean testEqualsItem : testEqualsList )
System.assertEquals( true, testEqualsItem );
for( Boolean testNotEqualsItem : testNotEqualsList )
System.assertEquals( false, testNotEqualsItem );
}
Enjoy.UTD: New method.