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

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

Getting SObject Record Type Info in Salesforce with Apex

ยท

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.

Did you find this article valuable?

Support shubham lashkan by becoming a sponsor. Any amount is appreciated!

ย