UTIL_SObjectDescribe
Class · Group: Utilities
global inherited sharing class UTIL_SObjectDescribeA 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.
Since: 1.0
Example:
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe(Account.SObjectType);
SObjectField nameField = describe.getNameField();
String fieldName = UTIL_SObjectDescribe.getCachedFieldName(Account.Industry);Methods
| Method | Description |
|---|---|
| global static Set extractValidObjectFieldNames(String objectName, 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 getAllFieldNames(SObjectType objectType) | Returns all queryable field names for an object by SObjectType (includes record type fields). |
| global static Set getAllFieldNames(String objectName) | Returns all queryable field names for an object by API name (includes record type fields). |
| global static Set 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 getFieldSetsMap() | Retrieves a map of field set names to FieldSet objects for the described object. |
| global Map 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 getNestableFieldNames(String objectName) | Walks the object tree and returns all fields for related objects (one level deep). |
| global static Set getNestableFieldNames(String objectName, Integer levels) | Walks the object tree and returns all fields for related objects up to the specified depth. |
| global static Map getObjectFieldMap(SObjectType objectType) | Returns the field map for an object by SObjectType. |
| global static Map getObjectFieldMap(String objectName) | Returns the field map for an object by API name. |
| global static Map 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 getPicklistEntries(SObjectField pickField) | Returns a list of picklist entries for a given SObject field reference. |
| global static Map getPicklistEntriesMap(SObjectField pickField) | Returns a map of picklist label-to-value pairs for a given SObject field reference. |
| global static Map getPicklistEntriesMap(SObjectField pickField, Boolean useLabelAsKey) | Returns a map of picklist entries with configurable key for a given SObject field reference. |
| global static List 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 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 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. |
Inner Classes
| Class | Description |
|---|---|
| FieldListBuilder | Builds a comma-separated field list from SObjectField tokens and optional FieldSet definitions. |
| FieldsMap | A subclass of NamespacedAttributeMap for handling field maps returned by DescribeSObjectResult.fields.getMap(). |
| GlobalDescribeMap | A subclass of NamespacedAttributeMap for handling global describe data returned by getGlobalDescribe. |
Method Details
extractValidObjectFieldNames
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:
Returns: String - A set containing only the valid field names
Since: 1.0
Example:
Set<String> fields = new Set<String>{ 'Name', 'InvalidField__c', 'Industry' };
Set<String> validFields = UTIL_SObjectDescribe.extractValidObjectFieldNames('Account', fields);flushCache
global static void flushCache()Clears the cache of global describe and SObject describe instances to free heap space.
Since: 1.0
Example:
UTIL_SObjectDescribe.flushCache();
System.debug(UTIL_SObjectDescribe.getDescribe('Account')); // Forces new describe instancegetAllFieldNames
global static Set<String> getAllFieldNames(SObjectType objectType)Returns all queryable field names for an object by SObjectType (includes record type fields).
Parameters:
objectType(SObjectType) - The SObjectType
Returns: String - A set of field names for the object
Since: 1.0
Example:
Set<String> fieldNames = UTIL_SObjectDescribe.getAllFieldNames(Account.SObjectType);getAllFieldNames
global static Set<String> getAllFieldNames(String objectName)Returns all queryable field names for an object by API name (includes record type fields).
Parameters:
objectName(String) - The API name of the object
Returns: String - A set of field names for the object
Since: 1.0
Example:
Set<String> fieldNames = UTIL_SObjectDescribe.getAllFieldNames('Account');getAllFieldNames
global static Set<String> getAllFieldNames(String objectName, Boolean includeRecordType)Returns all queryable field names for an object, optionally including record type fields.
Parameters:
objectName(String) - The API name of the objectincludeRecordType(Boolean) - Whether to include RecordType relationship fields
Returns: String - A set of field names for the object
Since: 1.0
Example:
Set<String> fieldNames = UTIL_SObjectDescribe.getAllFieldNames('Account', false);getCachedFieldDescribe
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:
field(SObjectField) - The SObjectField token to describe.
Returns: DescribeFieldResult - The cached DescribeFieldResult for the field, or null if the field is null.
Since: 1.0
Example:
String fieldName = UTIL_SObjectDescribe.getCachedFieldDescribe(Account.Name)?.getName();
System.debug(fieldName); // Outputs: NamegetCachedFieldName
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:
field(SObjectField) - The SObjectField token to get the name for.
Returns: String - The field's API name, or null if the field is null.
Since: 1.0
Example:
String fieldName = UTIL_SObjectDescribe.getCachedFieldName(Account.Name);
System.debug(fieldName); // Outputs: NamegetDefaultPicklistValue
global static String getDefaultPicklistValue(DescribeFieldResult fieldDescribe)Retrieves the default picklist value for a given field describe.
Parameters:
fieldDescribe(DescribeFieldResult) - The DescribeFieldResult to retrieve a picklist value for.
Returns: String - The default picklist value, or null if no default is available.
Since: 1.0
Example:
String picklistValue = UTIL_SObjectDescribe.getDefaultPicklistValue(Foobar__c.Picklist__c.getDescribe());getDefaultPicklistValue
global static String getDefaultPicklistValue(DescribeFieldResult fieldDescribe, Boolean returnFirstEntryIfNoDefault)Retrieves the default picklist value for a given field describe.
Parameters:
fieldDescribe(DescribeFieldResult) - The DescribeFieldResult to retrieve a picklist value for.returnFirstEntryIfNoDefault(Boolean) - If 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.
Since: 1.0
Example:
String picklistValue = UTIL_SObjectDescribe.getDefaultPicklistValue(Foobar__c.Picklist__c.getDescribe(), true);getDefaultPicklistValue
global static String getDefaultPicklistValue(SObjectField objectField)Retrieves the default picklist value for a given SObject field.
Parameters:
objectField(SObjectField) - The SObject field to retrieve a picklist value for.
Returns: String - The default picklist value, or null if no default is available.
Since: 1.0
Example:
String picklistValue = UTIL_SObjectDescribe.getDefaultPicklistValue(Foobar__c.Picklist__c);getDefaultPicklistValue
global static String getDefaultPicklistValue(SObjectField objectField, Boolean returnFirstEntryIfNoDefault)Retrieves the default picklist value for a given SObject field.
Parameters:
objectField(SObjectField) - The SObject field to retrieve a picklist value for.returnFirstEntryIfNoDefault(Boolean) - If 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.
Since: 1.0
Example:
String picklistValue = UTIL_SObjectDescribe.getDefaultPicklistValue(Foobar__c.Picklist__c, true);getDefaultRecordType
global static Id getDefaultRecordType(SObjectType objectType)Retrieves the Id of the default record type for an SObject.
Parameters:
objectType(SObjectType) - The SObject type.
Returns: Id - The Id of the default record type, or null if not found.
Since: 1.0
Example:
Id recordTypeId = UTIL_SObjectDescribe.getDefaultRecordType(Account.SObjectType);getDescribe
global DescribeSObjectResult getDescribe()Retrieves the raw DescribeSObjectResult for the described object.
Returns: DescribeSObjectResult - The DescribeSObjectResult containing detailed metadata for the object.
Since: 1.0
Example:
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe('Account');
DescribeSObjectResult result = describe.getDescribe();
System.debug(result.getLabel()); // Outputs: AccountgetDescribe
global static UTIL_SObjectDescribe getDescribe(DescribeSObjectResult describeResult)Retrieves a cached UTIL_SObjectDescribe instance for the specified DescribeSObjectResult.
Parameters:
describeResult(DescribeSObjectResult) - The DescribeSObjectResult for the SObject.
Returns: UTIL_SObjectDescribe - A UTIL_SObjectDescribe instance, or null if the describe result is null.
Since: 1.0
Example:
DescribeSObjectResult result = Account.SObjectType.getDescribe();
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe(result);
System.debug(describe.getSObjectType()); // Outputs: AccountgetDescribe
global static UTIL_SObjectDescribe getDescribe(SObject instance)Retrieves a cached UTIL_SObjectDescribe instance for the specified SObject instance.
Parameters:
instance(SObject) - The SObject instance.
Returns: UTIL_SObjectDescribe - A UTIL_SObjectDescribe instance, or null if the instance is null.
Since: 1.0
Example:
Account acc = new Account();
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe(acc);
System.debug(describe.getNameField().getDescribe().name); // Outputs: NamegetDescribe
global static UTIL_SObjectDescribe getDescribe(SObjectType objectType)Retrieves a cached UTIL_SObjectDescribe instance for the specified SObject type.
Parameters:
objectType(SObjectType) - The SObject type.
Returns: UTIL_SObjectDescribe - A UTIL_SObjectDescribe instance, or null if the object type is null.
Since: 1.0
Example:
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe(Account.SObjectType);
System.debug(describe.getDescribe().name); // Outputs: AccountgetDescribe
global static UTIL_SObjectDescribe getDescribe(String sObjectName)Retrieves a cached UTIL_SObjectDescribe instance for the specified SObject name.
Parameters:
sObjectName(String) - The API name of the SObject.
Returns: UTIL_SObjectDescribe - A UTIL_SObjectDescribe instance, or null if the SObject name is invalid.
Since: 1.0
Example:
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe('Account');
System.debug(describe.getSObjectType()); // Outputs: AccountgetField
global SObjectField getField(String name)Retrieves an SObjectField by name, with namespace handling enabled by default.
Parameters:
name(String) - The API name of the field to retrieve.
Returns: SObjectField - The SObjectField corresponding to the provided name, or null if not found.
Since: 1.0
Example:
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe('Contact');
SObjectField field = describe.getField('LastName');
System.debug(field.getDescribe().getLabel()); // Outputs: Last NamegetField
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:
fieldName(String) - The API name of the field to retrieve.implyNamespace(Boolean) - Whether to automatically handle namespace prefixes.
Returns: SObjectField - The SObjectField corresponding to the provided name, or null if not found.
Since: 1.0
Example:
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe('Contact');
SObjectField field = describe.getField('Account', false);
System.debug(field?.getDescribe().name); // Outputs: AccountIdgetFieldDescribe
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:
field(SObjectField) - The SObjectField token to describe.
Returns: DescribeFieldResult - The DescribeFieldResult for the field, or null if the field is null.
Since: 1.0
Example:
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe('Account');
DescribeFieldResult fieldDescribe = describe.getFieldDescribe(Account.Name);
System.debug(fieldDescribe.getLabel()); // Outputs: Account NamegetFieldDescribe
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:
fieldName(String) - The API name of the field to describe.
Returns: DescribeFieldResult - The DescribeFieldResult for the field, or null if the field doesn't exist.
Since: 1.0
Example:
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe('Account');
DescribeFieldResult fieldDescribe = describe.getFieldDescribe('Name');
System.debug(fieldDescribe.getLabel()); // Outputs: Account NamegetFieldName
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:
field(SObjectField) - The SObjectField token to get the name for.
Returns: String - The field's API name, or null if the field is null.
Since: 1.0
Example:
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe('Account');
String fieldName = describe.getFieldName(Account.Name);
System.debug(fieldName); // Outputs: NamegetFields
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.
Since: 1.0
Example:
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe('Account');
UTIL_SObjectDescribe.FieldsMap fields = describe.getFields();
System.debug(fields.get('Name')?.getDescribe().getLabel()); // Outputs: NamegetFieldSetsMap
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.
Since: 1.0
Example:
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe('Account');
Map<String, FieldSet> fieldSets = describe.getFieldSetsMap();
System.debug(fieldSets.keySet()); // Outputs: Set of field set namesgetFieldsMap
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.
Since: 1.0
Example:
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe('Account');
Map<String, SObjectField> fieldMap = describe.getFieldsMap();
System.debug(fieldMap.keySet().size()); // Outputs: Number of fields on AccountgetGlobalDescribe
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.
Since: 1.0
Example:
UTIL_SObjectDescribe.GlobalDescribeMap globalDescribe = UTIL_SObjectDescribe.getGlobalDescribe();
System.debug(globalDescribe.get('Account')?.getDescribe().getLabel()); // Outputs: AccountgetNameField
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.
Since: 1.0
Example:
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe('Account');
SObjectField nameField = describe.getNameField();
System.debug(nameField.getDescribe().name); // Outputs: NamegetNestableFieldNames
global static Set<String> getNestableFieldNames(String objectName)Walks the object tree and returns all fields for related objects (one level deep).
Parameters:
objectName(String) - The name of the starting object
Returns: String - A set of fields using multipart dot notation (e.g. Account.Name)
Since: 1.0
Example:
Set<String> fields = UTIL_SObjectDescribe.getNestableFieldNames('Contact');getNestableFieldNames
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:
objectName(String) - The name of the starting objectlevels(Integer) - The number of levels to traverse (maximum 5)
Returns: String - A set of fields using multipart dot notation (e.g. Account.Name)
Since: 1.0
Example:
Set<String> fields = UTIL_SObjectDescribe.getNestableFieldNames('Contact', 2);getObjectFieldMap
global static Map<String, SObjectField> getObjectFieldMap(SObjectType objectType)Returns the field map for an object by SObjectType.
Parameters:
objectType(SObjectType) - The type of the object for which to retrieve a field map
Returns: SObjectField - A Map of SObject Fields keyed by field API name
Since: 1.0
Example:
Map<String, SObjectField> fields = UTIL_SObjectDescribe.getObjectFieldMap(Account.SObjectType);getObjectFieldMap
global static Map<String, SObjectField> getObjectFieldMap(String objectName)Returns the field map for an object by API name.
Parameters:
objectName(String) - The name of the object to retrieve fields for
Returns: SObjectField - A Map of SObject Fields keyed by field API name
Since: 1.0
Example:
Map<String, SObjectField> fields = UTIL_SObjectDescribe.getObjectFieldMap('Account');getObjectFieldReferenceMap
global static Map<String, SObjectField> getObjectFieldReferenceMap(String objectName)Returns a map of reference (lookup/master-detail) fields keyed by relationship name.
Parameters:
objectName(String) - The 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
Since: 1.0
Example:
Map<String, SObjectField> refFields = UTIL_SObjectDescribe.getObjectFieldReferenceMap('Contact');getObjectNameFromId
global static String getObjectNameFromId(Id objectId)Returns the API name of an object based on its ID.
Parameters:
objectId(Id) - An Id of an SObject
Returns: String - The API name of the object, or null if the ID is invalid.
Since: 1.0
Example:
String objectName = UTIL_SObjectDescribe.getObjectNameFromId(recordId); // e.g. 'Account'getObjectNameFromType
global static String getObjectNameFromType(SObjectType objectType)Will get the API name for the SObject given a specific type. Uses a describe cache
Parameters:
objectType(SObjectType) - The object type for which to get the name
Returns: String - The Name of the object
Since: 1.0
Example:
String result = UTIL_SObjectDescribe.getObjectNameFromType(Account.SObjectType);See Also: UTIL_SObjectDescribe.getDescribe
getPicklistEntries
global static List<PicklistEntry> getPicklistEntries(SObjectField pickField)Returns a list of picklist entries for a given SObject field reference.
Parameters:
pickField(SObjectField) - SObject Field reference for the picklist field
Returns: PicklistEntry - List of picklist entries
Since: 1.0
Example:
List<PicklistEntry> entries = UTIL_SObjectDescribe.getPicklistEntries(Account.Industry);getPicklistEntriesMap
global static Map<String, String> getPicklistEntriesMap(SObjectField pickField)Returns a map of picklist label-to-value pairs for a given SObject field reference.
Parameters:
pickField(SObjectField) - The Object Field which to retrieve entries
Returns: String - A map of picklist values keyed by label
Since: 1.0
Example:
Map<String, String> valueByLabel = UTIL_SObjectDescribe.getPicklistEntriesMap(Account.Industry);getPicklistEntriesMap
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:
pickField(SObjectField) - The Object Field for which to retrieve entriesuseLabelAsKey(Boolean) - If true the key will be the picklist label, else it will be the API value
Returns: String - A map of the found items
Since: 1.0
Example:
Map<String, String> labelByValue = UTIL_SObjectDescribe.getPicklistEntriesMap(Account.Industry, false);getPicklistValues
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:
Returns: DTO_PickList - A list of DTO_PickList objects containing picklist names and their values
Since: 1.0
Example:
List<DTO_PickList> pickLists = UTIL_SObjectDescribe.getPicklistValues('Account', recordTypeId);getPicklistValuesByRecordType
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:
Returns: The ConnectApi picklist values collection
Since: 1.0
Example:
ConnectApi.PicklistValuesCollection collection = UTIL_SObjectDescribe.getPicklistValuesByRecordType('Account', recordTypeId);getRawGlobalDescribe
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.
Since: 1.0
Example:
Map<String, SObjectType> globalDescribe = UTIL_SObjectDescribe.getRawGlobalDescribe();
System.debug(globalDescribe.containsKey('Account')); // Outputs: truegetRecordTypeByDeveloperName
global static Id getRecordTypeByDeveloperName(SObjectType objectType, String recordTypeName)Retrieves the Id of a record type for an SObject by its developer name.
Parameters:
objectType(SObjectType) - The SObject type.recordTypeName(String) - The developer name of the record type.
Returns: Id - The Id of the record type, or null if not found.
Since: 1.0
Example:
Id recordTypeId = UTIL_SObjectDescribe.getRecordTypeByDeveloperName(Account.SObjectType, 'Customer');getSObjectFieldNames
global static Set<String> getSObjectFieldNames(Set<SObjectField> objectFields)Converts a set of SObjectField tokens to their API name strings.
Parameters:
objectFields(Set) - The fields for which to get field names
Returns: String - A set of SObject field API names
Since: 1.0
Example:
Set<SObjectField> fields = new Set<SObjectField>{ Account.Name, Account.Industry };
Set<String> names = UTIL_SObjectDescribe.getSObjectFieldNames(fields); // {'Name', 'Industry'}getSObjectType
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.
Since: 1.0
Example:
UTIL_SObjectDescribe describe = UTIL_SObjectDescribe.getDescribe('Account');
SObjectType objType = describe.getSObjectType();
System.debug(objType); // Outputs: AccountgetSObjectTypeById
global static SObjectType getSObjectTypeById(Id objectId)Returns the SObjectType token for a given record ID.
Parameters:
objectId(Id) - An Id of an SObject
Returns: SObjectType - The SObjectType for the record, or null if the ID is null.
Since: 1.0
Example:
SObjectType accountType = UTIL_SObjectDescribe.getSObjectTypeById(someAccountId);getSObjectTypeByName
global static SObjectType getSObjectTypeByName(String objectApiName)Returns the SObjectType token for a given object API name.
Parameters:
objectApiName(String) - The API name of the object
Returns: SObjectType - The corresponding SObjectType or null if the API name is invalid.
Since: 1.0
Example:
SObjectType accountType = UTIL_SObjectDescribe.getSObjectTypeByName('Account');isPersonAccountEnabled
global static Boolean isPersonAccountEnabled()Determines whether Person Accounts are enabled by checking for the isPersonAccount field on Account.
Returns: Boolean - true if Person Accounts are enabled, false otherwise.
Since: 1.0
Example:
Boolean isPersonAccountEnabled = UTIL_SObjectDescribe.isPersonAccountEnabled();
System.debug(isPersonAccountEnabled); // Outputs: true or false based on org configurationisRecordTypeAvailable
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:
objectType(SObjectType) - The SObject type.recordTypeName(String) - The developer name of the record type.
Returns: Boolean - true when the record type exists and is available to the running user; false otherwise.
Since: 1.1
Example:
if(UTIL_SObjectDescribe.isRecordTypeAvailable(Account.SObjectType, 'Customer'))
{
account.RecordTypeId = UTIL_SObjectDescribe.getRecordTypeByDeveloperName(Account.SObjectType, 'Customer');
}