Whenever an account phone is modified, update all the contacts of the account - Contacts other Phone as OldPhone of Account - Contacts Mobile Phone as NewPhone of Account
Thanks Suraj i am suffering from 3 hours thanks for help can you please help me in one more question here it is . If you delete any account record corresponding child contact records to be assigned to a Particular account. (Associate Contact record to another Account) Thanks in Advance
Nilesh,
You can do it like this.
All Answers
Trigger:-
apex:-
In case you find any other issue please mention.
If you find your Solution then mark this as the best answer.
Thanks and Regards
Suraj Tripathi.
Hi Nilesh,
Actually I have added One extra condition i.e when you are updating the Account and if there is a blank in phone then what will happen.
so Please note updated code.
Can you please mark it as the Best Answer so that other people would take reference from it .
Thank You so much
here it is . If you delete any account record corresponding child contact records to be assigned to a Particular account.
(Associate Contact record to another Account)
Thanks in Advance
Your requirement is when an Account is deleted then their child linked to another account.
Yeah, I can do it but After deleting the account their child would be linked to which account?
Any account we can assing by id any specifit account
Nilesh,
You can do it like this.
trigger AccountContactPhoneTrigger on Account (before update) {
AccountContactPhoneTriggerHelper.updatePhoneOfContact(Trigger.new, Trigger.oldMap);
}
//Helper Class
public class AccountContactPhoneTriggerHelper{
public static void updatePhoneOfContact(List<Account> newAccountList,Map<Id,Account> oldAccountMap){
List <Contact> updatedContactList = new List<Contact>();
for(Account acc : newAccountList){
if(acc.Phone != oldAccountMap.get(acc.Id).Phone){
List<Contact> contactList = [Select Phone, AccountId From Contact WHERE AccountId IN : oldAccountMap.keySet()];
for(Contact con : contactList){
if(con.AccountId == acc.Id){
con.NewPhone = acc.Phone;
con.OldPhone = oldAccountMap.get(acc.Id).Phone;
updatedContactList.add(con);
}
}
}
}
update updatedContactList;
}
}