# How do addError() methods work inside triggers, and can they be applied to specific fields?

**Topic:** Apex Trigger · **Difficulty:** Medium · **Asked at:** TCS, Deloitte, Capgemini · **Tags:** apex-trigger, trigger, validation

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-adderror-methods-work-inside-triggers-and-can-they-be-applied-to-specific-fields

## Answer

`addError(String msg)` marks a record (or a field) as failed with a custom message and blocks that record's save.

- Record level: `acc.addError('Cannot save');` shown at top of page.
- Field level: `acc.Phone.addError('Phone is invalid');` shown next to the field.
- In delete triggers use it on `Trigger.old` records.
- In bulk operations with `allOrNone=false`, only the flagged records fail; with the default, the whole DML rolls back.
- Use `addError(msg, false)` to disable HTML escaping only when the message is trusted.

It is typically used in before triggers for validations.

---
Source: ForceChallenges · last updated 2026-10-21
