Skip to content

UTIL_SObjectDescribe

Class · Group: Utilities

apex
global inherited sharing class UTIL_SObjectDescribe

A semi-intelligent wrapper for standard Apex Schema methods, providing internal caching to avoid hitting describe limits and helper methods for handling relationship field names and namespaces.

Example

apex
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe(Account.SObjectType);
SObjectField nameField = describe.getNameField();
String fieldName = UTIL_SObjectDescribe.getCachedFieldName(Account.Industry);

Methods

MethodDescription
global static Set<String> extractValidObjectFieldNames(String objectName, Set<String> fieldNames)Filters a set of field names, returning only those that are valid for the given object.
global static void flushCache()Clears the cache of global describe and SObject describe instances to free heap space.
global static Set<String> getAllFieldNames(SObjectType objectType)Returns all queryable field names for an object by SObjectType (includes record type fields).
global static Set<String> getAllFieldNames(String objectName)Returns all queryable field names for an object by API name (includes record type fields).
global static Set<String> getAllFieldNames(String objectName, Boolean includeRecordType)Returns all queryable field names for an object, optionally including record type fields.
global static DescribeFieldResult getCachedFieldDescribe(SObjectField field)Static accessor for field describes that enables context-agnostic lookups.
global static String getCachedFieldName(SObjectField field)Static convenience method to get a field's API name directly from an SObjectField token.
global static String getDefaultPicklistValue(DescribeFieldResult fieldDescribe)Retrieves the default picklist value for a given field describe.
global static String getDefaultPicklistValue(DescribeFieldResult fieldDescribe, Boolean returnFirstEntryIfNoDefault)Retrieves the default picklist value for a given field describe.
global static String getDefaultPicklistValue(SObjectField objectField)Retrieves the default picklist value for a given SObject field.
global static String getDefaultPicklistValue(SObjectField objectField, Boolean returnFirstEntryIfNoDefault)Retrieves the default picklist value for a given SObject field.
global static Id getDefaultRecordType(SObjectType objectType)Retrieves the Id of the default record type for an SObject.
global DescribeSObjectResult getDescribe()Retrieves the raw DescribeSObjectResult for the described object.
global static UTIL_SObjectDescribe getDescribe(DescribeSObjectResult describeResult)Retrieves a cached UTIL_SObjectDescribe instance for the specified DescribeSObjectResult.
global static UTIL_SObjectDescribe getDescribe(SObject instance)Retrieves a cached UTIL_SObjectDescribe instance for the specified SObject instance.
global static UTIL_SObjectDescribe getDescribe(SObjectType objectType)Retrieves a cached UTIL_SObjectDescribe instance for the specified SObject type.
global static UTIL_SObjectDescribe getDescribe(String sObjectName)Retrieves a cached UTIL_SObjectDescribe instance for the specified SObject name.
global SObjectField getField(String name)Retrieves an SObjectField by name, with namespace handling enabled by default.
global SObjectField getField(String fieldName, Boolean implyNamespace)Retrieves an SObjectField by name, handling relationship notation (e.g., 'Account' vs.
global DescribeFieldResult getFieldDescribe(SObjectField field)Retrieves a cached DescribeFieldResult for the given SObjectField token.
global DescribeFieldResult getFieldDescribe(String fieldName)Retrieves a cached DescribeFieldResult for the given field name.
global String getFieldName(SObjectField field)Retrieves the API name for the given SObjectField token.
global UTIL_SObjectDescribe.FieldsMap getFields()Retrieves a wrapped map of fields with namespace handling for the described object.
global Map<String, FieldSet> getFieldSetsMap()Retrieves a map of field set names to FieldSet objects for the described object.
global Map<String, SObjectField> getFieldsMap()Retrieves a map of field API names to SObjectField instances.
global static UTIL_SObjectDescribe.GlobalDescribeMap getGlobalDescribe()Retrieves a wrapped map of global SObjectType names to SObjectType instances with namespace handling.
global SObjectField getNameField()Retrieves the name field of the SObject (where isNameField() is true).
global static Set<String> getNestableFieldNames(String objectName)Walks the object tree and returns all fields for related objects (one level deep).
global static Set<String> getNestableFieldNames(String objectName, Integer levels)Walks the object tree and returns all fields for related objects up to the specified depth.
global static Map<String, SObjectField> getObjectFieldMap(SObjectType objectType)Returns the field map for an object by SObjectType.
global static Map<String, SObjectField> getObjectFieldMap(String objectName)Returns the field map for an object by API name.
global static Map<String, SObjectField> getObjectFieldReferenceMap(String objectName)Returns a map of reference (lookup/master-detail) fields keyed by relationship name.
global static String getObjectNameFromId(Id objectId)Returns the API name of an object based on its ID.
global static String getObjectNameFromType(SObjectType objectType)Will get the API name for the SObject given a specific type.
global static List<PicklistEntry> getPicklistEntries(SObjectField pickField)Returns a list of picklist entries for a given SObject field reference.
global static Map<String, String> getPicklistEntriesMap(SObjectField pickField)Returns a map of picklist label-to-value pairs for a given SObject field reference.
global static Map<String, String> getPicklistEntriesMap(SObjectField pickField, Boolean useLabelAsKey)Returns a map of picklist entries with configurable key for a given SObject field reference.
global static List<DTO_PickList> getPicklistValues(String objectApiName, Id recordTypeId)Returns all picklist values for a given object and record type combination as a list of DTO_PickList objects, including dependent picklist relationships.
global static ConnectApi.PicklistValuesCollection getPicklistValuesByRecordType(String objectApiName, Id recordTypeId)Returns all picklist values for a given object and record type combination, including dependent picklist relationships.
global static Map<String, SObjectType> getRawGlobalDescribe()Retrieves a cached map of global SObjectType names to SObjectType instances.
global static Id getRecordTypeByDeveloperName(SObjectType objectType, String recordTypeName)Retrieves the Id of a record type for an SObject by its developer name.
global static Set<String> getSObjectFieldNames(Set<SObjectField> objectFields)Converts a set of SObjectField tokens to their API name strings.
global SObjectType getSObjectType()Returns the SObjectType this instance is based on, useful for retrieving metadata about the specific object.
global static SObjectType getSObjectTypeById(Id objectId)Returns the SObjectType token for a given record ID.
global static SObjectType getSObjectTypeByName(String objectApiName)Returns the SObjectType token for a given object API name.
global static Boolean isPersonAccountEnabled()Determines whether Person Accounts are enabled by checking for the isPersonAccount field on Account.
global static Boolean isRecordTypeAvailable(SObjectType objectType, String recordTypeName)Reports whether a record type, identified by its developer name, is available to the running user for the given SObject.

extractValidObjectFieldNames

apex
global static Set<String> extractValidObjectFieldNames(String objectName, Set<String> fieldNames)

Filters a set of field names, returning only those that are valid for the given object.

Parameters

ParameterTypeDescription
objectNameStringThe API name of the object
fieldNamesSetThe field names to check

Returns String — A set containing only the valid field names

Example

apex
Set<String> fields = new Set<String>{ 'Name', 'InvalidField__c', 'Industry' };
Set<String> validFields = UTIL_SObjectDescribe.extractValidObjectFieldNames('Account', fields);

flushCache

apex
global static void flushCache()

Clears the cache of global describe and SObject describe instances to free heap space.

Example

apex
UTIL_SObjectDescribe.flushCache();
System.debug(UTIL_SObjectDescribe.getDescribe('Account')); // Forces new describe instance

getAllFieldNames

apex
global static Set<String> getAllFieldNames(SObjectType objectType)

Returns all queryable field names for an object by SObjectType (includes record type fields).

Parameters

ParameterTypeDescription
objectTypeSObjectTypeThe SObjectType

Returns String — A set of field names for the object

Example

apex
Set<String> fieldNames = UTIL_SObjectDescribe.getAllFieldNames(Account.SObjectType);
apex
global static Set<String> getAllFieldNames(String objectName)

Returns all queryable field names for an object by API name (includes record type fields).

Parameters

ParameterTypeDescription
objectNameStringThe API name of the object

Returns String — A set of field names for the object

Example

apex
Set<String> fieldNames = UTIL_SObjectDescribe.getAllFieldNames('Account');
apex
global static Set<String> getAllFieldNames(String objectName, Boolean includeRecordType)

Returns all queryable field names for an object, optionally including record type fields.

Parameters

ParameterTypeDescription
objectNameStringThe API name of the object
includeRecordTypeBooleanWhether to include RecordType relationship fields

Returns String — A set of field names for the object

Example

apex
Set<String> fieldNames = UTIL_SObjectDescribe.getAllFieldNames('Account', false);

getCachedFieldDescribe

apex
global static DescribeFieldResult getCachedFieldDescribe(SObjectField field)

Static accessor for field describes that enables context-agnostic lookups. This method allows classes like QRY_Generator and QRY_Builder to resolve field names without needing to know the parent SObjectType.

Parameters

ParameterTypeDescription
fieldSObjectFieldThe SObjectField token to describe.

Returns DescribeFieldResult — The cached DescribeFieldResult for the field, or null if the field is null.

Example

apex
String fieldName = UTIL_SObjectDescribe.getCachedFieldDescribe(Account.Name)?.getName();
System.debug(fieldName); // Outputs: Name

getCachedFieldName

apex
global static String getCachedFieldName(SObjectField field)

Static convenience method to get a field's API name directly from an SObjectField token. Uses the global field describe cache for efficiency. This is the preferred method when you only need the field name and don't require the full DescribeFieldResult.

Parameters

ParameterTypeDescription
fieldSObjectFieldThe SObjectField token to get the name for.

Returns String — The field's API name, or null if the field is null.

Example

apex
String fieldName = UTIL_SObjectDescribe.getCachedFieldName(Account.Name);
System.debug(fieldName); // Outputs: Name

getDefaultPicklistValue

apex
global static String getDefaultPicklistValue(DescribeFieldResult fieldDescribe)

Retrieves the default picklist value for a given field describe.

Parameters

ParameterTypeDescription
fieldDescribeDescribeFieldResultThe DescribeFieldResult to retrieve a picklist value for.

Returns String — The default picklist value, or null if no default is available.

Example

apex
String picklistValue = UTIL_SObjectDescribe.getDefaultPicklistValue(Foobar__c.Picklist__c.getDescribe());
apex
global static String getDefaultPicklistValue(DescribeFieldResult fieldDescribe, Boolean returnFirstEntryIfNoDefault)

Retrieves the default picklist value for a given field describe.

Parameters

ParameterTypeDescription
fieldDescribeDescribeFieldResultThe DescribeFieldResult to retrieve a picklist value for.
returnFirstEntryIfNoDefaultBooleanIf no default is found, return the first entry if available.

Returns String — The default picklist value, or null if no default (or first entry) is available.

Example

apex
String picklistValue = UTIL_SObjectDescribe.getDefaultPicklistValue(Foobar__c.Picklist__c.getDescribe(), true);
apex
global static String getDefaultPicklistValue(SObjectField objectField)

Retrieves the default picklist value for a given SObject field.

Parameters

ParameterTypeDescription
objectFieldSObjectFieldThe SObject field to retrieve a picklist value for.

Returns String — The default picklist value, or null if no default is available.

Example

apex
String picklistValue = UTIL_SObjectDescribe.getDefaultPicklistValue(Foobar__c.Picklist__c);
apex
global static String getDefaultPicklistValue(SObjectField objectField, Boolean returnFirstEntryIfNoDefault)

Retrieves the default picklist value for a given SObject field.

Parameters

ParameterTypeDescription
objectFieldSObjectFieldThe SObject field to retrieve a picklist value for.
returnFirstEntryIfNoDefaultBooleanIf no default is found, return the first entry if available.

Returns String — The default picklist value, or null if no default (or first entry) is available.

Example

apex
String picklistValue = UTIL_SObjectDescribe.getDefaultPicklistValue(Foobar__c.Picklist__c, true);

getDefaultRecordType

apex
global static Id getDefaultRecordType(SObjectType objectType)

Retrieves the Id of the default record type for an SObject.

Parameters

ParameterTypeDescription
objectTypeSObjectTypeThe SObject type.

Returns Id — The Id of the default record type, or null if not found.

Example

apex
Id recordTypeId = UTIL_SObjectDescribe.getDefaultRecordType(Account.SObjectType);

getDescribe

apex
global DescribeSObjectResult getDescribe()

Retrieves the raw DescribeSObjectResult for the described object.

Returns DescribeSObjectResult — The DescribeSObjectResult containing detailed metadata for the object.

Example

apex
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe('Account');
DescribeSObjectResult result = describe.getDescribe();
System.debug(result.getLabel()); // Outputs: Account
apex
global static UTIL_SObjectDescribe getDescribe(DescribeSObjectResult describeResult)

Retrieves a cached UTIL_SObjectDescribe instance for the specified DescribeSObjectResult.

Parameters

ParameterTypeDescription
describeResultDescribeSObjectResultThe DescribeSObjectResult for the SObject.

Returns UTIL_SObjectDescribe — A UTIL_SObjectDescribe instance, or null if the describe result is null.

Example

apex
DescribeSObjectResult result = Account.SObjectType.getDescribe();
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe(result);
System.debug(describe.getSObjectType()); // Outputs: Account
apex
global static UTIL_SObjectDescribe getDescribe(SObject instance)

Retrieves a cached UTIL_SObjectDescribe instance for the specified SObject instance.

Parameters

ParameterTypeDescription
instanceSObjectThe SObject instance.

Returns UTIL_SObjectDescribe — A UTIL_SObjectDescribe instance, or null if the instance is null.

Example

apex
Account acc = new Account();
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe(acc);
System.debug(describe.getNameField().getDescribe().name); // Outputs: Name
apex
global static UTIL_SObjectDescribe getDescribe(SObjectType objectType)

Retrieves a cached UTIL_SObjectDescribe instance for the specified SObject type.

Parameters

ParameterTypeDescription
objectTypeSObjectTypeThe SObject type.

Returns UTIL_SObjectDescribe — A UTIL_SObjectDescribe instance, or null if the object type is null.

Example

apex
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe(Account.SObjectType);
System.debug(describe.getDescribe().name); // Outputs: Account
apex
global static UTIL_SObjectDescribe getDescribe(String sObjectName)

Retrieves a cached UTIL_SObjectDescribe instance for the specified SObject name.

Parameters

ParameterTypeDescription
sObjectNameStringThe API name of the SObject.

Returns UTIL_SObjectDescribe — A UTIL_SObjectDescribe instance, or null if the SObject name is invalid.

Example

apex
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe('Account');
System.debug(describe.getSObjectType()); // Outputs: Account

getField

apex
global SObjectField getField(String name)

Retrieves an SObjectField by name, with namespace handling enabled by default.

Parameters

ParameterTypeDescription
nameStringThe API name of the field to retrieve.

Returns SObjectField — The SObjectField corresponding to the provided name, or null if not found.

Example

apex
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe('Contact');
SObjectField field = describe.getField('LastName');
System.debug(field.getDescribe().getLabel()); // Outputs: Last Name
apex
global SObjectField getField(String fieldName, Boolean implyNamespace)

Retrieves an SObjectField by name, handling relationship notation (e.g., 'Account' vs. 'AccountId') and optional namespace prefixing.

Parameters

ParameterTypeDescription
fieldNameStringThe API name of the field to retrieve.
implyNamespaceBooleanWhether to automatically handle namespace prefixes.

Returns SObjectField — The SObjectField corresponding to the provided name, or null if not found.

Example

apex
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe('Contact');
SObjectField field = describe.getField('Account', false);
System.debug(field?.getDescribe().name); // Outputs: AccountId

getFieldDescribe

apex
global DescribeFieldResult getFieldDescribe(SObjectField field)

Retrieves a cached DescribeFieldResult for the given SObjectField token. This overload accepts an SObjectField directly, providing type safety and avoiding string lookups. Delegates to the static getCachedFieldDescribe method to ensure both instance-based and static-based lookups share the same global cache.

Parameters

ParameterTypeDescription
fieldSObjectFieldThe SObjectField token to describe.

Returns DescribeFieldResult — The DescribeFieldResult for the field, or null if the field is null.

Example

apex
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe('Account');
DescribeFieldResult fieldDescribe = describe.getFieldDescribe(Account.Name);
System.debug(fieldDescribe.getLabel()); // Outputs: Account Name
apex
global DescribeFieldResult getFieldDescribe(String fieldName)

Retrieves a cached DescribeFieldResult for the given field name. This method provides significant performance benefits when repeatedly accessing field metadata, as it caches the describe result after the first call.

Parameters

ParameterTypeDescription
fieldNameStringThe API name of the field to describe.

Returns DescribeFieldResult — The DescribeFieldResult for the field, or null if the field doesn't exist.

Example

apex
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe('Account');
DescribeFieldResult fieldDescribe = describe.getFieldDescribe('Name');
System.debug(fieldDescribe.getLabel()); // Outputs: Account Name

getFieldName

apex
global String getFieldName(SObjectField field)

Retrieves the API name for the given SObjectField token. Delegates to the static getCachedFieldName method to ensure both instance-based and static-based lookups share the same global cache.

Parameters

ParameterTypeDescription
fieldSObjectFieldThe SObjectField token to get the name for.

Returns String — The field's API name, or null if the field is null.

Example

apex
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe('Account');
String fieldName = describe.getFieldName(Account.Name);
System.debug(fieldName); // Outputs: Name

getFields

apex
global UTIL_SObjectDescribe.FieldsMap getFields()

Retrieves a wrapped map of fields with namespace handling for the described object.

Returns UTIL_SObjectDescribe.FieldsMap — A FieldsMap containing the fields of the described object.

Example

apex
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe('Account');
UTIL_SObjectDescribe.FieldsMap fields = describe.getFields();
System.debug(fields.get('Name')?.getDescribe().getLabel()); // Outputs: Name

getFieldSetsMap

apex
global Map<String, FieldSet> getFieldSetsMap()

Retrieves a map of field set names to FieldSet objects for the described object.

Returns FieldSet — A map of field set names to FieldSet objects.

Example

apex
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe('Account');
Map<String, FieldSet> fieldSets = describe.getFieldSetsMap();
System.debug(fieldSets.keySet()); // Outputs: Set of field set names

getFieldsMap

apex
global Map<String, SObjectField> getFieldsMap()

Retrieves a map of field API names to SObjectField instances. Use getFields() for namespace handling.

Returns SObjectField — A map of field API names to SObjectField instances.

Example

apex
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe('Account');
Map<String, SObjectField> fieldMap = describe.getFieldsMap();
System.debug(fieldMap.keySet().size()); // Outputs: Number of fields on Account

getGlobalDescribe

apex
global static UTIL_SObjectDescribe.GlobalDescribeMap getGlobalDescribe()

Retrieves a wrapped map of global SObjectType names to SObjectType instances with namespace handling.

Returns UTIL_SObjectDescribe.GlobalDescribeMap — A GlobalDescribeMap containing global SObjectType data.

Example

apex
UTIL_SObjectDescribe.GlobalDescribeMap globalDescribe = UTIL_SObjectDescribe.getGlobalDescribe();
System.debug(globalDescribe.get('Account')?.getDescribe().getLabel()); // Outputs: Account

getNameField

apex
global SObjectField getNameField()

Retrieves the name field of the SObject (where isNameField() is true).

Returns SObjectField — The SObjectField where isNameField() is true, or null if no such field exists.

Example

apex
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe('Account');
SObjectField nameField = describe.getNameField();
System.debug(nameField.getDescribe().name); // Outputs: Name

getNestableFieldNames

apex
global static Set<String> getNestableFieldNames(String objectName)

Walks the object tree and returns all fields for related objects (one level deep).

Parameters

ParameterTypeDescription
objectNameStringThe name of the starting object

Returns String — A set of fields using multipart dot notation (e.g. Account.Name)

Example

apex
Set<String> fields = UTIL_SObjectDescribe.getNestableFieldNames('Contact');
apex
global static Set<String> getNestableFieldNames(String objectName, Integer levels)

Walks the object tree and returns all fields for related objects up to the specified depth.

Parameters

ParameterTypeDescription
objectNameStringThe name of the starting object
levelsIntegerThe number of levels to traverse (maximum 5)

Returns String — A set of fields using multipart dot notation (e.g. Account.Name)

Example

apex
Set<String> fields = UTIL_SObjectDescribe.getNestableFieldNames('Contact', 2);

getObjectFieldMap

apex
global static Map<String, SObjectField> getObjectFieldMap(SObjectType objectType)

Returns the field map for an object by SObjectType.

Parameters

ParameterTypeDescription
objectTypeSObjectTypeThe type of the object for which to retrieve a field map

Returns SObjectField — A Map of SObject Fields keyed by field API name

Example

apex
Map<String, SObjectField> fields = UTIL_SObjectDescribe.getObjectFieldMap(Account.SObjectType);
apex
global static Map<String, SObjectField> getObjectFieldMap(String objectName)

Returns the field map for an object by API name.

Parameters

ParameterTypeDescription
objectNameStringThe name of the object to retrieve fields for

Returns SObjectField — A Map of SObject Fields keyed by field API name

Example

apex
Map<String, SObjectField> fields = UTIL_SObjectDescribe.getObjectFieldMap('Account');

getObjectFieldReferenceMap

apex
global static Map<String, SObjectField> getObjectFieldReferenceMap(String objectName)

Returns a map of reference (lookup/master-detail) fields keyed by relationship name.

Parameters

ParameterTypeDescription
objectNameStringThe name of the object to retrieve fields map for

Returns SObjectField — A Map of SObject Fields keyed by relationship name; non-reference fields are excluded

Example

apex
Map<String, SObjectField> refFields = UTIL_SObjectDescribe.getObjectFieldReferenceMap('Contact');

getObjectNameFromId

apex
global static String getObjectNameFromId(Id objectId)

Returns the API name of an object based on its ID.

Parameters

ParameterTypeDescription
objectIdIdAn Id of an SObject

Returns String — The API name of the object, or null if the ID is invalid.

Example

apex
String objectName = UTIL_SObjectDescribe.getObjectNameFromId(recordId); // e.g. 'Account'

getObjectNameFromType

apex
global static String getObjectNameFromType(SObjectType objectType)

Will get the API name for the SObject given a specific type. Uses a describe cache

Parameters

ParameterTypeDescription
objectTypeSObjectTypeThe object type for which to get the name

Returns String — The Name of the object

Example

apex
String result = UTIL_SObjectDescribe.getObjectNameFromType(Account.SObjectType);

See Also UTIL_SObjectDescribe.getDescribe

getPicklistEntries

apex
global static List<PicklistEntry> getPicklistEntries(SObjectField pickField)

Returns a list of picklist entries for a given SObject field reference.

Parameters

ParameterTypeDescription
pickFieldSObjectFieldSObject Field reference for the picklist field

Returns PicklistEntry — List of picklist entries

Example

apex
List<PicklistEntry> entries = UTIL_SObjectDescribe.getPicklistEntries(Account.Industry);

getPicklistEntriesMap

apex
global static Map<String, String> getPicklistEntriesMap(SObjectField pickField)

Returns a map of picklist label-to-value pairs for a given SObject field reference.

Parameters

ParameterTypeDescription
pickFieldSObjectFieldThe Object Field which to retrieve entries

Returns String — A map of picklist values keyed by label

Example

apex
Map<String, String> valueByLabel = UTIL_SObjectDescribe.getPicklistEntriesMap(Account.Industry);
apex
global static Map<String, String> getPicklistEntriesMap(SObjectField pickField, Boolean useLabelAsKey)

Returns a map of picklist entries with configurable key for a given SObject field reference.

Parameters

ParameterTypeDescription
pickFieldSObjectFieldThe Object Field for which to retrieve entries
useLabelAsKeyBooleanIf true the key will be the picklist label, else it will be the API value

Returns String — A map of the found items

Example

apex
Map<String, String> labelByValue = UTIL_SObjectDescribe.getPicklistEntriesMap(Account.Industry, false);

getPicklistValues

apex
global static List<DTO_PickList> getPicklistValues(String objectApiName, Id recordTypeId)

Returns all picklist values for a given object and record type combination as a list of DTO_PickList objects, including dependent picklist relationships. Uses ConnectApi.RecordUi natively and transforms the results into framework DTOs.

Parameters

ParameterTypeDescription
objectApiNameStringThe API name of the SObject
recordTypeIdIdThe record type ID

Returns DTO_PickList — A list of DTO_PickList objects containing picklist names and their values

Example

apex
List<DTO_PickList> pickLists = UTIL_SObjectDescribe.getPicklistValues('Account', recordTypeId);

getPicklistValuesByRecordType

apex
global static ConnectApi.PicklistValuesCollection getPicklistValuesByRecordType(String objectApiName, Id recordTypeId)

Returns all picklist values for a given object and record type combination, including dependent picklist relationships. Uses ConnectApi.RecordUi natively.

Parameters

ParameterTypeDescription
objectApiNameStringThe API name of the SObject
recordTypeIdIdThe record type ID

Returns The ConnectApi picklist values collection

Example

apex
ConnectApi.PicklistValuesCollection collection = UTIL_SObjectDescribe.getPicklistValuesByRecordType('Account', recordTypeId);

getRawGlobalDescribe

apex
global static Map<String, SObjectType> getRawGlobalDescribe()

Retrieves a cached map of global SObjectType names to SObjectType instances.

Returns SObjectType — A map of SObjectType names to SObjectType instances.

Example

apex
Map<String, SObjectType> globalDescribe = UTIL_SObjectDescribe.getRawGlobalDescribe();
System.debug(globalDescribe.containsKey('Account')); // Outputs: true

getRecordTypeByDeveloperName

apex
global static Id getRecordTypeByDeveloperName(SObjectType objectType, String recordTypeName)

Retrieves the Id of a record type for an SObject by its developer name.

Parameters

ParameterTypeDescription
objectTypeSObjectTypeThe SObject type.
recordTypeNameStringThe developer name of the record type.

Returns Id — The Id of the record type, or null if not found.

Example

apex
Id recordTypeId = UTIL_SObjectDescribe.getRecordTypeByDeveloperName(Account.SObjectType, 'Customer');

getSObjectFieldNames

apex
global static Set<String> getSObjectFieldNames(Set<SObjectField> objectFields)

Converts a set of SObjectField tokens to their API name strings.

Parameters

ParameterTypeDescription
objectFieldsSetThe fields for which to get field names

Returns String — A set of SObject field API names

Example

apex
Set<SObjectField> fields = new Set<SObjectField>{ Account.Name, Account.Industry };
Set<String> names = UTIL_SObjectDescribe.getSObjectFieldNames(fields); // {'Name', 'Industry'}

getSObjectType

apex
global SObjectType getSObjectType()

Returns the SObjectType this instance is based on, useful for retrieving metadata about the specific object.

Returns SObjectType — The SObjectType of the described object.

Example

apex
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe('Account');
SObjectType objType = describe.getSObjectType();
System.debug(objType); // Outputs: Account

getSObjectTypeById

apex
global static SObjectType getSObjectTypeById(Id objectId)

Returns the SObjectType token for a given record ID.

Parameters

ParameterTypeDescription
objectIdIdAn Id of an SObject

Returns SObjectType — The SObjectType for the record, or null if the ID is null.

Example

apex
SObjectType accountType = UTIL_SObjectDescribe.getSObjectTypeById(someAccountId);

getSObjectTypeByName

apex
global static SObjectType getSObjectTypeByName(String objectApiName)

Returns the SObjectType token for a given object API name.

Parameters

ParameterTypeDescription
objectApiNameStringThe API name of the object

Returns SObjectType — The corresponding SObjectType or null if the API name is invalid.

Example

apex
SObjectType accountType = UTIL_SObjectDescribe.getSObjectTypeByName('Account');

isPersonAccountEnabled

apex
global static Boolean isPersonAccountEnabled()

Determines whether Person Accounts are enabled by checking for the isPersonAccount field on Account.

Returns Booleantrue if Person Accounts are enabled, false otherwise.

Example

apex
Boolean isPersonAccountEnabled = UTIL_SObjectDescribe.isPersonAccountEnabled();
System.debug(isPersonAccountEnabled); // Outputs: true or false based on org configuration

isRecordTypeAvailable

apex
global static Boolean isRecordTypeAvailable(SObjectType objectType, String recordTypeName)

Reports whether a record type, identified by its developer name, is available to the running user for the given SObject. A record type is available only when it exists, is active, and is assigned to the running user's profile or a permission set. Check this before stamping a RecordTypeId so an insert does not fail with INVALID_CROSS_REFERENCE_KEY when the type is not assigned to the running user — for example a packaged record type that a subscriber has not assigned to the profile the code runs under.

Parameters

ParameterTypeDescription
objectTypeSObjectTypeThe SObject type.
recordTypeNameStringThe developer name of the record type.

Returns Boolean — true when the record type exists and is available to the running user; false otherwise.

Example

apex
if(UTIL_SObjectDescribe.isRecordTypeAvailable(Account.SObjectType, 'Customer'))
{
    account.RecordTypeId = UTIL_SObjectDescribe.getRecordTypeByDeveloperName(Account.SObjectType, 'Customer');
}

Inner Classes

ClassDescription
FieldListBuilderBuilds a comma-separated field list from SObjectField tokens and optional FieldSet definitions.
FieldsMapA subclass of NamespacedAttributeMap for handling field maps returned by DescribeSObjectResult.fields.getMap().
GlobalDescribeMapA subclass of NamespacedAttributeMap for handling global describe data returned by getGlobalDescribe.