function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
prasanth kumarprasanth kumar 

mass update contacts otherphone when account phone is updated

Program not shownig error, but when i am editing and saving the phone number in account object, the corresponding contacts otherphone number is not changing.  please help.
 
trigger updatecontacts on Account (before update) {
    
    map<id,account> m1=new map<id,account>();
    
    for(integer i=0;i<trigger.new.size();i++)
    {
        if(trigger.old[i].phone!=trigger.new[i].phone)
        {
           m1.put(trigger.old[i].id,trigger.new[i]);
        }
    }
    
    list<contact> cons=new list<contact>();
    for(contact c:[select id,accountid,otherphone from contact where id in:m1.keyset()])
    {
        account a1=m1.get(c.accountid);
        c.otherphone=a1.phone;
        cons.add(c);
    }
    
update cons;
}

 
Best Answer chosen by prasanth kumar
Shrikant BagalShrikant Bagal
Hello prasanth,

As you are performing DML operation on another object, you should write a trigger on "after update"

Please try following code:
 
trigger updatecontacts on Account (after update) {
    
    Map<Id,Account> mapAccount = new Map<Id,Account>();
    
    for(Account acc: Trigger.New)
    {
        if(Trigger.oldMap.get(acc.Id).Phone != acc.Phone)
        {
           mapAccount.put(acc.Id,acc);
        }
    }
    
    List<Contact> lstContacts = new List<Contact>();
    for(Contact con :[SELECT Id, AccountId, OtherPhone FROM Contact where Id IN:mapAccount.KeySet()])
    {
        con.OtherPhone = mapAccount.get(con.AccountId).Phone;
        lstContacts.add(con);
    }    
	update lstContacts;
}

If its helps, please mark as best answer so it will help to other who will serve same problem.
T​hanks! 

All Answers

Shrikant BagalShrikant Bagal
Hello prasanth,

As you are performing DML operation on another object, you should write a trigger on "after update"

Please try following code:
 
trigger updatecontacts on Account (after update) {
    
    Map<Id,Account> mapAccount = new Map<Id,Account>();
    
    for(Account acc: Trigger.New)
    {
        if(Trigger.oldMap.get(acc.Id).Phone != acc.Phone)
        {
           mapAccount.put(acc.Id,acc);
        }
    }
    
    List<Contact> lstContacts = new List<Contact>();
    for(Contact con :[SELECT Id, AccountId, OtherPhone FROM Contact where Id IN:mapAccount.KeySet()])
    {
        con.OtherPhone = mapAccount.get(con.AccountId).Phone;
        lstContacts.add(con);
    }    
	update lstContacts;
}

If its helps, please mark as best answer so it will help to other who will serve same problem.
T​hanks! 
This was selected as the best answer
Alexandre BenitaAlexandre Benita
Hi,

I will suggest modifying your trigger with the "after" keyword for handling your update

Try modifying your code as follows ;
 
trigger updateContactsOtherPhone on Account (after update) {
    
    Set<ID> ids = trigger.new.keySet();
	List<Account> updatedAccounts = [SELECT Id, (select ID, AccountID from Contacts) FROM Account WHERE Id in :ids];
    List<Contact> contactsToUpdate = new List<Contact>();
  
	for (Account parentAccount : updatedAccounts) 
    { 
		for(Contact contactItem : parentAccount.Contacts) 
		{                 
			contactItem.otherphone = parentAccount.phone;
			contactsToUpdate.add(contactItem);
		}
	}
	
	if( !contactsToUpdate.isEmpty )
    {
		update contactsToUpdate;
    }
}

Regards,

Alexandre
prasanth kumarprasanth kumar

Shrikant Bagal,  i changed to after, but not updating.  please help.  i tried this trigger in another dummy developer account also. but not working. 
prasanth kumarprasanth kumar

Shrikant Bagal,   i actually saw this code and modified as per my requirememnt.  please see this original code.

Note:- if i run this origianl trigger then the values are updating, but this code is not for phone number and it is for address.
 
trigger updateContactsOnAddressChange on Account
                                         (before update) {

    // The map allows us to keep track of the accounts that have  
    
    // new addresses  
    
    Map<Id, Account> acctsWithNewAddresses = new Map<Id, Account>();

    // Trigger.new is a list of the Accounts that will be updated  
    
    // This loop iterates over the list, and adds any that have new  
    
    // addresses to the acctsWithNewAddresses map.  
    
    for (Integer i = 0; i < Trigger.new.size(); i++) {
        if (   (Trigger.old[i].ShippingCity != Trigger.new[i].
                                                 ShippingCity)
            || (Trigger.old[i].ShippingCountry != Trigger.new[i].
                                                 ShippingCountry)
            || (Trigger.old[i].ShippingPostalCode != Trigger.new[i].
                                                 ShippingPostalCode)
            || (Trigger.old[i].ShippingState != Trigger.new[i].
                                                 ShippingState)
            || (Trigger.old[i].ShippingStreet != Trigger.new[i].
                                                ShippingStreet))  {
            acctsWithNewAddresses.put(Trigger.old[i].id,
                                      Trigger.new[i]);
        }
    }

    List<Contact> updatedContacts = new List<Contact>();

    //Here we can see two syntatic features of Apex:  
    
    //  1) iterating over an embedded SOQL query  
    
    //  2) binding an array directly to a SOQL query with 'in'  
    

    for (Contact c : [SELECT id, accountId, MailingCity,
                             MailingCountry, MailingPostalCode,
                             MailingState, MailingStreet
                      FROM contact
                      WHERE accountId 
                            in :acctsWithNewAddresses.keySet()]) {
        Account parentAccount = acctsWithNewAddresses.get(c.accountId);
        c.MailingCity = parentAccount.ShippingCity;
        c.MailingCountry = parentAccount.ShippingCountry;
        c.MailingPostalCode = parentAccount.ShippingPostalCode;
        c.MailingState = parentAccount.ShippingState;
        c.MailingStreet = parentAccount.ShippingStreet;

        // Rather than insert the contacts individually, add the  
    
        // contacts to a list and bulk insert it. This makes the  
    
        // trigger run faster and allows us to avoid hitting the  
    
        // governor limit on DML statements  
    
        updatedContacts.add(c);
    }
    update updatedContacts;
}

 
prasanth kumarprasanth kumar
hi dudes, this program is working fine now.   Thanks for helping to me.