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

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

```java
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.

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

## getRecordTypeInfos Method

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

```java
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.

```java
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](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_Schema_RecordTypeInfo.htm).

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

```java
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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711790772501/41e721ca-7345-41d6-958c-fb2dec4bb3eb.png align="center")

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

%[https://www.youtube.com/watch?v=yj7c_0MtdIw] 

## 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**](https://blog.shubhamlashkan.com/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**](https://twitter.com/ShubhamLashkan).

Learning Salesforce then do checkout Salesforce Verse: [https://salesforceverse.com](https://salesforceverse.com)

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