Salesforce provides an Apex method for retrieving an object’s record type record id. The method accepts the record type label instead of the developer/api name, and this makes the Apex method sensitive record type label changes. Here is a typical example of the method in use. It strings together methods for the object’s schema, record type info, and record type id retrieval.
Id recTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Industrial Deal').getRecordTypeId();
There are a few workarounds to prevent hard coding the record type’s label. The most popular is to use a custom label. A custom label can easily be updated when the record type label changes. Also, it won’t require changing Apex code, and it will come over when building/refreshing a sandbox. The downside is that you must remember to change both if the record type’s label changes. Other alternatives to a using a custom label include custom settings and custom metadata.
// with a custom label. This assumes a custom label by the name OpportunityRecTypeIndustrialDeal exists Id recTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get(Label.OpportunityRecTypeIndustrialDeal).getRecordTypeId(); // with a custom setting. This assumes there is a custom setting object ObjectRecordTypeLabel__c that contains a field RecordTypeLabel__c ObjectRecordTypeLabel__c ortl = ObjectRecordTypeLabel__c.getInstance('Opportunity-Industrial-Deal'); Id recTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get(ortl.RecordTypeLabel__c).getRecordTypeId(); // with custom metadata. This assumes there is a custom metadata object ObjectRecordTypeLabel__mdt // querying against a custom metadata object doesn't count against soql limits ObjectRecordTypeLabel__mdt ortl = [SELECT DeveloperName, MasterLabel FROM ObjectRecordTypeLabel__mdt WHERE DeveloperName='Opportunity-Industrial-Deal' LIMIT 1]; Id recTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get(ortl.MasterLabel).getRecordTypeId();
** You will want to handle exceptions with all of these techniques. First, if the record type is not found. Second, if the Custom Setting or Custom Metadata record was not found.
Querying the RecordType sobject is also possible, but not recommended because the query will count against the SOQL limits in the processing transaction.
Id RecTypeId; try { RecTypeId = [SELECT Id, SobjectType, Name, DeveloperName FROM RecordType WHERE SobjectType='Opportunity' and DeveloperName='Industrial_Deal' LIMIT 1]; } catch (exception e) { // do appropriate error handling }
The record type information can also be acquired through the global describe methods. This can be useful if you are retrieving other information about the object in addition to the record type. Describe methods return a lot of information about the object itself, the object’s fields, and relationships. Below is an example of using describe methods to get the record type Id.
// a chain of methods to get the record type Id by label Id recTypeId = Schema.getGlobalDescribe().get('Opportunity').getDescribe().getRecordTypeInfosByName().get('Industrial Deal').getRecordTypeId(); // break the describe methods apart at each step Schema.SObjectType objType = Schema.getGlobalDescribe().get('Opportunity'); Schema.DescribeSobjectResult objTypeDesc = objType.getDescribe(); maprecTypeMap = objTypeDesc.getRecordTypeInfosById(); Schema.RecordTypeInfo rtByName = recTypeMap.get('Industrial Deal');