How to fetch record type information of an SObject using Apex in Salesforce?

shubham lashkanshubham lashkan
2 min read

In this article, we will look at code example that demonstrates how to retrieve record type information of Account Object using Apex in Salesforce.

Map of String, Id

Map<String,Id> recordType = new Map<String,id>();

Here we have created a map named recordType of String, Id which will use later to store record type name along with Id.

Account Object Metadata

We use the getDescribe() method on the SobjectType to retrieve the metadata for the Account object. The getDescribe() method returns a DescribeSObjectResult object.

Schema.DescribeSObjectResult sobjectResult = Account.SObjectType.getDescribe();

getRecordTypeInfos Method

A RecordTypeInfo object is returned from the sObject describe result using the getRecordTypeInfos method.

List<Schema.RecordTypeInfo> recordTypeInfo = sobjectResult.getRecordTypeInfos();

List all RecordType

We then loop through the result of getRecordTypeInfo() method to store the name and id of record type in the map named recordType which we created earlier. Here we use getName() method to find the name of record type and getRecordTypeId() method to find the id of record type.

for(Schema.RecordTypeInfo rt:recordTypeInfo){
    recordType.put(rt.getName(),rt.getRecordTypeId());
}

To find more information about the available methods check out RecordTypeInfo class from Apex Reference Guide.

Here is the complete code snippet which fetches all record type of the Account Object.

Map<String,Id> recordType = new Map<String,id>();
Schema.DescribeSObjectResult sobjectResult = Account.SObjectType.getDescribe();
List<Schema.RecordTypeInfo> recordTypeInfo = sobjectResult.getRecordTypeInfos();
for(Schema.RecordTypeInfo rt:recordTypeInfo){
    recordType.put(rt.getName(),rt.getRecordTypeId());
}
system.debug(recordType);

When you execute above code from anonymous window and open debug logs you will see a map of all record type of account object.

When i executed the above code i saw 2 record type because i have created dealer record type and master is the default one.

Also, check out the below video on How to fetch record type information using dynamic apex in salesforce.

Conclusion

In short you can use RecordTypeInfo class to find all information related to record type using dynamic apex in salesforce.

I hope you find this article helpful.

Please don't forget to like, comment and share and you can also check out my other articles. Also, don't forget to subscribe to my newsletter so you can be notified about new articles.

I am looking to get into technical writing so if you want me to write an article then please connect with me on my Twitter handle here.

Let me also know which topic I should write about next.

0
Subscribe to my newsletter

Read articles from shubham lashkan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

shubham lashkan
shubham lashkan

I am a Software Engineer currently working as Salesforce Developer with hands-on experience in Apex development along with declarative development.