When you work with Salesforce, many objects (SObjects) have record types to help you categorize records with different business processes or page layouts. Sometimes, as a developer, you want to retrieve all record types of a particular object dynamically in Apex without hardcoding anything.
In this post, we’ll learn how to write a simple Apex method that:
Takes the API name of an SObject (like
Account
,Case
, orContact
)Returns a list of all record type names (except the default ‘Master’ record type)
Uses the Schema class’s metadata methods, making it more flexible and efficient than querying the database
This is a great way to build dynamic components or logic that depend on record types without updating code every time a new record type is added.
🧠 What Are We Trying to Do?
Imagine you have a Visualforce page or Lightning component that lets users pick a record type for creating a new record. You want to show the list of all available record types for that SObject.
Our method will:
Use Schema Describe methods to get metadata about the SObject
Grab all record types via
getRecordTypeInfosByDeveloperName()
Filter out the ‘Master’ record type (which is the default, system one)
Return a list of record type names that you can display or use elsewhere
🔍 Code Explanation
public with sharing class ApexUseCaseTwentyNine {
public static List<String> retrieveRecordTypes(String sobjectName){
We start by defining a class called ApexUseCaseTwentyNine
and a static method retrieveRecordTypes
that accepts the SObject’s API name as a string and returns a list of record type names.
List<String> recordTypeNames = new List<String>();
We create an empty list to store record type names.
Map<String, Schema.RecordTypeInfo> recordTypeInfos = Schema.getGlobalDescribe()
.get(sobjectName).getDescribe().getRecordTypeInfosByDeveloperName();
Using Schema.getGlobalDescribe()
, we fetch the metadata for the given SObject name. Then, we call getRecordTypeInfosByDeveloperName()
which returns a map where the keys are developer names of record types and the values are RecordTypeInfo
objects.
for(String devName : recordTypeInfos.keySet()){
if(devName != 'Master'){
recordTypeNames.add(recordTypeInfos.get(devName).getName());
}
}
We loop through the developer names, skip the default ‘Master’ record type, and add the friendly name (getName()
) of each record type to our list.
return recordTypeNames;
}
}
Finally, we return the list of record type names.
✅ Why This Is Useful
No SOQL needed: This method uses metadata, so it won’t count against your governor limits.
Dynamic: Works for any SObject — no need to update code if record types change.
Great for UI: Perfect for building picklists or dropdowns that show record type choices.
Cleaner & safer: Avoids hardcoding record type names or IDs.
🧾 Final Code Snippet
public with sharing class ApexUseCaseTwentyNine {
public static List<String> retrieveRecordTypes(String sobjectName){
List<String> recordTypeNames = new List<String>();
Map<String, Schema.RecordTypeInfo> recordTypeInfos = Schema.getGlobalDescribe()
.get(sobjectName).getDescribe().getRecordTypeInfosByDeveloperName();
for(String devName : recordTypeInfos.keySet()){
if(devName != 'Master'){
recordTypeNames.add(recordTypeInfos.get(devName).getName());
}
}
return recordTypeNames;
}
}
🎥 Watch It in Action!
Prefer video tutorials? Watch this step-by-step demo showing how to get record types dynamically in Apex and how to use the returned list to create a Lightning dropdown.