Working with Record Types

Get Record Type Id by Developer Name

Salesforce has finally provided us with a way to easily get Record Type Id by Developer Name, without using a SOQL query. This new feature comes with Summer ‘18 Release and it should help us write more efficient and reliable code.

The old way

Let’s imagine that we have a Record Type on Account object called “Wholesale Partner”. We would like to obtain the Id of this Record Type in order to assign it to the RecordTypeId field on the Account record during the insert operation.

Previously, there were a couple of options to obtain a Record Type Id in order to assign it to a record, but none of them were really optimal. One approach was to use the Record Type Name in order to obtain a Record Type Id in the following way by using the getRecordTypeInfosByName() method of the DescribeSObjectResult class:

Id recordTypeId =
  Schema.SObjectType.Account.getRecordTypeInfosByName()
    .get('Wholesale Partner').getRecordTypeId();

The issue here is that the Record Type Name is nothing but a label, which can be easily changed by an administrator in Production, thus breaking your code without any warning. The second issue with this approach is that the logic might break in multi-language environments in case somebody translates the label. However, as far as I know, this was the only approach to obtain a Record Type Id without issuing a SOQL query.

This brings us to the second approach that uses the RecordType object. You can execute a SOQL query against this object to get the Record Type Id by Developer Name, which is much more stable than the label, it isn’t affected by translations, and cannot be changed that easily.

Id recordTypeId =
  [SELECT Id FROM RecordType
    WHERE DeveloperName = 'Wholesale_Partner' AND sObjectType = 'Account'].Id;

Although this approach is more reliable than the first, my issue with it is that it requires a SOQL query. In an environment that limits you to 100 SOQL queries per transaction, every one of them counts.

The new way

With Summer ‘18 Release we now finally have a way to use the Developer Name to obtain a Record Type Id without issuing a SOQL query. This is achieved by using the new getRecordTypeInfosByDeveloperName() method of the DescribeSObjectResult class:

Id recordTypeId =
  Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName()
    .get('Wholesale_Partner').getRecordTypeId();

In my opinion, this should be a new standard for this kind of tasks, it’s both reliable and cheap on the resources. And if you are really concerned with resources and your logic requires to get Ids of different Record Types during execution, you should maybe consider caching the Map<String, Schema.RecordTypeInfo> returned by the getRecordTypeInfosByDeveloperName() method. If you are interested you can check out this answer on StackExchange with analysis on the resource consumption by different Apex Describe calls.

In case you want to use the above sample in your code, you should replace the Account with the object that has your Record Type, and the Wholesale_Partner with the actual Developer Name of the Record Type that you need.

How to assign a Record Type Id to a Record

So now that you have a Record Type Id, you can assign it to a specific record by using the RecordTypeId field. Here’s an example for Account:

Account acc = new Account(Name='Test Account', RecordTypeId = recordTypeId);

or through a property like this:

acc.RecordTypeId = recordTypeId;

Permanent link to this article: https://salesforcebuddy.com/2019/01/working-with-record-types/

Leave a Reply

Your email address will not be published.