Skip to content

UTIL_SObject

Class · Group: Utilities

apex
global inherited sharing class UTIL_SObject

SObject runtime operations — filtering, field extraction, list-to-map conversion, and dot-notation field value retrieval.

Example

apex
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

MethodDescription
global static Set<Id> extractIds(List<SObject> objects)Extracts a unique set of Ids from a list of SObjects using the Id field.
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.
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.
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.
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.
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.
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.
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.
global static Set<String> extractUniqueValues(List<SObject> objects, String fieldName)Extracts a unique set of field values as strings from a list of SObjects.
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.
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.
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.
global static List<String> extractValues(List<SObject> objects, String fieldName)Extracts field values as strings from a list of SObjects.
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.
global static List<SObject> findWhere(List<SObject> objects, Map<String, Object> filter)Filters a list of SObjects by multiple field-value matches.
global static List<SObject> findWhere(List<SObject> objects, String fieldName, Object value)Filters a list of SObjects by a single field-value match.
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.
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.
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.
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<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.
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.
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.
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.
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.
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.
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.
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.
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.
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<SObject> omitWhere(List<SObject> objects, Map<String, Object> filter)Removes items from a list where the fields match the filter map.
global static List<SObject> 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

extractIds

apex
global static Set<Id> extractIds(List<SObject> objects)

Extracts a unique set of Ids from a list of SObjects using the Id field.

Parameters

ParameterTypeDescription
objectsListThe SObjects from which to extract Ids

Returns Id — Extracted Ids, could include a null

Example

apex
Set<Id> accountIds = UTIL_SObject.extractIds(accounts);
apex
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

ParameterTypeDescription
objectsListThe SObjects from which to extract Ids
ignoreNullsSObjectSet to true to exclude null Ids

Returns Id — Extracted Ids

Example

apex
Set<Id> accountIds = UTIL_SObject.extractIds(accounts, true);
apex
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

ParameterTypeDescription
objectsListThe SObjects from which to extract Ids
objectFieldSObjectThe field token containing the Id

Returns Id — Extracted Ids, could include a null

Example

apex
Set<Id> accountIds = UTIL_SObject.extractIds(contacts, Contact.AccountId);
apex
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

ParameterTypeDescription
objectsListThe SObjects from which to extract Ids
objectFieldSObjectThe field token containing the Id
ignoreNullsSObjectFieldSet to true to exclude null Ids

Returns Id — Extracted Ids

Example

apex
Set<Id> accountIds = UTIL_SObject.extractIds(contacts, Contact.AccountId, true);
apex
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

ParameterTypeDescription
objectsListThe SObjects from which to extract Ids
fieldNameSObjectThe API name of the field containing the Id (supports dot-notation)

Returns Id — Extracted Ids, could include a null

Example

apex
Set<Id> accountIds = UTIL_SObject.extractIds(contacts, 'AccountId');
apex
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

ParameterTypeDescription
objectsListThe SObjects from which to extract Ids
fieldNameSObjectThe API name of the field containing the Id (supports dot-notation)
ignoreNullsStringSet to true to exclude null Ids

Returns Id — Extracted Ids

Example

apex
Set<Id> accountIds = UTIL_SObject.extractIds(contacts, 'AccountId', true);

extractUniqueValues

apex
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

ParameterTypeDescription
objectsListThe SObjects from which to extract values
objectFieldSObjectThe field token to extract

Returns String — A set of string values (includes blanks)

Example

apex
Set<String> industries = UTIL_SObject.extractUniqueValues(accounts, Account.Industry);
apex
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

ParameterTypeDescription
objectsListThe SObjects from which to extract values
objectFieldSObjectThe field token to extract
ignoreBlankValuesSObjectFieldSet to true to exclude blank values

Returns String — A set of string values

Example

apex
Set<String> industries = UTIL_SObject.extractUniqueValues(accounts, Account.Industry, true);
apex
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

ParameterTypeDescription
objectsListThe SObjects from which to extract values
fieldNameSObjectThe API name of the field (supports dot-notation)

Returns String — A set of string values (includes blanks)

Example

apex
Set<String> industries = UTIL_SObject.extractUniqueValues(accounts, 'Industry');
apex
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

ParameterTypeDescription
objectsListThe SObjects from which to extract values
fieldNameSObjectThe API name of the field (supports dot-notation)
ignoreBlankValuesStringSet to true to exclude blank values

