Skip to content

UTIL_ValidationRule ​

Class · Group: Validation

Learn it: Fast Start - Custom Validations and Validation guide

apex
global inherited sharing class UTIL_ValidationRule

Formula-driven declarative validation framework for advanced validation scenarios that standard Salesforce validation rules cannot handle. This framework enables: Cross-object validation requiring queries Complex conditional logic exceeding native formula limits Validation with sophisticated bypass rules Shadow mode for safe production testing Warning severity that logs but allows save Important: This framework complements standard validation rules. Use native Salesforce validation rules for simple field checks. Use this framework for advanced scenarios.

Example

apex
List<UTIL_ValidationRule.ValidationResult> results =
    UTIL_ValidationRule.validate(Trigger.new, Trigger.old, TriggerOperation.BEFORE_UPDATE);
UTIL_ValidationRule.applyErrors(Trigger.new, results);

See Also: ValidationRuleGroup__mdt, ValidationRule__mdt, TRG_ExecuteValidationRules

Usage Patterns ​

Configure via TRG_ExecuteValidationRules trigger action:

apex
// No code required - configure via TriggerAction__mdt to use TRG_ExecuteValidationRules

Direct Programmatic Usage ​

apex
// Validate records programmatically
List<UTIL_ValidationRule.ValidationResult> results =
    UTIL_ValidationRule.validate(Trigger.new, Trigger.old, TriggerOperation.BEFORE_INSERT);

// Apply errors to records
UTIL_ValidationRule.applyErrors(Trigger.new, results);

Bypass Usage ​

apex
// Bypass all validation for an object
UTIL_ValidationRule.bypassObject('Account');
try
{
    insert accounts;
}
finally
{
    UTIL_ValidationRule.clearBypass('Account');
}

Methods ​

MethodDescription
global static void applyErrors(List<SObject> records, List<UTIL_ValidationRule.ValidationResult> results)Applies validation errors to the trigger records.
global static void bypassGroup(String groupDeveloperName)Turns off every rule in one validation rule group for the rest of the transaction, or until clearBypass or clearAllBypasses is called.
global static void bypassObject(String objectApiName)Bypasses all validation rules for the specified object.
global static void bypassRule(String ruleDeveloperName)Turns off one validation rule for the rest of the transaction, or until clearBypass or clearAllBypasses is called.
global static void clearAllBypasses()Clears all bypasses for the current transaction.
global static void clearBypass(String identifier)Clears a specific bypass (object, group, or rule).
global static void executeForTrigger(List<SObject> newRecords, List<SObject> oldRecords, TriggerOperation operation)Executes validation rules for the current trigger context.
global static Boolean isBypassed(String identifier)Checks if a specific bypass is currently active.
global static List<UTIL_ValidationRule.ValidationResult> validate(List<SObject> newRecords, List<SObject> oldRecords, TriggerOperation operation)Validates a list of records against all applicable validation rules.

applyErrors ​

apex
global static void applyErrors(List<SObject> records, List<UTIL_ValidationRule.ValidationResult> results)

Applies validation errors to the trigger records. Error severity violations call record.addError() to block save. Warning severity violations are logged but don't block save.

Page-level addError calls coalesce in Database.SaveResult. When the validation rule targets the whole record (i.e. kern__ErrorDisplayField__c is blank — page-level error), each violation invokes record.addError(message). Salesforce's platform behaviour for page-level addError is to COALESCE multiple calls on the same record into a SINGLE Database.SaveResult.Error entry — only the LAST rule's message is surfaced via getErrors(). Field-level addError (record.addError(field, message), used when kern__ErrorDisplayField__c is set) is per-field unique and does NOT coalesce.

If you want to see ALL violations on a record, consult the framework's own ValidationResult.errors collection, which preserves every violation regardless of page vs field scope. The Database.SaveResult.getErrors() collection should be treated as a "save blocked" / "save succeeded" boolean, not as a complete error inventory.

Empirically: a record violating two page-level rules (e.g. RequireAccountIndustry + PhoneOrWebsiteRequired) returns getErrors().size() == 1 with only the second rule's message — but ValidationResult.errors.size() == 2 reports both correctly.

Parameters

ParameterTypeDescription
recordsListThe list of records (Trigger.new)
resultsSObjectThe validation results

Example

