Get all record types available on sObject in APEX

Suppose that you want to get all record types of any standard or custom object in APEX class , here is the simplest way to solve your problem.

I have created two record types on Contact Object , Master is the default record type  available on every Object.

record types on contact object

Get All record Type

Here we are using Schema class to find record type , the below code will return all Standard and Custom record types available on Contact Object.

				
					Schema.DescribeSObjectResult describe =Schema.getGlobalDescribe().get('Contact').getDescribe(); 
Schema.RecordTypeInfo defaultRecordType;
for(Schema.RecordTypeInfo recordType : describe.getRecordTypeInfos()) {
    System.debug( recordType.getDeveloperName());
}
				
			

Get only Custom Record types

Given code help you to find all Custom record types of Contact Object .

				
					Schema.DescribeSObjectResult describe =Schema.getGlobalDescribe().get('Contact').getDescribe(); 
Schema.RecordTypeInfo defaultRecordType;
for(Schema.RecordTypeInfo recordType : describe.getRecordTypeInfos()) {
    if(recordType.isDefaultRecordTypeMapping()) {
        System.debug( recordType.getDeveloperName());
    }
   
}
				
			

Get Id of record type

To get the Id of record type use getRecordTypeId()  method .

				
					System.debug( recordType.getDeveloperName());
				
			

I hope this blog help you to learn something new . Please share with your Coders friends , don’t forget to subscribe our Youtube channal.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top