You need to sign in to do that
Don't have an account?
VijayNi
trigger to update the home address on conatct object if it's updated on account object
Hi Team,
I need to write a trigger to update the address on the contact object if a user select a checkbox on account filed address then copy the same values into conatct object
trigger accountcontact on account(after insert,after update)
{
List<contact> contactupdate list = new List<contact>();
{
for (account a : trigger.new){
contactupdate.add(a);
}
}
List<account> Updatedcontact = new List<account>([select id,name,accountid,(select id,name from contact where accountid in :accountid]);
for(account acc : trigger.new){
if(acc.address =='true')
contact.address = acc.address;
contact.city = accc.city;
contact.pincode= acc.pincode;
contact.district = acc.district;
updatedcontact.add(acc);
}
insert updatedcontact;
}
I need to write a trigger to update the address on the contact object if a user select a checkbox on account filed address then copy the same values into conatct object
trigger accountcontact on account(after insert,after update)
{
List<contact> contactupdate list = new List<contact>();
{
for (account a : trigger.new){
contactupdate.add(a);
}
}
List<account> Updatedcontact = new List<account>([select id,name,accountid,(select id,name from contact where accountid in :accountid]);
for(account acc : trigger.new){
if(acc.address =='true')
contact.address = acc.address;
contact.city = accc.city;
contact.pincode= acc.pincode;
contact.district = acc.district;
updatedcontact.add(acc);
}
insert updatedcontact;
}
Here is the code which suits your requirement.
This trigger is running on the after insert and after update. below is the apex class which is a call from the trigger. When You insert a new account then it will create a new account with the same address otherwise it will update the existing contact address. I hope you find the above solution helpful. If it does, please mark it as the Best Answer to help others too.
Thanks and Regards,
Sachin Arora
www.sachinsf.com
Trigger:-
trigger ContactAddresUpdate on Account (After insert,After Update)
{
ContactAddresUpdate_Handler.Address(Trigger.new);
}
Handler:-
public class ContactAddresUpdate_Handler
{
public static void Address(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 id,AccountId,MailingStreet,MailingCity, MailingState,MailingPostalCode,MailingCountry
from Contact where AccountId IN:AccList];
for(Contact con: contactList){
if(mapOfIdToAccount.get(con.AccountId).Contact_Address__c == True)
{
//con.AccountId = mapOfIdToAccount.get(con.AccountId).Id;
con.MailingStreet = mapOfIdToAccount.get(con.AccountId).BillingStreet;
con.MailingCity = mapOfIdToAccount.get(con.AccountId).BillingCity;
con.MailingState = mapOfIdToAccount.get(con.AccountId).BillingState;
con.MailingPostalCode = mapOfIdToAccount.get(con.AccountId).BillingPostalCode;
con.MailingCountry = mapOfIdToAccount.get(con.AccountId).BillingCountry;
lstOfUpdcontacts.add(con);
}
}
if(!lstOfUpdcontacts.IsEmpty())
{
update lstOfUpdcontacts;
System.debug('lstOfUpdcontactssssssss>>>'+lstOfUpdcontacts);
}
}
}
If you find this solution is helpful for you please mark best answer