To avoid SOQL Limit we can use dynamic apex to get RecordTypeId. We can get a particular RecordTypeId of a particular sObject or get all recordTypeid of one sobejct in a map.
I created a record type "NewLabel" on Account object. Enter the below Apex code in Execute Anonymous window and check the output:
Note that values of
Check the link below to check a utility class that performs the query against the RecordType object
https://interactiveties.com/blog/2016/recordtype-utility-class.php
global class RecordTypeUtils {
//Method to get the recordTypeId
public static Id recordTypeId(string obj,string recName){
Id recTypeId;
if(obj!= null && recName != null){
recTypeId= Schema.getGlobalDescribe().get(obj).getDescribe().getRecordTypeInfosByName().get(recName).getRecordTypeId();
}
return recTypeId;
}
//Method to get the map of recordType Name as key and recordTypeId as value
public static Map<String,Id> recordTypeMap(string obj){
Map<String,Id> recTypeNameWithIdMap=new Map<String,Id>();
if(obj!= null){
for(Schema.RecordTypeInfo recInfo : Schema.getGlobalDescribe().get(obj).getDescribe().getRecordTypeInfosByName().values()){
recTypeNameWithIdMap.put(recInfo.getName(),recInfo.getRecordTypeId());
}
}
return recTypeNameWithIdMap;
}
}
I created a record type "NewLabel" on Account object. Enter the below Apex code in Execute Anonymous window and check the output:
Map<String,Id> recTypeIdMap=RecordTypeUtils.recordTypeMap('Account');
System.debug(recTypeIdMap);
Id recId=RecordTypeUtils.recordTypeId('Account','NewLabel');
System.debug(recId);
Id recordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('NewLabel').getRecordTypeId();
System.debug(recordTypeId);
Note that values of
recId
and recordTypeId
in your output is same.Check the link below to check a utility class that performs the query against the RecordType object
https://interactiveties.com/blog/2016/recordtype-utility-class.php