RecordType Utility Apex Class
I like to group Apex methods by the object to which they apply. I find that this supports troubleshooting of code and ultimately reduces the amount of code in a given Salesforce organization. For example, every Salesforce developer that works in an Enterprise level organization or higher will find that more often than not they need to know the record types for a given object.
Therefore, I've created a Record Type utility class that performs the query against the RecordType object based upon a passed 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 you can see this simple class allows you to reuse an SOQL statement that you may be using repeatedly. To use in another class simply call the method as follows:
Map opportunityRecordTypes = recordTypeUtil.buildMapOfRecordTypes('Opportunity'); //build the map of Opportunity recordtypes
Id recordTypeId = opportunityRecordTypes.get('RecordType Name'); //grab the 'RecordType Name' RecordType Id
Need to use this logic in a trigger handler? Create a getter method that builds the map upon initialization of the handler which will reduce your SOQL statements and give you access to the information for all the 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
}
}
}