How to prevent duplicate creation of accounts using triggers ?

629    Asked by ElizabethClark in Salesforce , Asked on Jan 2, 2020
Answered by Elizabeth Clark

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’);

}

}



Your Answer

Interviews

Parent Categories