Skip to main content
Abhishek Pande (LTM) 님이 #Accounts에 글을 올렸습니다

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일 오후 1: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