Showing posts with label Id. Show all posts
Showing posts with label Id. Show all posts

Tuesday, March 19, 2013

Apex, check object by Id. Ver 3.0

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.
public static Boolean isIdsObjectTypeEqualToStringObjectType( String objectIdString, 
                 String objectTypeNameString ){
 
 return ( ( objectIdString instanceof Id )  
             && Id.valueOf( objectIdString ).getSObjectType().getDescribe().getName().equals( objectTypeNameString ) );
}
Test class will be the same.

Enjoy.

Friday, November 23, 2012

Apex, check object by Id. Update.

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;
}
Test class will be the same.

Enjoy.

Sunday, October 28, 2012

Apex, check object by Id.

Some times we need to know if Id matches the object we need. This method returns true, if matches:
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.