13

What is best practice for working with RecordType in Apex (using DeveloperName)? Why should I use SOQL with DeveloperName if I can do it without query using regular name:

Schema.SObjectType.Account.getRecordTypeInfosByName().get('xxx').getRecordTypeId();

vs

[SELECT Id FROM RecordType WHERE DeveloperName ='xxx'].Id;

Both Name and DeveloperName are changed when editing Name.

  • 4
    if you are worried about soql, use a lazy loading utility class that retrieves all recordtypes into a static map for subsequent lookup via a Util method. This way, only one soql is burned and only when needed – cropredy Mar 14 '17 at 1:10
15

Actually, that is not quite true. Name is the field that the user sees when they choose it in the screen (or on the Page Layout). Developer Name is the API name for the record.

This is very similar to the way the Label and the API Name work for fields and object.

Because of this, it is best practice to always use Developer Name, since it is less likely to change (and it is unique in the object). As a dev, you can easily rename the Name to adjust to changing business requirements, but need not rename the Developer Name and leave your code working just fine.

Per the Docs:

Developer Name

Required. The unique name of the object in the API. This name can contain only underscores and alphanumeric characters, and must be unique in your org. It must begin with a letter, not include spaces, not end with an underscore, and not contain two consecutive underscores. In managed packages, this field prevents naming conflicts on package installations. With this field, a developer can change the object’s name in a managed package and the changes are reflected in a subscriber’s organization. Label is Record Type Name.

Name

Required. Label of the record type in the user interface. Limit: 80 characters. Label is Record Type Label.

You can read the entire explanation here, in SFDC documentation.

  • 1
    So I believe Salesforce has to add ability to get Id by DeveloperName without query. Because sometimes extra query can be serious issue. – Vladyslav K Mar 13 '17 at 17:07
  • You could use getRecordTypeInfos(); and iterate. No new soql queries needed. – Sebastian Kessel Mar 13 '17 at 17:09
  • But there is no DeveloperName in RecordTypeInfo class – Vladyslav K Mar 13 '17 at 17:11
  • 1
    Huh, so right you are. Very strange indeed. I hope this gets remedied soon. – Sebastian Kessel Mar 13 '17 at 17:12
  • 3
    The cool thing about RecordType.Name is: Account myAccount = new Account( Name="TestAccount", RecordType = new RecordType(Name="myRecordTypeName)); insert myAccount – Christian Szandor Knapp Mar 13 '17 at 17:38
5

Developer name is optional, usually label changes quite frequently.

Best practice is to always use developer name to get recordtype Id.

5

Use a unique identifier wherever possible to query for vital configuration data. In this case, DeveloperName is unique, where Name is not. So there is a clear and compelling case to use the former.

2

Another reason to use the DeveloperName is that the Name is subject to translation and could generate problems if your org has user in multiple locales

0

Just writing this for anybody who comes through one of the search engines: Salesforce recently released the getRecordTypeInfosByDeveloperName() function. So you can write:

Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('xxx').getRecordTypeId();

With xxx beeing the save Developer Name (as other answers already pointed out). More Information here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject_describe.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.