You need to sign in to do that
Don't have an account?
Robert 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;
}
}
}
--------------------------------------------------------
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;
}
}
}
Change the Trigger.isBefore to Trigger.IsAfter. see the modified code for trigger below.
Replace the UpdateAddressToContacts method with the below code.
Let me know if you face any issues,
Thanks and Regards,
Sai Krishna Tavva.
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
Update the Soql query as below.
Let me know if you face any errors,
Thanks and Regards,
Sai Krishna Tavva.
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;
}
}
}