You need to sign in to do that
Don't have an account?
LoriJ
Trigger to update contact address and phone number if account address and phone number change.
Would someone be able to help me write a trigger so that when an account address and phone number change, the address and phone number on all contacts change? In my perfect world, the phone number would only change on the contact IF before the update they were the same number. Since I am not a code writer other than when I have to be, I am not sure that is even possible.
Thanks!
It certainly is possible. Here's a rough draft:
This is a standard "aggregate-query-update" style trigger. First, we gather all accounts that have changed, then we query for all contacts for those accounts and see if they matched the old values, and if so, add them to a list to update, and finally we update the records. Here's a probable test method to go with it.
You might want to make it update either the entire address or not at all instead of field by field, but I thought I'd make it simple to give you an idea that it certainly can be done.
Sorry for my delay in expressing my appreciation. Thank you for the help! This definitely gets me going!
hi,
I have a similar trigger the only difference is that it triggers before insert and after update, and I'm writting a test and I checked the coverage is 18%, I can't see to cover line 17, line 21, line 25, line 30 to 37, line 45 and line 47.
I get an error in my debug log that says :
*******Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updateContactsOnAddressChange: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object**********
This is my trigger:
this is my Test:
Appreciate any help,
newbee
Thank you. After thinking about this, it made sense to remove "before insert" so I did and it covers my trigger.
i saw u r works really u r thiniking soo fast and hard work ...
public static void ChangePhone( List<Account> lstAcc)
{
Contact c = new Contact();
for(Account a : lstAcc)
{
c = [Select phone from Contact where AccountId = :a.Id LIMIT 1];
c.phone = a.Phone;
update(c);
}
Trigger:
trigger Trigger_6 on Account (after update )
{
Trigger_Practice_1.ChangePhone(trigger.new);
}