Get Record Type ID Using Object Name

We can get the Object Record Type Id values in multiple ways like using SOQL, getDescribSObject and below are some examples:

Example 1:

//Replace Object Name (API Name) and Record Type Name (Record Type API)
private static Id businessAccountRecTypeId = null;
private static Id getEventRecodTypeId() {
if (businessAccountRecTypeId == null) {
businessAccountRecTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get(‘Business_Account’).getRecordTypeId(); //Business_Account RecordType API value from Account Object
}
return businessAccountRecTypeId;
}

Example 2:
//Replace Object Name (API Name) and Record Type Name (Not Record Type API)

Id businessAccountRecTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get(‘Business Account’).getRecordTypeId();

Example 3:
This way to get a recordTypeId using SOQL query in Apex
[Select id from RecordType where sObjectType = ‘Account’ and developerName =’Business_Account’].id

Important Note:
Salesforce Developer Tips: How to get a RecordType Id by Name without SOQL, use always as per example 1 without using SOQL Query to retrieve Object Record Type ID.

Useful Links:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_Schema_RecordTypeInfo.htm

 

 

(26)

Written by 

I am Sakthivel Madesh, a certified Salesforce Developer & Administrator working on Salesforce Technology since 2011. I am currently working in Techforce Services as Sr. Salesforce Developer. I have worked on multiple technologies Like PHP, Java, Salesforce, I am very passionate about Salesforce and to earn 13x Salesforce Certification. I love Trailhead for learning Salesforce Skill and Become a Trailhead Ranger too. I Love to Learn & Share my Salesforce knowledge to Salesforce Learners/Beginner using My Blog and Salesforce Community.

Leave a Reply