Skip to main content

When Billing address in Contact record is updated , it should update the billing address of all the contacts for that Account .

Please help on this.

 

#Triggers #Accounts #Contacts

3 条评论
  1. 2023年1月11日 13:03

    @Abhishek Pande Copy paste this below code and try to update contact.

    Try to choose those contact whose Account have more than one Contact. Before Activating trigger keep Mailing Address blank

     

    trigger updateContactAddress contact (after update){

            if(trigger.isAfter && trigger.isUpdate){

               set<Id> accountIdSet = new set<Id>();

                for(Contact con : trigger.new){

                    if(con.MailingCity != trigger.oldMap.get(con.Id).MailingCity ||

                       con.MailingCountry != trigger.oldMap.get(con.Id).MailingCountry ||

                       con.MailingState != trigger.oldMap.get(con.Id).MailingState ||

                       con.MailingStreet != trigger.oldMap.get(con.Id).MailingStreet ||

                       con.MailingPostalCode != trigger.oldMap.get(con.Id).MailingPostalCode)

                    {

                        accountIdSet.add(con.AccountId);

                    }

                }

                System.debug('Account List= ' +accountIdSet);               

                list<Contact> conListToUpdate = new list<Contact>();

                for(contact conNew  : trigger.new){

                    for(contact conOld : [Select Id, lastName, AccountId, MailingAddress, MailingCity, 

                                          MailingCountry, MailingState, MailingStreet, MailingPostalCode 

                                          from Contact where AccountId in:accountIdSet])

                    {

                        if(conOld.Id != conNew.Id )

                        {

                            conOld.MailingCity = trigger.newMap.get(conNew.Id).MailingCity;

                            conOld.MailingCountry = trigger.newMap.get(conNew.Id).MailingCountry;

                            conOld.MailingState = trigger.newMap.get(conNew.Id).MailingState;

                            conOld.MailingStreet = trigger.newMap.get(conNew.Id).MailingStreet;

                            conOld.MailingPostalCode = trigger.newMap.get(conNew.Id).MailingPostalCode;

                            conListToUpdate.add(conOld);

                        }

                    }

                }

                update conListToUpdate;

                System.debug('Updated Contact List = ' +conListToUpdate);

        }

    }

     

    Please mark this answer as best if it helps . Thanks

0/9000