ForceChallenges
ProblemsDailyNotes
Sign in
Problem list
Sign in to submit

Maintain Account Contact and Opportunity Counts

HardApex Triggeron Contact 50 ptsCreated Oct 10, 2026

Write bulk-safe Apex triggers to maintain two custom count fields on Account: Contact_Count__c and Opportunity_Count__c. The Contact trigger must recalculate the Contact count after insert, update, delete, and undelete. The Opportunity trigger must recalculate the Opportunity count after insert, update, delete, and undelete. Handle records reassigned to another Account and reset counts to zero when the last child is removed. Use aggregate SOQL and avoid queries or DML inside loops.

Constraints

  • Create Contact_Count__c and Opportunity_Count__c as Number fields on Account.
  • Recalculate counts for all affected Account IDs, including both old and new Account IDs on update.
  • Use bulk-safe logic and aggregate queries.
  • The platform trigger runner validates the Contact-trigger path; include the corresponding Opportunity-trigger implementation in your submission.
Apex TriggerBulkificationAggregate SOQLContactOpportunityRoll-up
CodeApex
Loading editor…
Bulk insert contacts updates contact counts

Seed data

Account× 5 rows
Name: Count Account {{i}}Contact_Count__c: 0Opportunity_Count__c: 0

DML under test

insertContact× 10 records
LastName: Contact {{i}}AccountId: {{id:Account:i%5+1}}

Expected

  • SELECT COUNT() FROM Account WHERE Contact_Count__c = 2