as soon a I create account record I want to update the contact record's mailing address field. I have written trigger on account because if I update account address field automatically related contacts address field should update I have written this trigger m not finding result.
trigger createopp on Account (after insert,after update) {
List<Contact> opp = New List<Contact>();
set<id> ids=new set<id>();
for(Account con:trigger.new) {
list<Contact> oppsi=[select id,mailingstreet,mailingcity,mailingstate,mailingpostalcode from contact where accountid :con.id ];
if(oppsi.size()==0){
Contact opp1 = new Contact();
opp1.AccountID= con.id;
opp1.mailingstreet = con.Shippingstreet;
opp1.mailingcity = con.Shippingcity;
opp1.mailingstate = con.Shippingstate;
opp1.mailingpostalcode =con.shippingpostalcode;
opp.add(opp1);
}
}
try {
insert opp;
} catch(Dmlexception e) {}
}
Best Answer
Please see code below and comments inside
Read articles about trigger best practice: