I want to populate Mailing address of contact based on the Billing Address of the related Account of the particular contact.Please find the below source code to do this.
Trigger Code
Trigger Code
Trigger CopyBillAddres on Contact(Before insert , before update) { Set<Id> AccountIds =new Set<Id>(); Map<id,account> mapAcc = new Map<id,account>(); for(Contact con :Trigger.new) { AccountIds.add(con.AccountId); } for(account a : [select id,BillingStreet,BillingCity,BillingState,BillingPostalCode,BillingCountry from account where id in : AccountIds]) { mapAcc.put(a.id,a); } for(Contact con :Trigger.new) { con.MailingStreet = mapAcc.get(con.AccountId).BillingStreet; con.MailingCity = mapAcc.get(con.AccountId).BillingCity; con.MailingState = mapAcc.get(con.AccountId).BillingState; con.MailingPostalCode = mapAcc.get(con.AccountId).BillingPostalCode; con.mailingCountry = mapAcc.get(con.AccountId).BillingCountry; } }
Notes
- If you want change any field values go with before triggers only.
- In before triggers if you were making any changes to fields explicitly no need to write DML statement
No comments:
Post a Comment