Sync Contact City When Account Billing City Changes
MediumApex Triggeron Account 25 ptsCreated Oct 13, 2026
When an Account's Billing City changes, update the custom Mailing_City_Copy__c field on all related Contacts to match. Only update Contacts whose city is different. Use one query and one update for the batch.
Constraints
Use bulk-safe logic. Do not run SOQL or DML inside loops.
AccountContactAfter UpdateBulkification
CodeApex
Loading editor…
Contact city follows account city
Seed data
Account× 1 rows
Name: City AccountBillingCity: Old City
Contact× 2 rows
LastName: City Contact {{i}}AccountId: {{id:Account:1}}Mailing_City_Copy__c: Old City
DML under test
updateAccountWHERE Name = 'City Account'
SET
BillingCity: Mumbai
Expected
SELECT COUNT() FROM Contact WHERE Mailing_City_Copy__c = 'Mumbai'