Hello Everyone, In this port, we are going to see, how we can get salesforce object recordtype dynamically using apex. Record types let you offer different business processes, picklist values, and page layout to different users. So let's begin,

Get record type info dynamically in Salesforce

1. First we are going to see how we get record types using soql query.

 SELECT Id,Name from RecordType WHERE SobjectType='Account' 

In "SobjectType" where condition, you can use any Sobject whether it's custom or standard.

2. In this point, I am going to show you how we get a particular record type id by using it's label or it's name (Developer Name) .

Syntex:

 Schema.SObjectType.SObjectAPIName.getRecordTypeInfosByName().get('record type label').getRecordTypeId();
Schema.SObjectType.SObjectAPIName.getRecordTypeInfosByName().get('record type label').getName();
Schema.SObjectType.SObjectAPIName.getRecordTypeInfosByName().get('record type label').getDeveloperName();
OR
Schema.SObjectType.SObjectAPIName.getRecordTypeInfosByDeveloperName().get('record type name').getRecordTypeId();
Schema.SObjectType.SObjectAPIName.getRecordTypeInfosByDeveloperName().get('record type name').getName();
Schema.SObjectType.SObjectAPIName.getRecordTypeInfosByDeveloperName().get('record type name').getDeveloperName();
OR
Schema.SObjectType.SObjectAPIName.getRecordTypeInfosById().get('record type Id').getRecordTypeId();
Schema.SObjectType.SObjectAPIName.getRecordTypeInfosById().get('record type Id').getName();
Schema.SObjectType.SObjectAPIName.getRecordTypeInfosById().get('record type Id').getDeveloperName();

In above code syntex, replace "SobjectAPIName" with your desired salesforce sobject.

By Record Type Label:

 Id recordTypeId=Schema.SObjectType.Account.getRecordTypeInfosByName().get('Account').getRecordTypeId();
OR
String Name=Schema.SObjectType.Account.getRecordTypeInfosByName().get('Account').getName();
OR
String DevName=Schema.SObjectType.Account.getRecordTypeInfosByName().get('Account').getDeveloperName();   

 

By Record Type Name:

 Id recordTypeId=Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Account').getRecordTypeId();
OR
String Name=Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Account').getName();
OR
String DevName=Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Account').getDeveloperName(); 

By Record Type Id:

 Id recordTypeId=Schema.SObjectType.Account.getRecordTypeInfosById().get('0120K000001R5VRQA0').getRecordTypeId();
OR
String Name=Schema.SObjectType.Account.getRecordTypeInfosById().get('0120K000001R5VRQA0').getName();
OR
String DevName=Schema.SObjectType.Account.getRecordTypeInfosById().get('0120K000001R5VRQA0').getDeveloperName();


Hope you like this post, for any query or suggestions please feel free to comment. 

Thank you.