How to prevent duplicate creation of accounts using triggers ?
trigger AccountDuplicateTrigger on Account (before insert, before update) {
map account map = new map();
for(account acc : trigger.new){
if((acc.name!=null) && (trigger.isinsert || (acc.name!= trigger.oldmap.get(acc.id).name))){
if(accountmap.containsKey(acc.Name)){
acc.name.adderror(‘account name already exists’);
}else {
accountmap.put(acc.Name, acc);
}
}
}
for(account account : [select id, name from account where name IN : account map.keyset()]){
account accountnewmap = account api.get(account.name);
accountnewmap.Name.adderror(‘account with this name already exixts’);
}
}