+ Start a Discussion
Robert John LacatanRobert John Lacatan 

Updating the mailing address of Contact object from the Billing Address of Account object

Im new to sales force and I really don't know if my codes are good. But if update the Billing Address it would not update the mailing address in my Contact. can somebody point out what I'm missing? thanks
--------------------------------------------------------
trigger contactFromAccount on Account (after update) {
        ManagingAccountAndContacts begintrigger = new ManagingAccountAndContacts();
        
        if(Trigger.isbefore && Trigger.isupdate){
               begintrigger.isBeforeUpdate(trigger.new);
        }
}

---------------------------------------------------
public class ManagingAccountAndContacts {
        public void isBeforeUpdate(List<Account> accList){
            updateAddressToContacts(accList);
        }
    
        public void updateAddressToContacts(List<Account> accList){
             String accName = '';
            String contactmailingStreet = '';
            String contactmailingCity = '';
            String contactmailingState = '';
            String contactmailingPostalCode = '';
            String contactmailingCountry = '';
            
            for(Account acc:accList){
            accName = acc.Name;
               contactmailingStreet = acc.BillingStreet;  
            contactmailingCity = acc.BillingCity;
            contactmailingState = acc.BillingState;
            contactmailingPostalCode = acc.BillingPostalCode;
            contactmailingCountry = acc.BillingCountry;
            }
        
            List<Contact> contactList =[select MailingStreet,MailingCity, MailingState,MailingPostalCode,MailingCountry 
                         from Contact where Account.name Like:(accName)];
       
            for(Contact con: contactList){
                con.MailingStreet = contactmailingStreet;
             con.MailingCity =  contactmailingCity;
             con.MailingState = contactmailingState;
             con.MailingPostalCode = contactmailingPostalCode;
             con.MailingCountry = contactmailingCountry;
             
               }
        }
}

 
Tavva Sai KrishnaTavva Sai Krishna
Hi Robert,

Change the Trigger.isBefore to Trigger.IsAfter. see the modified code for trigger below.
trigger contactFromAccount on Account (after update) {
        ManagingAccountAndContacts begintrigger = new ManagingAccountAndContacts();
        
        if(Trigger.isafter  && Trigger.isupdate){
               begintrigger.isBeforeUpdate(trigger.new);
        }
}

Replace the UpdateAddressToContacts method with the below code. 
public void updateAddressToContacts(List<Account> accList){
	Map<Id,Account> mapOfIdToAccount = new Map<Id,Account>();
	List<contact>	lstOfUpdcontacts = new List<Contact>();
	for(Account acc:accList){
		mapOfIdToAccount.put(acc.id,acc);
	}

	List<Contact> contactList =[select MailingStreet,MailingCity, MailingState,MailingPostalCode,MailingCountry 
								from Contact where AccountId IN:accList];
   
	for(Contact con: contactList){
		contact con = new Contact(Id = con.Id, MailingStreet = mapOfIdToAccount.get(con.AccountId).BillingStreet,
			MailingCity =  mapOfIdToAccount.get(con.AccountId).BillingCity, MailingState = mapOfIdToAccount.get(con.AccountId).BillingState,
			MailingPostalCode = mapOfIdToAccount.get(con.AccountId).BillingPostalCode, MailingCountry = mapOfIdToAccount.get(con.AccountId).BillingCountry);
		lstOfUpdcontacts.add(con);	 
	}
	if(!lstOfUpdcontacts.IsEmpty()){
		update lstOfUpdcontacts;
	}
}

Let me know if you face any issues,

Thanks and Regards,
Sai Krishna Tavva.
Robert John LacatanRobert John Lacatan
Hi Sai.. thanks for your response.. doing what you said I encounter this error

trying variable duplicate on this part:
for(Contact con: contactList){
12        contact con = new Contact(Id = con.Id, MailingStreet =mapOfIdToAccount.get(con.AccountId).BillingStreet,


so i tried to change the con under the for loop to cons
for(Contact con: contactList){
12        contact cons = new Contact(Id = con.Id, MailingStreet =mapOfIdToAccount.get(con.AccountId).BillingStreet,

upon updating another error says:

Apex trigger contactFromAccount caused an unexpected exception, contact your administrator: contactFromAccount: execution of AfterUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Contact.AccountId: Class.ManagingAccountAndContacts.updateAddressToContacts: line 17, column 1


...

by the way may I know what's missing with my code above? is there somthing wrong with my querry that it wont update the contacts?? thanks
Tavva Sai KrishnaTavva Sai Krishna
Hi Robert,
Update the Soql query as below.
List<Contact> contactList =[select MailingStreet,MailingCity, MailingState,MailingPostalCode,MailingCountry,AccountId
								from Contact where AccountId IN:accList];

Let me know if you face any errors,

Thanks and Regards,
Sai Krishna Tavva.

 

Robert John LacatanRobert John Lacatan
Hi Sai! It is now working. made some changes on the last part. Thank you for your help. Can you tell me why my code on the original post is not working? i really want to know why...

Again, Big Thanks!

public class ManagingAccountAndContacts {
        public void isAfterUpdate(List<Account> accList){
            updateAddressToContacts(accList);
        }
    
        public void updateAddressToContacts(List<Account> accList){
             Map<Id,Account> mapOfIdToAccount = new Map<Id,Account>();
            List<contact>    lstOfUpdcontacts = new List<Contact>();
            for(Account acc:accList){
            mapOfIdToAccount.put(acc.id,acc);// Map the ID of accounts
            }
            //the query below returns all the mailing address of contacts associated to the account being updated
            List<Contact> contactList =[select accountId,MailingStreet,MailingCity, MailingState,MailingPostalCode,MailingCountry 
                                        from Contact where AccountId IN:accList];
               Account newAcc;
            for(Contact con: contactList){
                newAcc = mapOfIdToAccount.get(con.AccountId);// assigns the new value of mailing address
                con.MailingStreet = newAcc.BillingStreet; 
                con.MailingCity = newAcc.BillingCity;
                con.MailingState = newAcc.BillingPostalCode;
                con.MailingCountry = newAcc.BillingCountry;
                
                lstOfUpdcontacts.add(con);     
            }
            if(!lstOfUpdcontacts.IsEmpty()){
            update lstOfUpdcontacts;
            }
        }        
        
}