This trigger is to show the use of Map in apex. Here we are updating the contact's type field with the corresponding parent(Account)'s name.
for ex:
if Account name = 'Prestige';
Contact name = 'contact 3'
then the 'Type' field from the contact will get updated with
Contact Type - 'Prestige - Contact'
for ex:
if Account name = 'Prestige';
Contact name = 'contact 3'
then the 'Type' field from the contact will get updated with
Contact Type - 'Prestige - Contact'
1 2 3 4 5 6 7 8 9 10 11 12 13 | trigger UpdateContactFieldWithAccountUsingMap on Contact (before insert) {
set<id> Accountids = New set<id>();
for(contact con : trigger.new){
Accountids.add(con.AccountId);
}
map<Id,Account> IdAccountMap = new map<Id, Account>([select id, name from account where id in : Accountids]);
for(contact con : trigger.new){
if(con.accountId!=null && IdAccountMap.containskey(con.accountId)){
con.Title = IdAccountMap.get(con.AccountId).name + '- Contact';
}
}
}
|
No comments:
Post a Comment