Hi everyone , - Answers - Salesforce Trailblazer Community
Trailblazer Community
Ask Search:
ANANYA SINGHA ROYANANYA SINGHA ROY 

Hi everyone ,

Whenever a Contact is created/update related to an Account, its address should be updated to Parent Account address fields(on update, only when any address field value change).
How can I do this with apex trigger?
Best Answer chosen by ANANYA SINGHA ROY
Usman AliUsman Ali
Hi Anaya, 

There is another mistake on that bolded line, you were using AccountId but you supposed to use c.Id 
trigger NewTrigger on Contact (After update) 
{
    Set<Id> contactIds = new Set<Id>();
    Map<Id,Contact> contactMap = new Map<Id,Contact>();
    for (contact c : trigger.new) {
        contact old = trigger.oldMap.get(c.Id);
        if (c.mailingStreet     != old.mailingStreet|| 
            c.mailingcity       != old.mailingcity || 
            c.mailingstate      != old.mailingstate ||
            c.mailingpostalcode != old.mailingpostalcode ||
            c.mailingcountry    != old.mailingcountry) {
            contactIds.add(c.accountId);
            contactMap.put(c.AccountId, c);
        }
    }
    if (contactIds.size() > 0) {
        Account[] acc = [SELECT Id FROM Account WHERE Id in :contactIds];
        for (Account a : acc) {
            Contact c = contactMap.get(a.Id);
           a.billingStreet     = c.mailingStreet;      
           a.billingcity       = c.mailingcity;
           a.billingstate      = c.mailingstate;
           a.billingpostalcode = c.mailingpostalcode;
           a.billingcountry    = c.mailingcountry    ;
                    }
        update acc;
    }
}

 

All Answers

Usman AliUsman Ali
Hi Anaya,

You can do with Process builder or record-triggered flow instead of apex trigger. if something is not possible with PB , Workflow or Flow then you should go for an apex trigger
Malika PathakMalika Pathak
Hi ANANYA,
public class updateAccountAddress {
    public static void UpdateAccount(List<Contact> conList){
        Set<Id> setIds=new Set<Id>();
        for(Contact con:conList){
            setIds.add(con.accountId);
        }
        List<Account> accList=new List<Account>();
        accList=[SELECT Id,BillingCity FROM Account Where Id IN :setIds];
        Map<Id,Account> accmap=new Map<Id,Account>(accList);
        Map<Id,List<Contact>> mapvalue=new map<Id,List<contact>>();
        for(Contact con: conList){
            if(!mapValue.containsKey(con.AccountId)){
                mapValue.put(con.AccountId,new List<Contact>());
            }
            mapValue.get(con.AccountId).add(con);
        }
        for(contact con: conList){
            if(mapvalue.containsKey(con.AccountId) && accmap.containsKey(con.AccountId)){
                accmap.get(con.AccountId).BillingCity=con.MailingCity;
                accmap.get(con.AccountId).BillingCountry=con.MailingCountry;
                accmap.get(con.AccountId).BillingPostalCode=con.MailingPostalCode;
                accmap.get(con.AccountId).BillingState=con.MailingState;
                accmap.get(con.AccountId).BillingStreet=con.MailingStreet;
            }
        }
        update accmap.values();
    }

}

if this answer is helpful for you, please mark it as the best answer.
ANANYA SINGHA ROYANANYA SINGHA ROY
Hi  Usman Ali,

Can you tell me what's wrong with this code


trigger NewTrigger on Contact (After update) {
    Set<Id> contactIds = new Set<Id>();
    for (contact c : trigger.new) {
        contact old = trigger.oldMap.get(c.accountId);
        if (c.mailingStreet     != old.mailingStreet|| 
            c.mailingcity       != old.mailingcity || 
            c.mailingstate      != old.mailingstate ||
            c.mailingpostalcode != old.mailingpostalcode ||
            c.mailingcountry    != old.mailingcountry) {
            contactIds.add(c.accountId);
        }
    }
    if (contactIds.size() > 0) {
        Account[] acc = [SELECT Id FROM Account WHERE Id in :contactIds];
        for (Account a : acc) {
            Contact c = trigger.newmap.get(a.Id);
           a.billingStreet     = c.mailingStreet;      
           a.billingcity       = c.mailingcity;
           a.billingstate      = c.mailingstate;
           a.billingpostalcode = c.mailingpostalcode;
           a.billingcountry    = c.mailingcountry    ;
                    }
        update acc;
    }
}    
 
