# Block Deleting Accounts with Open Deals

**Type:** Apex trigger · **Difficulty:** Medium · **Object:** Account · **Topics:** before delete, addError, Bulkification

Practice this problem interactively (write and run your Apex trigger against realistic data): https://www.forcechallenges.com/problems/prevent-delete-with-open-opps

## Problem

Prevent users from deleting an **Account** that still has at least one **open** Opportunity (`IsClosed = false`).

Write a **before delete** trigger that calls `addError('Cannot delete an account with open opportunities')` on each offending record in `Trigger.old`. Other accounts in the same batch must still be deleted.

Use **one** aggregate or semi-join query per chunk — the test deletes 300 accounts at once.

## Constraints

- Use `record.addError(message)` — throwing an exception would fail the whole batch.
- At most 1 SOQL query per 200-record chunk.

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