@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])
{
{
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
3 commenti