User Story
As a user, when I update the Billing Country field on an Account, I want to automatically update the Mailing Country field for all related Contacts. This will ensure that the Mailing Country field is always consistent with the Billing Country field and avoid any data inconsistencies.
Code
trigger UpdateContactsOnAccountUpdate on Account (after update) {
Set<Id> accountIds = new Set<Id>();
for (Account acc : Trigger.new) {
if (acc.BillingCountry != Trigger.oldMap.get(acc.Id).BillingCountry) {
accountIds.add(acc.Id);
}
}
if (accountIds.size() > 0) {
List<Contact> contactsToUpdate = [SELECT Id, MailingCountry, Account.BillingCountry
FROM Contact
WHERE AccountId IN :accountIds];
for (Contact c : contactsToUpdate) {
c.MailingCountry = c.Account.BillingCountry;
}
update contactsToUpdate;
}
}
Video
Video does not exists.