Returns String — A set of string values

Example

apex
Set<String> industries = UTIL_SObject.extractUniqueValues(accounts, 'Industry', true);

extractValues

apex
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

ParameterTypeDescription
objectsListThe SObjects from which to extract values
objectFieldSObjectThe field token to extract

Returns String — A list of string values (includes blanks)

Example

apex
List<String> names = UTIL_SObject.extractValues(accounts, Account.Name);
apex
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

ParameterTypeDescription
objectsListThe SObjects from which to extract values
objectFieldSObjectThe field token to extract
ignoreBlankValuesSObjectFieldSet to true to exclude blank values

Returns String — A list of string values

Example

apex
List<String> names = UTIL_SObject.extractValues(accounts, Account.Name, true);
apex
global static List<String> extractValues(List<SObject> objects, String fieldName)

Extracts field values as strings from a list of SObjects.

Parameters

ParameterTypeDescription
objectsListThe SObjects from which to extract values
fieldNameSObjectThe API name of the field (supports dot-notation)

Returns String — A list of string values (includes blanks)

Example

apex
List<String> names = UTIL_SObject.extractValues(accounts, 'Name');
apex
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

ParameterTypeDescription
objectsListThe SObjects from which to extract values
fieldNameSObjectThe API name of the field (supports dot-notation)
ignoreBlankValuesStringSet to true to exclude blank values

Returns String — A list of string values

Example

apex
List<String> names = UTIL_SObject.extractValues(accounts, 'Name', true);

findWhere

apex
global static List<SObject> findWhere(List<SObject> objects, Map<String, Object> filter)

Filters a list of SObjects by multiple field-value matches.

Parameters

ParameterTypeDescription
objectsListThe SObjects to filter
filterSObjectA map of field API names and their expected values

Returns SObject — Matching SObjects

Example

apex
Map<String, Object> filter = new Map<String, Object>{ 'Industry' => 'Technology', 'Active__c' => true };
List<SObject> matches = UTIL_SObject.findWhere(accounts, filter);
apex
global static List<SObject> findWhere(List<SObject> objects, String fieldName, Object value)

Filters a list of SObjects by a single field-value match.

Parameters

ParameterTypeDescription
objectsListThe SObjects to filter
fieldNameSObjectThe field API name to match
valueStringThe value to match against

Returns SObject — Matching SObjects

Example

apex
List<SObject> techAccounts = UTIL_SObject.findWhere(accounts, 'Industry', 'Technology');

findWhereIn

apex
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

ParameterTypeDescription
objectsListThe SObjects to filter
fieldNameSObjectThe field API name to match
valuesStringThe values to match against

Returns SObject — Matching SObjects

Example

apex
List<Object> targetIndustries = new List<Object>{ 'Technology', 'Finance' };
List<SObject> filtered = UTIL_SObject.findWhereIn(accounts, 'Industry', targetIndustries);

getChangedRecords

apex
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

ParameterTypeDescription
newRecordsListThe new versions of the records (from Trigger.new).
oldRecordsSObjectThe old versions of the records (from Trigger.old).
fieldsListThe 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.

Example

apex
List<SObject> addressChanged = UTIL_SObject.getChangedRecords
(
    newRecords, oldRecords,
    new Set<SObjectField>{ Foobar__c.Text__c, Foobar__c.Email__c }
);
apex
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

ParameterTypeDescription
newRecordsListThe new versions of the records (from Trigger.new).
oldRecordsSObjectThe old versions of the records (from Trigger.old).
fieldListThe field to check for changes.

Returns SObject — A list containing only the new records where the specified field value changed.

Example

apex
List<SObject> statusChanged = UTIL_SObject.getChangedRecords(newRecords, oldRecords, Foobar__c.Status__c);

getFieldValue

apex
global static Object getFieldValue(SObject anObject, String fieldName)

Retrieves the value for a field on an SObject using dot-notation for related objects.

Parameters

ParameterTypeDescription
anObjectSObjectThe SObject (can include related object data)
fieldNameStringA single field name or dot-notation path (e.g. 'Contact.Name')

Returns Object — The value of the field

Example

apex
Object name = UTIL_SObject.getFieldValue(account, 'Name');
Object contactName = UTIL_SObject.getFieldValue(myCase, 'Contact.Name');

