Get Record Type ID by Name for a Salesforce Object

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 … Read more

Apex File Naming Standards

Consistent file naming helps keep Visualforce and Apex classes easy to recognize and find. I’ve seen many orgs with both the good and the bad. Below is a table with naming suggestions. Some of them are mine, but most came from a presentation that I found referenced in a Trailhead module. The presentation is by … Read more

Remove Last Comma or Other Characters from the End of a String

Apex Removing the last comma is often needed when dynamically building a SOQL Select string. Salesforce quietly released a bunch of new string methods in Winter ’13 that I have started to really appreciate. One of the new methods is removeEnd() and removeEndIngoreCase(). These are great for removing that last comma. String soql = ‘Select … Read more

Salesforce Describe Methods – Record Types, Fields, and Global Describe

Record Type Describe

Getting an object’s record type Id is simple with the statement below, and it avoids using a SOQL statment on the RecordType object. However, the method name is misleading because getRecordTypeInfosByName actually retrieves the record type with the Label and not the Name. Also, be careful because the name is case sensitive. A Null Pointer Exception will be thrown if the label does not match exact spelling and case, so add appropriate error handling.

// Return record type Id by DeveloperName 
Id MyRecTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('My_Dev_Name').getRecordTypeId()

// Return record type Id by Label
Id MyRecTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('My Record Type Label').getRecordTypeId();

Read more