Skip to content

UTIL_ValidationRule.INT_BulkValidationContext ​

Class · Group: Validation

Learn it: Fast Start - Custom Validations and Validation guide

apex
global interface UTIL_ValidationRule.INT_BulkValidationContext

Known Derived Types: UTIL_FormulaContext.FoobarBulkContext

Optional interface for context classes that load data once per batch. Validation rules and trigger action entry formulas call preLoad() once before checking the records. Implement this in addition to INT_SObjectFormulaEvaluationContext when your context class needs to query related data. The preLoad() method is called once per batch of records BEFORE the record loop, allowing you to perform bulk queries and cache results for use in setContext().

Example

apex
global class VAL_AccountWithContactsContext
    implements UTIL_FormulaFilter.INT_SObjectFormulaEvaluationContext,
               UTIL_ValidationRule.INT_BulkValidationContext
{
    private Map<Id, Integer> contactCountByAccountId = new Map<Id, Integer>();
    global Integer ContactCount { get; set; }
    global void preLoad(List<SObject> newRecords, List<SObject> oldRecords)
    {
        Set<Id> accountIds = UTIL_SObject.extractIds(newRecords);
        List<AggregateResult> results = QRY_Builder.selectFrom(Contact.SObjectType)
            .count('Id')
            .groupBy(Contact.AccountId)
            .condition(Contact.AccountId).isIn(new List<Id>(accountIds))
            .toList();
        for(AggregateResult result : results)
        {
            contactCountByAccountId.put((Id)result.get('AccountId'), (Integer)result.get('count_Id'));
        }
    }
    global void setContext(SObject oldRecord, SObject newRecord)
    {
        Account account = (Account)newRecord;
        this.ContactCount = contactCountByAccountId.get(account.Id) ?? 0;
    }
}

Methods ​

MethodDescription
global abstract void preLoad(List<SObject> newRecords, List<SObject> oldRecords)Called once per batch before the record loop.

preLoad ​

apex
global abstract void preLoad(List<SObject> newRecords, List<SObject> oldRecords)

Called once per batch before the record loop. Use this to perform bulk queries and cache results.

Parameters

ParameterTypeDescription
newRecordsListThe new records, or the deleted records on a delete.
oldRecordsSObjectThe old records, or null on an insert, an undelete or a delete.