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.

No comments:

Post a Comment