RecordType Utility Apex Class


I prefer organizing Apex methods according to the specific objects they relate to. This practice aids in code troubleshooting and, in the long run, minimizes the overall code volume within a Salesforce organization. As an illustration, developers operating in Enterprise-level or larger organizations frequently encounter the need to retrieve record types for a particular object.

Consequently, I’ve developed a utility class dedicated to Record Types. This class conducts queries on the RecordType object, taking into account the specified object declaration.

/*
	Created by: Greg Hacic
	Last Update: 24 February 2016 by Greg Hacic
	Questions?: greg@interactiveties.com
*/
public class recordTypeUtil {
	
	//creates a map of all Record Type Names to Ids based upon a passed Object
	public static Map<String, Id> buildMapOfRecordTypes(String passedSobjectType) {
		Map<String, Id> returnMap = new Map<String, Id>(); //the return map variable
		for (RecordType rt : [SELECT Id, Name FROM RecordType WHERE SobjectType = :passedSobjectType]) { //grab the recordtypes for the specified sObject
			returnMap.put(rt.Name, rt.Id); //put the details into our map (Name -> Id)
		}
		return returnMap; //return the map
	}

}

As evident, this straightforward class enables you to recycle a frequently used SOQL statement. To implement it in another class, just invoke the method in the following manner:

Map opportunityRecordTypes = recordTypeUtil.buildMapOfRecordTypes('Opportunity'); //build the map of Opportunity recordtypes
Id recordTypeId = opportunityRecordTypes.get('RecordType Name'); //grab the 'RecordType Name' RecordType Id

Planning to implement this logic in a trigger handler? Consider establishing a getter method that constructs the map during the handler’s initialization. This approach not only cuts down on the number of SOQL statements but also grants you access to the information for all trigger contexts.

/*
	Created by: Greg Hacic
	Last Update: 24 February 2016 by Greg Hacic
	Questions?: greg@interactiveties.com
*/
public class accountTriggerHandler extends TriggerHandler {
	
	private Map<Id, Account> newRecordMap; //map of new records
	private Map<Id, Account> oldRecordMap; //map of old records
	private List<Account> newRecords; //list of new records
	private List<Account> oldRecords; //list of old records
	
	//constructor
	public accountTriggerHandler() {
		newRecordMap = (Map<Id, Account>)Trigger.newMap; //cast the map of new records
		newRecords = (List<Account>)Trigger.new;  //cast the list of new records
		oldRecordMap = (Map<Id, Account>)Trigger.oldMap; //cast the map of old records
		oldRecords = (List<Account>)Trigger.old; //cast the list of old records
	}
	
	//override the beforeUpdate trigger context
	public override void beforeUpdate() {
		
		Id clientRecordTypeId = contactRecordTypes.get('Client'); //grab the Client RecordType Id
		
		for (Account a : newRecords) { //for each new Account record
			if (a.RecordTypeId == clientRecordTypeId) { //if the recordtype is a match
				//do something...
			}
		}
	}
	
	//builds a map Account RecordTypeIds (RecordType.Name -> RecordType.Id)
	public static Map<String, Id> accountRecordTypes {
		//getter constructor
		get {
			Map<String, Id> returnMap = recordTypeUtil.buildMapOfRecordTypes('Account'); //build the map of Account recordtypes
			return returnMap; //return the map
		}
	}

}