3

I used recordtypeId == '012A0000000rI1T' in a method to query if my Account recordtype "Practice Account" was what I wanted.

I've been reading that this is not a best practice and I should use something like:

Schema.SObjectType.Account.getRecordTypeInfosByName().get('Practice Account').getRecordTypeId()

My question is does getRecordTypeInfosByName() look at the developer name or the record type label? I can't find any info about which it looks at.

2

2 Answers 2

7

It's not entirely clear in the documentation on the gerRecordTypeInfosByName() method, but the documentation does make a mention of using the 'label'

getRecordTypeInfosByName()

Returns a map that matches record labels to their associated record type. The current user is not required to have access to a record type to see it in this map.

I don't know if there are any standard objects that have record type names that include a space, but I know that my org has a few.

Checking my codebase revealed the following line

Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Change Order').getRecordTypeId();

As API names cannot contain spaces, and I've had this particular line of code in production for coming up on 3 years, we can conclude that the map returned by getRecordTypeInfosByName() is indeed keyed on the record type's label.

1
16

getRecordTypeInfosByName() looks for Label.

After summer '18 preview, Salesforce is going to support getRecordTypeInfosByDeveloperName() delivered: https://developer.salesforce.com/docs/atlas.en-us.214.0.apexcode.meta/apexcode/apex_methods_system_sobject_describe.htm

Example: (Change your record type developer name in below example)

system.debug(Case.sObjectType.getDescribe().getRecordTypeInfosByDeveloperName().get('Customer_Success_Support').getRecordTypeId());
3
  • A sample code snippet will help. Commented May 10, 2018 at 18:35
  • 1
    Using my example you would do something like this: System.debug(Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Practice_Account').getRecordTypeId()); returns 012A0000000rI1TIAU Commented May 10, 2018 at 18:47
  • 1
    For using this "getRecordTypeInfosByDeveloperName()" you should maintain your class version atleast 43. Commented Oct 7, 2018 at 23:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .