# When an Account is marked Inactive__c = true, automatically close all related open Cases and mark open Opportunities as Closed Lost.

**Topic:** Development · **Difficulty:** Medium · **Asked at:** Accenture, Deloitte, TCS · **Tags:** apex-trigger, trigger, coding, scenario

Read it on the site: https://www.forcechallenges.com/interview-questions/when-an-account-is-marked-inactive-c-true-automatically-close-all-related-open-cases-and-mark-open

## Answer

Use `after update` on Account, detect `Inactive__c` changing to true, and update related Cases and Opportunities in bulk.

```apex
trigger AccountTrigger on Account (after update) {
    Set<Id> inactive = new Set<Id>();
    for (Account a : Trigger.new) if (a.Inactive__c && !Trigger.oldMap.get(a.Id).Inactive__c) inactive.add(a.Id);
    if (inactive.isEmpty()) return;
    List<Case> cases = [SELECT Id FROM Case WHERE AccountId IN :inactive AND IsClosed = false];
    for (Case c : cases) c.Status = 'Closed';
    List<Opportunity> opps = [SELECT Id FROM Opportunity WHERE AccountId IN :inactive AND IsClosed = false];
    for (Opportunity o : opps) o.StageName = 'Closed Lost';
    update cases; update opps;
}
```
For large volumes, hand off to Queueable or Batch Apex.

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