groupByKey

apex
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

ParameterTypeDescription
objectListListThe SObjects to group
keyFieldSObjectThe field token for the key

Returns SObject — A map of Id to list of SObjects

Example

apex
Map<Id, List<SObject>> contactsByAccount = UTIL_SObject.groupByKey(contacts, Contact.AccountId);
apex
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

ParameterTypeDescription
objectListListThe SObjects to group
keyFieldNameSObjectThe API name of the Id field (supports dot-notation)

Returns SObject — A map of Id to list of SObjects

Example

apex
Map<Id, List<SObject>> contactsByAccount = UTIL_SObject.groupByKey(contacts, 'AccountId');

groupByStringKey

apex
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

ParameterTypeDescription
objectListListThe SObjects to group
keyFieldSObjectThe field token for the key

Returns SObject — A map of String to list of SObjects

Example

apex
Map<String, List<SObject>> accountsByIndustry = UTIL_SObject.groupByStringKey(accounts, Account.Industry);
apex
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

ParameterTypeDescription
objectListListThe SObjects to group
keyFieldNameSObjectThe API name of the String field (supports dot-notation)

Returns SObject — A map of String to list of SObjects

Example

apex
Map<String, List<SObject>> accountsByIndustry = UTIL_SObject.groupByStringKey(accounts, 'Industry');

hasFieldChanged

apex
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

ParameterTypeDescription
newRecordSObjectThe new version of the record (from Trigger.new).
oldRecordSObjectThe old version of the record (from Trigger.oldMap).
fieldSObjectFieldThe field to check for changes.

Returns Boolean — True if the field value differs between the old and new records.

Example

apex
if(UTIL_SObject.hasFieldChanged(newRecord, oldRecord, Foobar__c.Status__c))
{
    // Handle status change
}

indexById

apex
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

ParameterTypeDescription
objectListListThe SObjects to map
keyFieldSObjectThe field token for the key

Returns SObject — A map of Id to SObject

Example

apex
Map<Id, SObject> accountById = UTIL_SObject.indexById(accounts, Account.Id);
apex
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

ParameterTypeDescription
objectListListThe SObjects to map
keyFieldNameSObjectThe API name of the Id field (supports dot-notation)

Returns SObject — A map of Id to SObject

Example

apex
Map<Id, SObject> accountById = UTIL_SObject.indexById(accounts, 'Id');

indexByStringKey

apex
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

ParameterTypeDescription
objectListListThe SObjects to map
keyFieldSObjectThe field token for the key

Returns SObject — A map of String to SObject

Example

apex
Map<String, SObject> accountByName = UTIL_SObject.indexByStringKey(accounts, Account.Name);
apex
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

ParameterTypeDescription
objectListListThe SObjects to map
keyFieldNameSObjectThe API name of the String field (supports dot-notation)

Returns SObject — A map of String to SObject

Example

apex
Map<String, SObject> accountByName = UTIL_SObject.indexByStringKey(accounts, 'Name');

matches

apex
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

ParameterTypeDescription
objSObjectThe SObject to check.
filterMapMapA map containing field API names and their expected values.

Returns Boolean — True if all fields in the filter map match the corresponding SObject fields.

Example

apex
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); // True

omitWhere

apex
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

ParameterTypeDescription
objectsListThe SObjects to filter
filterSObjectA map of fields and values that cause exclusion

Returns SObject — SObjects not matching the exclusion criteria

Example

apex
Map<String, Object> filter = new Map<String, Object>{ 'Status' => 'Closed', 'IsDeleted' => true };
List<SObject> activeRecords = UTIL_SObject.omitWhere(allRecords, filter);
apex
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

ParameterTypeDescription
objectsListThe SObjects to filter
fieldNameSObjectThe field API name to match for exclusion
valueStringThe value that causes exclusion

Returns SObject — SObjects not matching the exclusion criteria

Example

apex
List<SObject> nonTech = UTIL_SObject.omitWhere(accounts, 'Industry', 'Technology');

validateId

apex
global static Id validateId(String stringId)

Will return an Id if the string is a valid Id else null

Parameters

ParameterTypeDescription
stringIdStringA string containing the ID

Returns Id — Either a valid ID or null

Example

apex
Id result = UTIL_SObject.validateId('001000000000001');