Show:            

RecordTypes

This class is allows for easy, readable access to Record Type information from other classes.

Written by Evan Callahan, copyright 2010 Groundwire
Updated by David Schach, copyright 2021-2024 X-Squared on Demand

This program is released under the GNU General Public License. http://www.gnu.org/licenses/

This class is meant to allow for access to Record Type information from within other classes.

It is called statically, and therefore will supply the same information to all calls made from within one transaction, or set of trigger calls. This is beneficial because this info should be the same for all calls in a transaction, and by calling it statically we reduce the calls that are made, making the total transaction more efficient.

Usage recommendation: Always use DeveloperName instead of Name , as this is more likely to be hard-coded in places, while the Name is actually a label and may change (especially due to user language).
An easy-to-read table with the various methods, their inputs, and their outputs is available at Record Types
LICENSE
Copyright Evan Callahan & David Schach
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
Signature
global inherited sharing class RecordTypes
See
Author
Evan Callahan , Groundwire
Since
  • 2010
  • Author
    David Schach , X-Squared on Demand
    Since
  • 2021
  • Author
    Nicolas Vasquez, Globant
    Since
  • 2022
  • RecordTypes Properties

    Name Signature Description
    allRecordTypes
    private static Map<String, List<Schema.RecordTypeInfo>> allRecordTypes
    map of all Record Types by sObject name (lowercase), including inactive and unavailable
    availableRecordTypesMap
    private static Map<String, List<Schema.Recordtypeinfo>> availableRecordTypesMap
    List of available (to the running user) Record Type Info sObjects
    defaultRecordTypeIds
    private static Map<String, Id> defaultRecordTypeIds
    Map of default Record Type Ids for the running user
    defaultRecordTypes
    private static Map<String, Schema.RecordTypeInfo> defaultRecordTypes
    Map of object name to the default Schema.RecordTypeInfo object for that object
    recordTypesByDevName
    private static Map<String, Map<String, Schema.RecordTypeInfo>> recordTypesByDevName
    Map of Record Types by sObject name (lowercase), then by DeveloperName
    recordTypesById
    private static Map<String, Map<Id, Schema.RecordTypeInfo>> recordTypesById
    Map of Record Types by sObject name (lowercase), then by Id
    recordTypesByName
    private static Map<String, Map<String, Schema.RecordTypeInfo>> recordTypesByName
    Map of Record Types by sObject name (lowercase), then by Name
    recordTypesIdMap
    private static Map<Id, RecordType> recordTypesIdMap
    Lazy-loaded, memoized map of queried RecordType records by Id
    sObjectDescribes
    private static Map<String, Schema.DescribeSObjectResult> sObjectDescribes
    Hold Sobject describe results as we need them

    RecordTypes Methods

    clearMapsInTest()

    If we are running a test, clear sObject maps. We do this to avoid using @testVisible on the maps themselves
    This is private and testvisible so it cannot be called in production code context
    Signature
    @testVisible
    private static void clearMapsInTest()

    fillMapsForObject(objectName)

    Gets record type maps for a new sObject
    We check in each public method if the sObject has already been described so we do not eagerly load DescribeSObjectResult
    Signature
    private static void fillMapsForObject(String objectName)
    Parameters
    objectName
    Type: String
    SObject name (with __c if custom, etc)
    Author

    getAllRecordTypesForSelectList(objectName)

    Make a Visualforce picklist with ALL RecordType Names displayed and the RecordType Id as the value
    Unavailable Record Types will be _disabled_ in the picklist.
    Signature
    global static List<SelectOption> getAllRecordTypesForSelectList(String objectName)
    Parameters
    objectName
    Type: String
    SObject name (with __c if custom, etc)
    Returns
    List<SelectOption> Visualforce SelectOption list
    Author

    getAllRecordTypesForSelectList(objectName, disableUnavailable)

    Make a Visualforce picklist with ALL RecordType Names displayed and the RecordType Id as the value
    Unavailable Record Types can be disabled in the picklist by setting disableUnavailable to true ..
    Signature
    global static List<SelectOption> getAllRecordTypesForSelectList(String objectName, Boolean disableUnavailable)
    Parameters
    objectName
    Type: String
    SObject name (with __c if custom, etc)
    disableUnavailable
    Type: Boolean
    Should we disable unavailable RTs in the SelectList?
    Returns
    List<SelectOption> Visualforce SelectOption list
    Author

    getAvailableRecordTypeDevNameIdMap(objectName)

    Gives a map of all available (to the running user) RecordTypeId s by developername for an sObject
    Signature
    global static Map<String, Id> getAvailableRecordTypeDevNameIdMap(String objectName)
    Parameters
    objectName
    Type: String
    SObject name (with __c if custom, etc)
    Returns
    Map<String, Id> Map<RecordType Name, RecordType Id>
    Authors
    Nicolas Vasquez, David Schach
    Since
  • 2023 Use available record types map
  • getAvailableRecordTypeNameIdMap(objectName)

    Gives a map of all available (to the running user) RecordTypeId s by name for an sObject
    Signature
    global static Map<String, Id> getAvailableRecordTypeNameIdMap(String objectName)
    Parameters
    objectName
    Type: String
    SObject name (with __c if custom, etc)
    Returns
    Map<String, Id> Map<RecordType Name, RecordType Id>
    Authors
    Nicolas Vasquez, David Schach
    Since
  • 2023 Use available record types map
  • getAvailableRecordTypesForSelectList(objectName)

    Make a Visualforce picklist of only available Record Types with the RecordType Name displayed and the RecordType Id as the value. No options are disabled.
    Signature
    global static List<SelectOption> getAvailableRecordTypesForSelectList(String objectName)
    Parameters
    objectName
    Type: String
    SObject name (with __c if custom, etc)
    Returns
    List<SelectOption> Visualforce SelectOption list
    Author
    Evan Callahan
    Example
     RecordTypes.getAvailableRecordTypesForSelectList('Account') 
     

    System.SelectOption[value="<SFID>", label="Available", disabled="false"] System.SelectOption[value="<SFID>", label="Default", disabled="false"] System.SelectOption[value="<SFID>", label="Inactive", disabled="true"]

    getAvailableRecordTypesIdSet(objectName)

    Gives a set of all available (to the running user) RecordTypeId s for an sObject
    Signature
    global static Set<Id> getAvailableRecordTypesIdSet(String objectName)
    Parameters
    objectName
    Type: String
    SObject name (with __c if custom, etc)
    Returns
    Set<Id> Map<RecordType Name, RecordType Id>
    Authors
    Nicolas Vasquez, David Schach
    Since
  • 2024 Use available record types map to generate set
  • getDefaultRecordType(objectName)

    Given an sObject Name, return the default Record Type Id for the running user
    Signature
    @SuppressWarnings('PMD.AvoidDeeplyNestedIfStmts')
    global static Schema.RecordTypeInfo getDefaultRecordType(String objectName)
    Parameters
    objectName
    Type: String
    SObject name (with __c if custom, etc)
    Returns
    Id Default Record Type Id for the running user for this object

    getDefaultRecordTypeDevName(objectName)

    Given an sObject Name, return the default Record Type DeveloperName for the running user
    Signature
    global static String getDefaultRecordTypeDevName(String objectName)
    Parameters
    objectName
    Type: String
    SObject name (with __c if custom, etc)
    Returns
    String Default Record Type DeveloperName for the running user for this object

    getDefaultRecordTypeId(objectName)

    Given an sObject Name, return the default Record Type Id for the running user
    Signature
    global static Id getDefaultRecordTypeId(String objectName)
    Parameters
    objectName
    Type: String
    SObject name (with __c if custom, etc)
    Returns
    Id Default Record Type Id for the running user for this object

    getDefaultRecordTypeName(objectName)

    Given an sObject Name, return the default Record Type Name for the running user
    Signature
    global static String getDefaultRecordTypeName(String objectName)
    Parameters
    objectName
    Type: String
    SObject name (with __c if custom, etc)
    Returns
    String Default Record Type Name for the running user for this object

    getRecordTypeDevNameFromId(objectName, recordTypeId)

    Given an sObject and RecordType Id, return the RecordType DeveloperName
    This saves a query on the RecordType object
    Signature
    global static String getRecordTypeDevNameFromId(String objectName, Id recordTypeId)
    Parameters
    objectName
    Type: String
    SObject name (with __c if custom, etc)
    recordTypeId
    Type: Id
    RecordType Id
    Returns
    RecordType DeveloperName
    See
    getRecordTypeDevNameFromId
    Author

    getRecordTypeDevNameFromId(recordTypeId)

    Generate map of all record type developerNames by Id & return Record Type DeveloperName
    This is an overloaded method for efficiency, using only the Id
    Signature
    global static String getRecordTypeDevNameFromId(Id recordTypeId)
    Parameters
    recordTypeId
    Type: Id
    RecordTypeId
    Returns
    String Record Type Name
    See
    getRecordTypeDevNameFromId
    Author
    Since
  • 2022-03-29
  • getRecordTypeDevNameFromName(objectName, recordTypeName)

    Given an sObject and RecordType Name, return the RecordType Id
    Note: Since RecordType.Name is not unique, this section could give unpredicable results
    If possible, DO NOT USE this method - use a method with RecordType.DeveloperName instead
    Signature
    global static String getRecordTypeDevNameFromName(String objectName, String recordTypeName)
    Parameters
    objectName
    Type: String
    SObject name (with __c if custom, etc)
    recordTypeName
    Type: String
    RecordType Name (can include spaces)
    Returns
    RecordType.DeveloperName
    Author
    Example
     RecordTypes.getRecordTypeDevNameFromName('Account','Person Account')

    getRecordTypeDevNameIdMap(objectName)

    Gives a map of all record type IDs by DeveloperName for an sObject
    Returns Master record type if no other RTs available
    Signature
    global static Map<String, Id> getRecordTypeDevNameIdMap(String objectName)
    Parameters
    objectName
    Type: String
    SObject name (with __c if custom, etc)
    Returns
    Map<String, Id> Map<recordTypeDevName, RecordType Id>
    Author

    getRecordTypeFromDevName(objectName, recordTypeDevName)

    Given an sObject and RecordType DeveloperName, return the RecordType Id
    Signature
    global static Schema.RecordTypeInfo getRecordTypeFromDevName(String objectName, String recordTypeDevName)
    Parameters
    objectName
    Type: String
    SObject name (with __c if custom, etc)
    recordTypeDevName
    Type: String
    RecordType.DeveloperName (cannot include spaces)
    Returns
    Schema.RecordTypeInfo RecordTypeInfo object
    Author
    Example
     RecordTypes.getRecordTypeFromDevName('Account','Person_Account')

    getRecordTypeFromId(recordTypeId)

    Given a RecordType Id, return the Record Type object
    Signature
    global static Schema.RecordTypeInfo getRecordTypeFromId(Id recordTypeId)
    Parameters
    recordTypeId
    Type: Id
    RecordType Id
    Returns
    Schema.RecordTypeInfo RecordTypeInfo object
    Author

    getRecordTypeFromId(objectName, recordTypeId)

    Given an sObject and RecordType Id, return the Record Type object
    Signature
    global static Schema.RecordTypeInfo getRecordTypeFromId(String objectName, Id recordTypeId)
    Parameters
    objectName
    Type: String
    SObject name (with __c if custom, etc)
    recordTypeId
    Type: Id
    RecordType Id
    Returns
    Schema.RecordTypeInfo RecordTypeInfo object
    Author

    getRecordTypeFromName(objectName, recordTypeName)

    Given an sObject and RecordType Name, return the RecordType object
    Note: Since RecordType.Name is not unique, this section could give unpredicable results
    If possible, DO NOT USE this method - use a method with RecordType.DeveloperName instead
    Signature
    global static Schema.RecordTypeInfo getRecordTypeFromName(String objectName, String recordTypeName)
    Parameters
    objectName
    Type: String
    SObject name (with __c if custom, etc)
    recordTypeName
    Type: String
    RecordType Name (can include spaces)
    Returns
    Schema.RecordTypeInfo RecordTypeInfo object
    Author
    Example
     RecordTypes.getRecordTypeFromName('Account','Person Account')

    getRecordTypeIdFromDevName(objectName, recordTypeDevName)

    Given an sObject and RecordType DeveloperName, return the RecordType Id
    This should be the most-used method in this class
    Signature
    global static Id getRecordTypeIdFromDevName(String objectName, String recordTypeDevName)
    Parameters
    objectName
    Type: String
    SObject name (with __c if custom, etc)
    recordTypeDevName
    Type: String
    RecordType.DeveloperName (cannot include spaces)
    Returns
    RecordType Id
    Author
    Example
     RecordTypes.getRecordTypeIdFromDevName('Account','Person_Account')

    getRecordTypeIdFromName(objectName, recordTypeName)

    Given an sObject and RecordType Name, return the RecordType Id
    Note: Since RecordType.Name is not unique, this section could give unpredicable results
    If possible, DO NOT USE this method - use a method with RecordType.DeveloperName instead
    Signature
    global static Id getRecordTypeIdFromName(String objectName, String recordTypeName)
    Parameters
    objectName
    Type: String
    SObject name (with __c if custom, etc)
    recordTypeName
    Type: String
    RecordType Name (can include spaces)
    Returns
    RecordType Id
    Author
    Example
     RecordTypes.getRecordTypeIdFromName('Account','Person Account')

    getRecordTypeIdSetFromDevNames(objectName, recordTypeDevNameSet)

    Gives a set of RecordType Ids from an sObject and a set of RecordType DeveloperNames
    Signature
    global static Set<Id> getRecordTypeIdSetFromDevNames(String objectName, Set<String> recordTypeDevNameSet)
    Parameters
    objectName
    Type: String
    SObject name (with __c if custom, etc)
    recordTypeDevNameSet
    Type: Set<String>
    Set of DeveloperNames
    Returns
    Set<Id> All RecordType IDs for a given sObject
    Author
    Example
     Set<String> rtNames = new Set<String> {'Person_Account', 'Customer'}; 
     getRecordTypeIdSetFromDevNames('Account', rtNames);

    getRecordTypeNameFromDevName(objectName, recordTypeDevName)

    Given an sObject and RecordType DeveloperName, return the RecordType Name
    Signature
    global static String getRecordTypeNameFromDevName(String objectName, String recordTypeDevName)
    Parameters
    objectName
    Type: String
    SObject name (with __c if custom, etc)
    recordTypeDevName
    Type: String
    RecordType.DeveloperName (cannot include spaces)
    Returns
    RecordType Name
    Author
    Example
     RecordTypes.getRecordTypeNameFromDevName('Account','Person_Account')

    getRecordTypeNameFromId(objectName, recordTypeId)

    Given an sObject and RecordType Id, return the RecordType Name
    Signature
    global static String getRecordTypeNameFromId(String objectName, Id recordTypeId)
    Parameters
    objectName
    Type: String
    SObject name (with __c if custom, etc)
    recordTypeId
    Type: Id
    RecordType Id
    Returns
    RecordType Name
    Author

    getRecordTypeNameFromId(recordTypeId)

    Generate map of all record type names by Id & return Record Type Name
    Signature
    global static String getRecordTypeNameFromId(Id recordTypeId)
    Parameters
    recordTypeId
    Type: Id
    RecordTypeId
    Returns
    String Record Type Name
    Author
    Since
  • 2022-03-29
  • getRecordTypeNameIdMap(objectName)

    Gives a map of all RecordType IDs by name for an sObject
    Signature
    global static Map<String, Id> getRecordTypeNameIdMap(String objectName)
    Parameters
    objectName
    Type: String
    SObject name (with __c if custom, etc)
    Returns
    Map<String, Id> Map<RecordType Name, RecordType Id>
    Author
    Evan Callahan

    getRecordTypesForObject(objectName)

    Gives a list of all the RecordTypeInfo object records for an sObject
    This list has more items than just the record types available to the running user.
    This method has no immediately obvious uses, but perhaps an outside library will need this list for, say, a project for a LWC.
    Signature
    global static List<Schema.RecordTypeInfo> getRecordTypesForObject(String objectName)
    Parameters
    objectName
    Type: String
    SObject name (with __c if custom, etc)
    Returns
    List<Schema.RecordTypeInfo> List of all record types for the object (active and inactive)
    Author

    getStringRecordTypesForSelectList(objectName)

    Make a Visualforce picklist with the RecordType Name displayed and the RecordType Name as the value
    Signature
    global static List<SelectOption> getStringRecordTypesForSelectList(String objectName)
    Parameters
    objectName
    Type: String
    SObject name (with __c if custom, etc)
    Returns
    List<SelectOption> Visualforce SelectOption list
    Author
    Evan Callahan

    isRecordTypeAvailable(objectName, recordTypeDevName)

    Is this record type (identified by developer name) available (and active) for the current user?
    Signature
    public static Boolean isRecordTypeAvailable(String objectName, String recordTypeDevName)
    Parameters
    objectName
    Type: String
    SObject name (with __c if custom, etc)
    recordTypeDevName
    Type: String
    RecordType.DeveloperName (cannot include spaces)
    Returns
    Boolean Is this Record Type available to the running user?

    isRecordTypeDefault(objectName, recordTypeDevName)

    Is this record type (identified by developer name) the default record type for the current user?
    Signature
    public static Boolean isRecordTypeDefault(String objectName, String recordTypeDevName)
    Parameters
    objectName
    Type: String
    SObject name (with __c if custom, etc)
    recordTypeDevName
    Type: String
    RecordType.DeveloperName (cannot include spaces)
    Returns
    Boolean Is this Record Type the default for the running user?