apex
UTIL_ValidationRule.applyErrors(records, new List<ValidationResult>());

bypassGroup ​

apex
global static void bypassGroup(String groupDeveloperName)

Turns off every rule in one validation rule group for the rest of the transaction, or until clearBypass or clearAllBypasses is called. The name must match the group's DeveloperName exactly, including case. A blank name does nothing. Each bypass is recorded as an audit log entry while the BypassAudit_Enabled feature flag is on, as it is by default.

Parameters

ParameterTypeDescription
groupDeveloperNameStringThe DeveloperName of the ValidationRuleGroup__mdt to bypass

Example

apex
UTIL_ValidationRule.bypassGroup('SalesCloud_Account_Validations');

bypassObject ​

apex
global static void bypassObject(String objectApiName)

Bypasses all validation rules for the specified object. This is the highest level of bypass and takes precedence over group and rule bypasses.

Parameters

ParameterTypeDescription
objectApiNameStringThe API name of the object to bypass (e.g., 'Account')

Example

apex
UTIL_ValidationRule.bypassObject('Account');
try
{
    insert accounts; // No validation rules fire
}
finally
{
    UTIL_ValidationRule.clearBypass('Account');
}

bypassRule ​

apex
global static void bypassRule(String ruleDeveloperName)

Turns off one validation rule for the rest of the transaction, or until clearBypass or clearAllBypasses is called. The name must match the rule's DeveloperName exactly, including case. A blank name does nothing. Each bypass is recorded as an audit log entry while the BypassAudit_Enabled feature flag is on, as it is by default.

Parameters

ParameterTypeDescription
ruleDeveloperNameStringThe DeveloperName of the ValidationRule__mdt to bypass

Example

apex
UTIL_ValidationRule.bypassRule('Account_Requires_Primary_Contact');

clearAllBypasses ​

apex
global static void clearAllBypasses()

Clears all bypasses for the current transaction.

Example

apex
UTIL_ValidationRule.clearAllBypasses();

clearBypass ​

apex
global static void clearBypass(String identifier)

Clears a specific bypass (object, group, or rule). For each bypass type the identifier was actually registered under, emits a validation CLEAR audit entry carrying the matching prefixed target (object:/group:/rule:) so the clear correlates to its originating bypass. An identifier that is not currently bypassed removes nothing and emits nothing.

Parameters

ParameterTypeDescription
identifierStringThe object API name, group DeveloperName, or rule DeveloperName to clear

Example

apex
UTIL_ValidationRule.clearBypass('Account');

executeForTrigger ​

apex
global static void executeForTrigger(List<SObject> newRecords, List<SObject> oldRecords, TriggerOperation operation)

Executes validation rules for the current trigger context. This is the main entry point for trigger-based validation.

Parameters

ParameterTypeDescription
newRecordsListThe list of new records (Trigger.new)
oldRecordsSObjectThe list of old records (Trigger.old), may be null for insert
operationListThe trigger operation

Example

apex
// In a trigger action class
UTIL_ValidationRule.executeForTrigger(newRecords, oldRecords, TriggerOperation.BEFORE_UPDATE);

isBypassed ​

apex
global static Boolean isBypassed(String identifier)

Checks if a specific bypass is currently active.

Parameters

ParameterTypeDescription
identifierStringThe object API name, group DeveloperName, or rule DeveloperName

Returns Boolean — True if bypassed

Example

apex
Boolean result = UTIL_ValidationRule.isBypassed('value');

validate ​

apex
global static List<UTIL_ValidationRule.ValidationResult> validate(List<SObject> newRecords, List<SObject> oldRecords, TriggerOperation operation)

Validates a list of records against all applicable validation rules.

Parameters

ParameterTypeDescription
newRecordsListThe list of new records to validate
oldRecordsSObjectThe list of old records (may be null for insert)
operationListThe trigger operation determining which rules apply

Returns UTIL_ValidationRule.ValidationResult — List of ValidationResult objects, one per record

Example

apex
List<ValidationResult> result = UTIL_ValidationRule.validate(records, records, TriggerOperation.BEFORE_INSERT);

Inner Classes ​

ClassDescription
INT_BulkValidationContextOptional interface for context classes that load data once per batch.
ValidationErrorRepresents a single validation error or warning.
ValidationResultResult of validating a single record.