Salesforce Apex : how to get recordtype name by Id

Varun   Varun 1452 Views
Using apex sobject describe you can fetch easily recordType name by providing Id. If you are  making multiple call to this describe then you may hit limit.

Account.SObjectType.getDescribe().getRecordTypeInfosById().get('RECORD_TYPE_ID').getName()
Example which cache of previous fetches and avoid multiple calls.

public class UtilRecordTypes{
private UtilRecordTypes(){}
private static Map<Id,String> accountRecordTypeDetailsCache = null;

public static String getAccountRecordTypeNameById(Id recTyId){
    if(accountRecordTypeDetailsCache ==null){
              accountRecordTypeDetailsCache = new Map<Id,String>();
     }

     if(!accountRecordTypeDetailsCache.containsKey(recTyId)){
          accountRecordTypeDetailsCache.put(recTyId,
                       Account.SObjectType.getDescribe().
                      getRecordTypeInfosById().get(recTyId).getName());
      }
   
      return accountRecordTypeDetailsCache.get(recTyId);
}

public static String getsObjectRecordTypeNameById(String sObjectName,Id recTyId){

    if(accountRecordTypeDetailsCache ==null){
              accountRecordTypeDetailsCache = new Map<Id,String>();
     }

     if(!accountRecordTypeDetailsCache.containsKey(recTyId)){
          accountRecordTypeDetailsCache.put(recTyId,
                       Schema.getGlobalDescribe().get(sObjectName).
                      getRecordTypeInfosById().get(recTyId).getName());
      }
   
      return accountRecordTypeDetailsCache.get(recTyId);
}



}