UTIL_SObject
Class · Group: Utilities
global inherited sharing class UTIL_SObjectSObject runtime operations — filtering, field extraction, list-to-map conversion, and dot-notation field value retrieval.
Since: 1.0
Example:
Set<Id> accountIds = UTIL_SObject.extractIds(contacts, Contact.AccountId, true);
Map<Id, List<SObject>> contactsByAccount = UTIL_SObject.groupByKey(contacts, Contact.AccountId);
List<SObject> changed = UTIL_SObject.getChangedRecords(newRecords, oldRecords, Account.Name);Methods
| Method | Description |
|---|---|
| global static Set extractIds(List<SObject> objects) | Extracts a unique set of Ids from a list of SObjects using the Id field. |
| global static Set extractIds(List<SObject> objects, Boolean ignoreNulls) | Extracts a unique set of Ids from a list of SObjects using the Id field. |
| global static Set extractIds(List<SObject> objects, SObject objectField) | Extracts a unique set of Ids from a list of SObjects using a typed field token. |
| global static Set extractIds(List<SObject> objects, SObject objectField, Boolean ignoreNulls) | Extracts a unique set of Ids from a list of SObjects using a typed field token with null filtering. |
| global static Set extractIds(List<SObject> objects, String fieldName) | Extracts a unique set of Ids from a list of SObjects using a named field. |
| global static Set extractIds(List<SObject> objects, String fieldName, Boolean ignoreNulls) | Extracts a unique set of Ids from a list of SObjects using a named field with null filtering. |
| global static Set extractUniqueValues(List<SObject> objects, SObject objectField) | Extracts a unique set of field values as strings from a list of SObjects using a typed field token. |
| global static Set extractUniqueValues(List<SObject> objects, SObject objectField, Boolean ignoreBlankValues) | Extracts a unique set of field values as strings from a list of SObjects using a typed field token with blank filtering. |
| global static Set extractUniqueValues(List<SObject> objects, String fieldName) | Extracts a unique set of field values as strings from a list of SObjects. |
| global static Set extractUniqueValues(List<SObject> objects, String fieldName, Boolean ignoreBlankValues) | Extracts a unique set of field values as strings from a list of SObjects with blank filtering. |
| global static List extractValues(List<SObject> objects, SObject objectField) | Extracts field values as strings from a list of SObjects using a typed field token. |
| global static List extractValues(List<SObject> objects, SObject objectField, Boolean ignoreBlankValues) | Extracts field values as strings from a list of SObjects using a typed field token with blank filtering. |
| global static List extractValues(List<SObject> objects, String fieldName) | Extracts field values as strings from a list of SObjects. |
| global static List extractValues(List<SObject> objects, String fieldName, Boolean ignoreBlankValues) | Extracts field values as strings from a list of SObjects with blank filtering. |
| global static List findWhere(List<SObject> objects, Map<String, Object> filter) | Filters a list of SObjects by multiple field-value matches. |
| global static List findWhere(List<SObject> objects, String fieldName, Object value) | Filters a list of SObjects by a single field-value match. |
| global static List findWhereIn(List<List<Object> values) | Filters a list of SObjects where a field value is in the provided list. |
| global static List getChangedRecords(List<List<SObject> oldRecords, SObject> fields) | Returns only the records where any of the specified fields have changed between the old and new versions. |
| global static List getChangedRecords(List<List<SObject> oldRecords, SObject field) | Returns only the records where a specific field value has changed between the old and new versions. |
| global static Object getFieldValue(SObject anObject, String fieldName) | Retrieves the value for a field on an SObject using dot-notation for related objects. |
| global static Map groupByKey(List<List keyField) | Converts a list of SObjects into a grouped map by an Id field using a typed field token. |
| global static Map groupByKey(List<List keyFieldName) | Converts a list of SObjects into a grouped map by an Id field. |
| global static Map groupByStringKey(List<List keyField) | Converts a list of SObjects into a grouped map by a String field using a typed field token. |
| global static Map groupByStringKey(List<List keyFieldName) | Converts a list of SObjects into a grouped map by a String field. |
| global static Boolean hasFieldChanged(SObject newRecord, SObject oldRecord, SObject field) | Checks whether a specific field value has changed between the old and new versions of a record. |
| global static Map indexById(List<List keyField) | Converts a list of SObjects into a single-value map by an Id field using a typed field token. |
| global static Map indexById(List<List keyFieldName) | Converts a list of SObjects into a single-value map by an Id field. |
| global static Map indexByStringKey(List<List keyField) | Converts a list of SObjects into a single-value map by a String field using a typed field token. |
| global static Map indexByStringKey(List<List keyFieldName) | Converts a list of SObjects into a single-value map by a String field. |
| global static Boolean matches(SObject obj, Map<String, Object> filterMap) | Checks if an SObject matches the field-value pairs in the provided filter map. |
| global static List omitWhere(List<SObject> objects, Map<String, Object> filter) | Removes items from a list where the fields match the filter map. |
| global static List omitWhere(List<SObject> objects, String fieldName, Object value) | Removes items from a list where the field matches a single value. |
| global static Id validateId(String stringId) | Will return an Id if the string is a valid Id else null |
Method Details
extractIds
global static Set<Id> extractIds(List<SObject> objects)Extracts a unique set of Ids from a list of SObjects using the Id field.
Parameters:
objects(List) - The SObjects from which to extract Ids
Returns: Id - Extracted Ids, could include a null
Since: 1.0
Example:
Set<Id> accountIds = UTIL_SObject.extractIds(accounts);extractIds
global static Set<Id> extractIds(List<SObject> objects, Boolean ignoreNulls)Extracts a unique set of Ids from a list of SObjects using the Id field.
Parameters:
objects(List) - The SObjects from which to extract IdsignoreNulls(SObject) - Set to true to exclude null Ids
Returns: Id - Extracted Ids
Since: 1.0
Example:
Set<Id> accountIds = UTIL_SObject.extractIds(accounts, true);extractIds
global static Set<Id> extractIds(List<SObject> objects, SObjectField objectField)Extracts a unique set of Ids from a list of SObjects using a typed field token.
Parameters:
objects(List) - The SObjects from which to extract IdsobjectField(SObject) - The field token containing the Id
Returns: Id - Extracted Ids, could include a null
Since: 1.0
Example:
Set<Id> accountIds = UTIL_SObject.extractIds(contacts, Contact.AccountId);extractIds
global static Set<Id> extractIds(List<SObject> objects, SObjectField objectField, Boolean ignoreNulls)Extracts a unique set of Ids from a list of SObjects using a typed field token with null filtering.
Parameters:
objects(List) - The SObjects from which to extract IdsobjectField(SObject) - The field token containing the IdignoreNulls(SObjectField) - Set to true to exclude null Ids
Returns: Id - Extracted Ids
Since: 1.0
Example:
Set<Id> accountIds = UTIL_SObject.extractIds(contacts, Contact.AccountId, true);extractIds
global static Set<Id> extractIds(List<SObject> objects, String fieldName)Extracts a unique set of Ids from a list of SObjects using a named field.
Parameters:
objects(List) - The SObjects from which to extract IdsfieldName(SObject) - The API name of the field containing the Id (supports dot-notation)
Returns: Id - Extracted Ids, could include a null
Since: 1.0
Example:
Set<Id> accountIds = UTIL_SObject.extractIds(contacts, 'AccountId');extractIds
global static Set<Id> extractIds(List<SObject> objects, String fieldName, Boolean ignoreNulls)Extracts a unique set of Ids from a list of SObjects using a named field with null filtering.
Parameters:
objects(List) - The SObjects from which to extract IdsfieldName(SObject) - The API name of the field containing the Id (supports dot-notation)ignoreNulls(String) - Set to true to exclude null Ids
Returns: Id - Extracted Ids
Since: 1.0
Example:
Set<Id> accountIds = UTIL_SObject.extractIds(contacts, 'AccountId', true);extractUniqueValues
global static Set<String> extractUniqueValues(List<SObject> objects, SObjectField objectField)Extracts a unique set of field values as strings from a list of SObjects using a typed field token.
Parameters:
objects(List) - The SObjects from which to extract valuesobjectField(SObject) - The field token to extract
Returns: String - A set of string values (includes blanks)
Since: 1.0
Example:
Set<String> industries = UTIL_SObject.extractUniqueValues(accounts, Account.Industry);extractUniqueValues
global static Set<String> extractUniqueValues(List<SObject> objects, SObjectField objectField, Boolean ignoreBlankValues)Extracts a unique set of field values as strings from a list of SObjects using a typed field token with blank filtering.
Parameters:
objects(List) - The SObjects from which to extract valuesobjectField(SObject) - The field token to extractignoreBlankValues(SObjectField) - Set to true to exclude blank values
Returns: String - A set of string values
Since: 1.0
Example:
Set<String> industries = UTIL_SObject.extractUniqueValues(accounts, Account.Industry, true);extractUniqueValues
global static Set<String> extractUniqueValues(List<SObject> objects, String fieldName)Extracts a unique set of field values as strings from a list of SObjects.
Parameters:
objects(List) - The SObjects from which to extract valuesfieldName(SObject) - The API name of the field (supports dot-notation)
Returns: String - A set of string values (includes blanks)
Since: 1.0
Example:
Set<String> industries = UTIL_SObject.extractUniqueValues(accounts, 'Industry');extractUniqueValues
global static Set<String> extractUniqueValues(List<SObject> objects, String fieldName, Boolean ignoreBlankValues)Extracts a unique set of field values as strings from a list of SObjects with blank filtering.
Parameters:
objects(List) - The SObjects from which to extract valuesfieldName(SObject) - The API name of the field (supports dot-notation)ignoreBlankValues(String) - Set to true to exclude blank values
Returns: String - A set of string values
Since: 1.0
Example:
Set<String> industries = UTIL_SObject.extractUniqueValues(accounts, 'Industry', true);extractValues
global static List<String> extractValues(List<SObject> objects, SObjectField objectField)Extracts field values as strings from a list of SObjects using a typed field token.
Parameters:
objects(List) - The SObjects from which to extract valuesobjectField(SObject) - The field token to extract
Returns: String - A list of string values (includes blanks)
Since: 1.0
Example:
List<String> names = UTIL_SObject.extractValues(accounts, Account.Name);extractValues
global static List<String> extractValues(List<SObject> objects, SObjectField objectField, Boolean ignoreBlankValues)Extracts field values as strings from a list of SObjects using a typed field token with blank filtering.
Parameters:
objects(List) - The SObjects from which to extract valuesobjectField(SObject) - The field token to extractignoreBlankValues(SObjectField) - Set to true to exclude blank values
Returns: String - A list of string values
Since: 1.0
Example:
List<String> names = UTIL_SObject.extractValues(accounts, Account.Name, true);extractValues
global static List<String> extractValues(List<SObject> objects, String fieldName)Extracts field values as strings from a list of SObjects.
Parameters:
objects(List) - The SObjects from which to extract valuesfieldName(SObject) - The API name of the field (supports dot-notation)
Returns: String - A list of string values (includes blanks)
Since: 1.0
Example:
List<String> names = UTIL_SObject.extractValues(accounts, 'Name');extractValues
global static List<String> extractValues(List<SObject> objects, String fieldName, Boolean ignoreBlankValues)Extracts field values as strings from a list of SObjects with blank filtering.
Parameters:
objects(List) - The SObjects from which to extract valuesfieldName(SObject) - The API name of the field (supports dot-notation)ignoreBlankValues(String) - Set to true to exclude blank values
Returns: String - A list of string values
Since: 1.0
Example:
List<String> names = UTIL_SObject.extractValues(accounts, 'Name', true);findWhere
global static List<SObject> findWhere(List<SObject> objects, Map<String, Object> filter)Filters a list of SObjects by multiple field-value matches.
Parameters:
objects(List) - The SObjects to filterfilter(SObject) - A map of field API names and their expected values
Returns: SObject - Matching SObjects
Since: 1.0
Example:
Map<String, Object> filter = new Map<String, Object>{ 'Industry' => 'Technology', 'Active__c' => true };
List<SObject> matches = UTIL_SObject.findWhere(accounts, filter);findWhere
global static List<SObject> findWhere(List<SObject> objects, String fieldName, Object value)Filters a list of SObjects by a single field-value match.
Parameters:
objects(List) - The SObjects to filterfieldName(SObject) - The field API name to matchvalue(String) - The value to match against
Returns: SObject - Matching SObjects
Since: 1.0
Example:
List<SObject> techAccounts = UTIL_SObject.findWhere(accounts, 'Industry', 'Technology');findWhereIn
global static List<SObject> findWhereIn(List<SObject> objects, String fieldName, List<Object> values)Filters a list of SObjects where a field value is in the provided list.
Parameters:
objects(List) - The SObjects to filterfieldName(SObject) - The field API name to matchvalues(String) - The values to match against
Returns: SObject - Matching SObjects
Since: 1.0
Example:
List<Object> targetIndustries = new List<Object>{ 'Technology', 'Finance' };
List<SObject> filtered = UTIL_SObject.findWhereIn(accounts, 'Industry', targetIndustries);getChangedRecords
global static List<SObject> getChangedRecords(List<SObject> newRecords, List<SObject> oldRecords, Set<SObjectField> fields)Returns only the records where any of the specified fields have changed between the old and new versions. Compares each record in the new list against the corresponding record in the old list by index.
Parameters:
newRecords(List) - The new versions of the records (from Trigger.new).oldRecords(SObject) - The old versions of the records (from Trigger.old).fields(List) - The set of fields to check for changes. A record is included if any field changed.
Returns: SObject - A list containing only the new records where at least one of the specified fields changed.
Since: 1.0
Example:
List<SObject> addressChanged = UTIL_SObject.getChangedRecords
(
newRecords, oldRecords,
new Set<SObjectField>{ Foobar__c.Text__c, Foobar__c.Email__c }
);getChangedRecords
global static List<SObject> getChangedRecords(List<SObject> newRecords, List<SObject> oldRecords, SObjectField field)Returns only the records where a specific field value has changed between the old and new versions. Compares each record in the new list against the corresponding record in the old list by index.
Parameters:
newRecords(List) - The new versions of the records (from Trigger.new).oldRecords(SObject) - The old versions of the records (from Trigger.old).field(List) - The field to check for changes.
Returns: SObject - A list containing only the new records where the specified field value changed.
Since: 1.0
Example:
List<SObject> statusChanged = UTIL_SObject.getChangedRecords(newRecords, oldRecords, Foobar__c.Status__c);getFieldValue
global static Object getFieldValue(SObject anObject, String fieldName)Retrieves the value for a field on an SObject using dot-notation for related objects.
Parameters:
anObject(SObject) - The SObject (can include related object data)fieldName(String) - A single field name or dot-notation path (e.g. 'Contact.Name')
Returns: Object - The value of the field
Since: 1.0
Example:
Object name = UTIL_SObject.getFieldValue(account, 'Name');
Object contactName = UTIL_SObject.getFieldValue(myCase, 'Contact.Name');groupByKey
global static Map<Id, List<SObject>> groupByKey(List<SObject> objectList, SObjectField keyField)Converts a list of SObjects into a grouped map by an Id field using a typed field token.
Parameters:
Returns: SObject - A map of Id to list of SObjects
Since: 1.0
Example:
Map<Id, List<SObject>> contactsByAccount = UTIL_SObject.groupByKey(contacts, Contact.AccountId);groupByKey
global static Map<Id, List<SObject>> groupByKey(List<SObject> objectList, String keyFieldName)Converts a list of SObjects into a grouped map by an Id field.
Parameters:
objectList(List) - The SObjects to groupkeyFieldName(SObject) - The API name of the Id field (supports dot-notation)
Returns: SObject - A map of Id to list of SObjects
Since: 1.0
Example:
Map<Id, List<SObject>> contactsByAccount = UTIL_SObject.groupByKey(contacts, 'AccountId');groupByStringKey
global static Map<String, List<SObject>> groupByStringKey(List<SObject> objectList, SObjectField keyField)Converts a list of SObjects into a grouped map by a String field using a typed field token.
Parameters:
Returns: SObject - A map of String to list of SObjects
Since: 1.0
Example:
Map<String, List<SObject>> accountsByIndustry = UTIL_SObject.groupByStringKey(accounts, Account.Industry);groupByStringKey
global static Map<String, List<SObject>> groupByStringKey(List<SObject> objectList, String keyFieldName)Converts a list of SObjects into a grouped map by a String field.
Parameters:
objectList(List) - The SObjects to groupkeyFieldName(SObject) - The API name of the String field (supports dot-notation)
Returns: SObject - A map of String to list of SObjects
Since: 1.0
Example:
Map<String, List<SObject>> accountsByIndustry = UTIL_SObject.groupByStringKey(accounts, 'Industry');hasFieldChanged
global static Boolean hasFieldChanged(SObject newRecord, SObject oldRecord, SObjectField field)Checks whether a specific field value has changed between the old and new versions of a record.
Parameters:
newRecord(SObject) - The new version of the record (from Trigger.new).oldRecord(SObject) - The old version of the record (from Trigger.oldMap).field(SObjectField) - The field to check for changes.
Returns: Boolean - True if the field value differs between the old and new records.
Since: 1.0
Example:
if(UTIL_SObject.hasFieldChanged(newRecord, oldRecord, Foobar__c.Status__c))
{
// Handle status change
}indexById
global static Map<Id, SObject> indexById(List<SObject> objectList, SObjectField keyField)Converts a list of SObjects into a single-value map by an Id field using a typed field token.
Parameters:
Returns: SObject - A map of Id to SObject
Since: 1.0
Example:
Map<Id, SObject> accountById = UTIL_SObject.indexById(accounts, Account.Id);indexById
global static Map<Id, SObject> indexById(List<SObject> objectList, String keyFieldName)Converts a list of SObjects into a single-value map by an Id field.
Parameters:
objectList(List) - The SObjects to mapkeyFieldName(SObject) - The API name of the Id field (supports dot-notation)
Returns: SObject - A map of Id to SObject
Since: 1.0
Example:
Map<Id, SObject> accountById = UTIL_SObject.indexById(accounts, 'Id');indexByStringKey
global static Map<String, SObject> indexByStringKey(List<SObject> objectList, SObjectField keyField)Converts a list of SObjects into a single-value map by a String field using a typed field token.
Parameters:
Returns: SObject - A map of String to SObject
Since: 1.0
Example:
Map<String, SObject> accountByName = UTIL_SObject.indexByStringKey(accounts, Account.Name);indexByStringKey
global static Map<String, SObject> indexByStringKey(List<SObject> objectList, String keyFieldName)Converts a list of SObjects into a single-value map by a String field.
Parameters:
objectList(List) - The SObjects to mapkeyFieldName(SObject) - The API name of the String field (supports dot-notation)
Returns: SObject - A map of String to SObject
Since: 1.0
Example:
Map<String, SObject> accountByName = UTIL_SObject.indexByStringKey(accounts, 'Name');matches
global static Boolean matches(SObject obj, Map<String, Object> filterMap)Checks if an SObject matches the field-value pairs in the provided filter map.
Parameters:
obj(SObject) - The SObject to check.filterMap(Map) - A map containing field API names and their expected values.
Returns: Boolean - True if all fields in the filter map match the corresponding SObject fields.
Since: 1.0
Example:
SObject account = new Account(Name = 'Test Account', Industry = 'Technology');
Map<String, Object> filterMap = new Map<String, Object>{'Name' => 'Test Account', 'Industry' => 'Technology'};
Boolean doesMatch = UTIL_SObject.matches(account, filterMap); // TrueomitWhere
global static List<SObject> omitWhere(List<SObject> objects, Map<String, Object> filter)Removes items from a list where the fields match the filter map.
Parameters:
objects(List) - The SObjects to filterfilter(SObject) - A map of fields and values that cause exclusion
Returns: SObject - SObjects not matching the exclusion criteria
Since: 1.0
Example:
Map<String, Object> filter = new Map<String, Object>{ 'Status' => 'Closed', 'IsDeleted' => true };
List<SObject> activeRecords = UTIL_SObject.omitWhere(allRecords, filter);omitWhere
global static List<SObject> omitWhere(List<SObject> objects, String fieldName, Object value)Removes items from a list where the field matches a single value.
Parameters:
objects(List) - The SObjects to filterfieldName(SObject) - The field API name to match for exclusionvalue(String) - The value that causes exclusion
Returns: SObject - SObjects not matching the exclusion criteria
Since: 1.0
Example:
List<SObject> nonTech = UTIL_SObject.omitWhere(accounts, 'Industry', 'Technology');validateId
global static Id validateId(String stringId)Will return an Id if the string is a valid Id else null
Parameters:
stringId(String) - A string containing the ID
Returns: Id - Either a valid ID or null
Since: 1.0
Example:
Id result = UTIL_SObject.validateId('001000000000001');