User Story
If you update the phone number in the account's phone field, update the same phone number in related contacts if they do not have a phone number.
Code
Apex Trigger
Trigger UpdatePhoneNumber on Account (before update) {
UpdatePhoneNumberHandler.updatePhoneNumber(Trigger.new,Trigger.oldMap);
}
Apex Handler Class
public class UpdatePhoneNumberHandler {
public void updatePhoneNumber(List<Account> newAccounts, Map<Id, Account> oldAccountsMap){
List<Contact> contactsWithoutPhone = [SELECT Id, Phone FROM Contact WHERE AccountId IN :newAccounts AND Phone = null];
for(Account acc : newAccounts){
if(acc.Phone != oldAccountsMap.get(acc.Id).Phone) {
for(Contact con : contactsWithoutPhone){
if(con.AccountId == acc.Id) {
con.Phone = acc.Phone;
}
}
}
}
update contactsWithoutPhone;
}
}
Video
Video does not exists.