Skip to content

BeforeDelete.Validator ​

Reject a delete before the records are removed, like a validation rule written in Apex.

Signature

apex
public interface Validator extends BeforeDelete.Handler {
    Boolean addErrorOnBeforeDeleteWhen(TriggerTypes.DeleteRecord record);
    void addErrorOnBeforeDelete(TriggerTypes.RejectableDeleteRecord record);
}
  • addErrorOnBeforeDeleteWhen(record): called once per record in the chunk. Return true to reject the record.
  • addErrorOnBeforeDelete(record): called right after its predicate returns true; it must attach an error to the record, or the library throws.

Example

apex
public with sharing class ContactValidator implements BeforeDelete.Validator {
    public Boolean addErrorOnBeforeDeleteWhen(TriggerTypes.DeleteRecord record) {
        return record.isTrue(Contact.DoNotCall);
    }

    public void addErrorOnBeforeDelete(TriggerTypes.RejectableDeleteRecord record) {
        record.addError('A Do Not Call contact cannot be deleted.');
    }
}
apex
public with sharing class AccountDeletionGuardValidator implements BeforeDelete.Validator, BeforeDelete.RelatedQuery {
    public Map<String, BeforeDelete.RecordsProvider> queryRelatedOnBeforeDelete() {
        return new Map<String, BeforeDelete.RecordsProvider>{ 'openOpportunities' => new OpenOpportunitiesProvider() };
    }

    public Boolean addErrorOnBeforeDeleteWhen(TriggerTypes.DeleteRecord record) {
        return record.getRelated('openOpportunities').getFirstWhereKeyEquals(record.getId()) != null;
    }

    public void addErrorOnBeforeDelete(TriggerTypes.RejectableDeleteRecord record) {
        record.addError('Close or move the open opportunities before you delete this account.');
    }

    private without sharing class OpenOpportunitiesProvider implements BeforeDelete.RecordsProvider {
        public List<SObject> query(TriggerTypes.DeleteRecords records) {
            return [SELECT Id, AccountId FROM Opportunity WHERE AccountId IN :records.getIds() AND IsClosed = FALSE];
        }

        public String keyOf(SObject row) {
            return ((Opportunity) row).AccountId;
        }
    }
}

Rules ​

  • Block with record.addError. addError(message) or addError(field, message) attaches the error to the row being deleted, and its delete fails. DML in the predicate or the action throws.
  • Every qualified record must get an error. If addErrorOnBeforeDelete attaches none, the library throws a TriggerTypes.TriggerLibException, even with ContinueOnError.
  • Field errors add up. A record with an error still reaches every later handler in the list. A second record-level error replaces the first, so the library sees no new error and throws.
  • Validator wins. A class that also implements BeforeDelete.Writer runs only as a Validator.

WARNING

A guard must see every row. A with sharing provider misses rows the user cannot see, and a missed row lets the delete through. Never add ContinueOnError to a guard: a swallowed exception lets the delete through too.

Test ​

apex
@IsTest
static void addErrorOnBeforeDeleteWithoutOpenOpportunities() {
    // Setup
    Account acme = new Account(Name = 'Acme');

    TriggerOrchestrator.mock().beforeDeleteFor(AccountDeletionGuardValidator.class).with(acme);

    // Test
    TriggerOrchestrator.runTestFor(new AccountDeletionGuardValidator());

    // Verify
    Assert.isFalse(acme.hasErrors(), 'An account without open opportunities can be deleted.');
}