Apex Trigger
trigger UpdateAccountBillingAddress on Contact (after update) {
Set<Id> accountIds = new Set<Id>();
for (Contact contact : Trigger.new) {
if (contact.MailingStreet != Trigger.oldMap.get(contact.Id).MailingStreet ||
contact.MailingCity != Trigger.oldMap.get(contact.Id).MailingCity ||
contact.MailingState != Trigger.oldMap.get(contact.Id).MailingState ||
contact.MailingPostalCode != Trigger.oldMap.get(contact.Id).MailingPostalCode ||
contact.MailingCountry != Trigger.oldMap.get(contact.Id).MailingCountry) {
accountIds.add(contact.AccountId);
}
}
List<Account> accounts = [SELECT Id, BillingStreet, BillingCity, BillingState, BillingPostalCode, BillingCountry FROM Account WHERE Id IN :accountIds];
for (Account account : accounts) {
for (Contact contact : Trigger.new) {
if (account.Id == contact.AccountId) {
account.BillingStreet = contact.MailingStreet;
account.BillingCity = contact.MailingCity;
account.BillingState = contact.MailingState;
account.BillingPostalCode = contact.MailingPostalCode;
account.BillingCountry = contact.MailingCountry;
}
}
}
update accounts;
}
Explanation
This trigger first gets the IDs of all accounts whose related contact's mailing address was updated. Then, it retrieves the billing addresses of these accounts and updates them with the corresponding values from the contact's mailing address. Finally, it saves the updated accounts back to the database.
Note that this trigger is defined as an "after update" trigger, which means that it will run after a contact's mailing address has been updated and saved to the database.
Video
Video does not exists.