You need to sign in to do that
Don't have an account?

Write Test Class for this Trigger Code
Handler--------------------------
public class UpdateAccAddressTriggerHandler{
public static void changeAccAddress(list<contact> contList){
set<Id> AcctIds=new set<Id>();
list<Account> accList=new list<Account>();
map<Id,Contact> contMap = new map<Id,Contact>();
for(Contact con:contList){
AcctIds.add(con.AccountId);
}
for(Contact con:[Select Id,AccountId, MailingStreet,MailingCity, MailingState, MailingCountry, MailingPostalCode,LastModifiedDate from Contact
WHERE AccountId IN:AcctIds order by LastModifiedDate Desc limit 1]){
if(!contMap.containskey(con.accountId)){
contMap.put(con.AccountId,con);
}
}
for(Account acc:[select id,BillingCity,BillingStreet,BillingCountry,BillingPostalCode from Account where id IN:AcctIds]){
Contact con =contMap.get(acc.id);
acc.BillingStreet=con.MailingStreet;
acc.BillingCity=con.MailingCity;
acc.BillingState=con.MailingState;
acc.BillingPostalCode=con.MailingPostalCode;
acc.BillingCountry=con.MailingCountry;
accList.add(acc);
}
update accList;
}
}
Trigger--------------------------
trigger updateAccAddressTrigger on Contact (after insert,after update) {
if(Trigger.isAfter){
UpdateAccAddressTriggerHandler.changeAccAddress(Trigger.New);
}
}
public class UpdateAccAddressTriggerHandler{
public static void changeAccAddress(list<contact> contList){
set<Id> AcctIds=new set<Id>();
list<Account> accList=new list<Account>();
map<Id,Contact> contMap = new map<Id,Contact>();
for(Contact con:contList){
AcctIds.add(con.AccountId);
}
for(Contact con:[Select Id,AccountId, MailingStreet,MailingCity, MailingState, MailingCountry, MailingPostalCode,LastModifiedDate from Contact
WHERE AccountId IN:AcctIds order by LastModifiedDate Desc limit 1]){
if(!contMap.containskey(con.accountId)){
contMap.put(con.AccountId,con);
}
}
for(Account acc:[select id,BillingCity,BillingStreet,BillingCountry,BillingPostalCode from Account where id IN:AcctIds]){
Contact con =contMap.get(acc.id);
acc.BillingStreet=con.MailingStreet;
acc.BillingCity=con.MailingCity;
acc.BillingState=con.MailingState;
acc.BillingPostalCode=con.MailingPostalCode;
acc.BillingCountry=con.MailingCountry;
accList.add(acc);
}
update accList;
}
}
Trigger--------------------------
trigger updateAccAddressTrigger on Contact (after insert,after update) {
if(Trigger.isAfter){
UpdateAccAddressTriggerHandler.changeAccAddress(Trigger.New);
}
}
Try this code:
Note: Might be some update needed. If it works mark ur ans.
Thanks
Niraj