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.

Since: 1.0

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 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

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:

  • objects (List) - The SObjects from which to extract Ids

Returns: Id - Extracted Ids, could include a null

Since: 1.0

Example:

apex
Set<Id> accountIds = UTIL_SObject.extractIds(accounts);

extractIds

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:

  • objects (List) - The SObjects from which to extract Ids
  • ignoreNulls (SObject) - Set to true to exclude null Ids

Returns: Id - Extracted Ids

Since: 1.0

Example:

apex
Set<Id> accountIds = UTIL_SObject.extractIds(accounts, true);

extractIds

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:

  • objects (List) - The SObjects from which to extract Ids
  • objectField (SObject) - The field token containing the Id

Returns: Id - Extracted Ids, could include a null

Since: 1.0

Example:

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

extractIds

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:

  • objects (List) - The SObjects from which to extract Ids
  • objectField (SObject) - The field token containing the Id
  • ignoreNulls (SObjectField) - Set to true to exclude null Ids

Returns: Id - Extracted Ids

Since: 1.0

Example:

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

extractIds

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:

  • objects (List) - The SObjects from which to extract Ids
  • fieldName (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:

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

extractIds

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:

  • objects (List) - The SObjects from which to extract Ids
  • fieldName (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:

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:

  • objects (List) - The SObjects from which to extract values
  • objectField (SObject) - The field token to extract

Returns: String - A set of string values (includes blanks)

Since: 1.0

Example:

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

extractUniqueValues

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:

  • objects (List) - The SObjects from which to extract values
  • objectField (SObject) - The field token to extract
  • ignoreBlankValues (SObjectField) - Set to true to exclude blank values

Returns: String - A set of string values

Since: 1.0

Example:

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

extractUniqueValues

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:

  • objects (List) - The SObjects from which to extract values
  • fieldName (SObject) - The API name of the field (supports dot-notation)

Returns: String - A set of string values (includes blanks)

Since: 1.0

Example:

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

extractUniqueValues

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:

  • objects (List) - The SObjects from which to extract values
  • fieldName (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:

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:

  • objects (List) - The SObjects from which to extract values
  • objectField (SObject) - The field token to extract

Returns: String - A list of string values (includes blanks)

Since: 1.0

Example:

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

extractValues

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:

  • objects (List) - The SObjects from which to extract values
  • objectField (SObject) - The field token to extract
  • ignoreBlankValues (SObjectField) - Set to true to exclude blank values

Returns: String - A list of string values

Since: 1.0

Example:

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

extractValues

apex
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 values
  • fieldName (SObject) - The API name of the field (supports dot-notation)

Returns: String - A list of string values (includes blanks)

Since: 1.0

Example:

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

extractValues

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:

  • objects (List) - The SObjects from which to extract values
  • fieldName (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:

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:

  • objects (List) - The SObjects to filter
  • filter (SObject) - A map of field API names and their expected values

Returns: SObject - Matching SObjects

Since: 1.0

Example:

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

findWhere

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:

  • objects (List) - The SObjects to filter
  • fieldName (SObject) - The field API name to match
  • value (String) - The value to match against

Returns: SObject - Matching SObjects

Since: 1.0

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:

  • objects (List) - The SObjects to filter
  • fieldName (SObject) - The field API name to match
  • values (String) - The values to match against

Returns: SObject - Matching SObjects

Since: 1.0

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:

  • 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:

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

getChangedRecords

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:

  • 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:

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:

  • 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:

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:

  • objectList (List) - The SObjects to group
  • keyField (SObject) - The field token for the key

Returns: SObject - A map of Id to list of SObjects

Since: 1.0

Example:

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

groupByKey

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:

  • objectList (List) - The SObjects to group
  • keyFieldName (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:

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:

  • objectList (List) - The SObjects to group
  • keyField (SObject) - The field token for the key

Returns: SObject - A map of String to list of SObjects

Since: 1.0

Example:

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

groupByStringKey

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:

  • objectList (List) - The SObjects to group
  • keyFieldName (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:

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:

  • 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:

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:

  • objectList (List) - The SObjects to map
  • keyField (SObject) - The field token for the key

Returns: SObject - A map of Id to SObject

Since: 1.0

Example:

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

indexById

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:

  • objectList (List) - The SObjects to map
  • keyFieldName (SObject) - The API name of the Id field (supports dot-notation)

Returns: SObject - A map of Id to SObject

Since: 1.0

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:

  • objectList (List) - The SObjects to map
  • keyField (SObject) - The field token for the key

Returns: SObject - A map of String to SObject

Since: 1.0

Example:

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

indexByStringKey

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:

  • objectList (List) - The SObjects to map
  • keyFieldName (SObject) - The API name of the String field (supports dot-notation)

Returns: SObject - A map of String to SObject

Since: 1.0

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:

  • 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:

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:

  • objects (List) - The SObjects to filter
  • filter (SObject) - A map of fields and values that cause exclusion

Returns: SObject - SObjects not matching the exclusion criteria

Since: 1.0

Example:

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

omitWhere

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:

  • objects (List) - The SObjects to filter
  • fieldName (SObject) - The field API name to match for exclusion
  • value (String) - The value that causes exclusion

Returns: SObject - SObjects not matching the exclusion criteria

Since: 1.0

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:

  • stringId (String) - A string containing the ID

Returns: Id - Either a valid ID or null

Since: 1.0

Example:

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