Usman AliUsman Ali
Hi ANaya

This line is wrong, as you writing trigger on Contact and Trigger.newMap holds Map<Id,Contact> means ContactId as key and object a value, I made some adjustments in your code, please see the difference. 

I think I have replied you on other question with more optimized version of trigger.
trigger NewTrigger on Contact (After update) 
{
    Set<Id> contactIds = new Set<Id>();
    Map<Id,Contact> contactMap = new Map<Id,Contact>();
    for (contact c : trigger.new) {
        contact old = trigger.oldMap.get(c.accountId);
        if (c.mailingStreet     != old.mailingStreet|| 
            c.mailingcity       != old.mailingcity || 
            c.mailingstate      != old.mailingstate ||
            c.mailingpostalcode != old.mailingpostalcode ||
            c.mailingcountry    != old.mailingcountry) {
            contactIds.add(c.accountId);
            contactMap.put(c.AccountId, c);
        }
    }
    if (contactIds.size() > 0) {
        Account[] acc = [SELECT Id FROM Account WHERE Id in :contactIds];
        for (Account a : acc) {
            Contact c = contactMap.get(a.Id);
           a.billingStreet     = c.mailingStreet;      
           a.billingcity       = c.mailingcity;
           a.billingstate      = c.mailingstate;
           a.billingpostalcode = c.mailingpostalcode;
           a.billingcountry    = c.mailingcountry    ;
                    }
        update acc;
    }
}

 
Piyush SinghalPiyush Singhal
Hiii ANANYA,
You can use this code also, a little bit updated version of malika pathak's code

public class AccountAddressUpdate {
    public static void UpdateAccount(List<Contact> conList){
        Set<Id> conIds=new Set<Id>();
        for(Contact con:conList)
        {
            conIds.add(con.AccountId);
        }
        LIST<Account> accList=new LIST<Account>();
        accList=[select id,BillingCity,BillingCountry,BillingPostalCode,BillingState,BillingStreet from Account where Id In: conIds ];
        Map<Id,Account> mapAccountIdVsAccount=new Map<Id,Account>();
        for(Account acc:accList)
        { 
            if(!mapAccountIdVsAccount.containsKey(acc.Id))
                mapAccountIdVsAccount.put(acc.Id, acc);
        }
        for(Contact con:conList)
        {
            mapAccountIdVsAccount.get(con.AccountId).BillingCity=con.MailingCity;
            mapAccountIdVsAccount.get(con.AccountId).BillingCountry=con.MailingCountry;
            mapAccountIdVsAccount.get(con.AccountId).BillingPostalCode=con.MailingPostalCode;
            mapAccountIdVsAccount.get(con.AccountId).BillingState=con.MailingState;
            mapAccountIdVsAccount.get(con.AccountId).BillingStreet=con.MailingStreet;      
        }
        update mapAccountIdVsAccount.values();
        
    }   
}
ANANYA SINGHA ROYANANYA SINGHA ROY
User-added image
Hi
Usman Ali,
I am getting this error
Usman AliUsman Ali
Hi Anaya, 

There is another mistake on that bolded line, you were using AccountId but you supposed to use c.Id 
trigger NewTrigger on Contact (After update) 
{
    Set<Id> contactIds = new Set<Id>();
    Map<Id,Contact> contactMap = new Map<Id,Contact>();
    for (contact c : trigger.new) {
        contact old = trigger.oldMap.get(c.Id);
        if (c.mailingStreet     != old.mailingStreet|| 
            c.mailingcity       != old.mailingcity || 
            c.mailingstate      != old.mailingstate ||
            c.mailingpostalcode != old.mailingpostalcode ||
            c.mailingcountry    != old.mailingcountry) {
            contactIds.add(c.accountId);
            contactMap.put(c.AccountId, c);
        }
    }
    if (contactIds.size() > 0) {
        Account[] acc = [SELECT Id FROM Account WHERE Id in :contactIds];
        for (Account a : acc) {
            Contact c = contactMap.get(a.Id);
           a.billingStreet     = c.mailingStreet;      
           a.billingcity       = c.mailingcity;
           a.billingstate      = c.mailingstate;
           a.billingpostalcode = c.mailingpostalcode;
           a.billingcountry    = c.mailingcountry    ;
                    }
        update acc;
    }
}

 
This was selected as the best answer