---
url: https://trigger.beyondthecloud.dev/before-update/validator.md
description: >-
  Reject an update with a record-level or field-level error before it is saved,
  such as a forbidden transition or a cleared value.
---

# BeforeUpdate.Validator

Reject an update before it is saved, like a validation rule written in Apex.

**Signature**

```apex
public interface Validator extends BeforeUpdate.Handler {
    Boolean addErrorOnBeforeUpdateWhen(TriggerTypes.UpdateRecord record);
    void addErrorOnBeforeUpdate(TriggerTypes.RejectableUpdateRecord record);
}
```

* `addErrorOnBeforeUpdateWhen(record)`: called once per record in the chunk. Return `true` to reject the record.
* `addErrorOnBeforeUpdate(record)`: called right after its predicate returns true; it must attach an error to the record, or the library throws.

**Example**

::: code-group

```apex [Skeleton]
public with sharing class ContactValidator implements BeforeUpdate.Validator {
    public Boolean addErrorOnBeforeUpdateWhen(TriggerTypes.UpdateRecord record) {
        return record.isChangedTo(Contact.Email, null);
    }

    public void addErrorOnBeforeUpdate(TriggerTypes.RejectableUpdateRecord record) {
        record.addError(Contact.Email, 'Email cannot be removed.');
    }
}
```

```cls \[Error on a field]
public with sharing class OpportunityWinAmountValidator implements BeforeUpdate.Validator {
    public Boolean addErrorOnBeforeUpdateWhen(TriggerTypes.UpdateRecord record) {
        return record.isChangedTo(Opportunity.StageName, 'Closed Won') && (record.isNull(Opportunity.Amount) || record.lessThanOrEqualTo(Opportunity.Amount, 0));
    }

    public void addErrorOnBeforeUpdate(TriggerTypes.RejectableUpdateRecord record) {
        record.addError(Opportunity.Amount, 'Set a positive Amount before moving this Opportunity to Closed Won.');
    }
}

```

```cls \[Block a transition]
public with sharing class AccountDemotionValidator implements BeforeUpdate.Validator {
    public Boolean addErrorOnBeforeUpdateWhen(TriggerTypes.UpdateRecord record) {
        Account oldAccount = (Account) record.getOldSObject();

        return record.isChangedTo(Account.Type, 'Prospect') && oldAccount.Type?.startsWith('Customer') == true;
    }

    public void addErrorOnBeforeUpdate(TriggerTypes.RejectableUpdateRecord record) {
        Account oldAccount = (Account) record.getOldSObject();

        record.addError(Account.Type, 'This account is already ' + oldAccount.Type + ' and cannot be moved back to Prospect. Close it as Lost instead.');
    }
}

```

:::

## Rules {#rules}

* **Gate on a change.** A Validator runs on every update, nested ones included, with no recursion limit. Without `isChanged`, a record that fails the check cannot be updated at all, not even by automation.
* **Name and address fields lose the field.** On them, `record.addError(field, message)` shows the message at record level. To keep it on the field, call `((Account) record.getNewSObject()).BillingCountry.addError(message)`.
* **Never call `addError` on `getOldSObject()`.** It throws a `FinalException` that nothing in the trigger can catch.
* **Populator wins.** A class that also implements `BeforeUpdate.Populator` runs only as a Populator.

::: warning
No DML. If the handler runs DML or publishes an event, the library throws.
:::

## Test {#test}

```apex
@IsTest
static void addErrorOnBeforeUpdateWithZeroAmount() {
    // Setup
    Opportunity renewal = new Opportunity(StageName = 'Closed Won', Amount = 0);

    TriggerOrchestrator.mock().beforeUpdateFor(OpportunityWinAmountValidator.class).with(renewal, new Opportunity(StageName = 'Negotiation/Review'));

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

    // Verify
    Assert.isTrue(renewal.hasErrors(), 'Winning a deal without an amount should be rejected.');
}
```
