Trigger on contact to create account with the last name of contact and update contact with related account
trigger ContactAccountCreate on Contact (before insert,before update){
List<String> conlst = new List<String>();
map<string,id> mp = new map<string,id>();
for(Contact con : Trigger.new){
conlst.add(con.lastname);
}
List<Account> aclst = new List<Account>();
for(String lm : conlst){
Account ac = new Account();
ac.Name = lm;
aclst.add(ac);
}
if(aclst.size()>0){
insert aclst;
}
for(Account a : aclst){
mp.put(a.name,a.id);
}
for(Contact con : Trigger.new){
con.AccountId = mp.get(con.lastname);
}
}
Comments
Post a Comment