You need to sign in to do that
Don't have an account?
HARISH S 11
trigger on contact record to fetch the account address
I have the the below code which works very well if I insert a new record, but if I add after update in the trigger it is not working and throws some organizational error. Could somebody please let me know what needs to be done if I need to update the existing records in the org?
trigger updatecontact1 on Contact (after insert){
List<contact> lstConUpdate = new List<Contact>();
set<Id> sAccId = new set<Id>();
for(Contact con: trigger.new){
sAccId.add(con.AccountId);
}
List<Account> lstAccount = [select id, Address_1__c, Address_2__c,City__c,Country__c, (select id,Address_1__c, Address_2__c,City__c,Country__c from contacts) from account where id IN: sAccId];
for(Account acc: lstAccount){
for(Contact con: acc.contacts){
con.Address_1__c = acc.Address_1__c;
con.Address_2__c = acc.Address_2__c;
con.City__c=acc.City__c;
con.Country__c=acc.Country__c;
lstConUpdate.add(con);
}
}
if(lstConUpdate.size() > 0){
update lstConUpdate;
}
}
trigger updatecontact1 on Contact (after insert){
List<contact> lstConUpdate = new List<Contact>();
set<Id> sAccId = new set<Id>();
for(Contact con: trigger.new){
sAccId.add(con.AccountId);
}
List<Account> lstAccount = [select id, Address_1__c, Address_2__c,City__c,Country__c, (select id,Address_1__c, Address_2__c,City__c,Country__c from contacts) from account where id IN: sAccId];
for(Account acc: lstAccount){
for(Contact con: acc.contacts){
con.Address_1__c = acc.Address_1__c;
con.Address_2__c = acc.Address_2__c;
con.City__c=acc.City__c;
con.Country__c=acc.Country__c;
lstConUpdate.add(con);
}
}
if(lstConUpdate.size() > 0){
update lstConUpdate;
}
}
Hi Kiran,
Below is the complete Solution for the same: Regards,
Ajay