This is the trigger i have wrote to prevent the creation of duplicate Accounts based on Account Name.
trigger preventDuplicateAccount on Account(before insert,before update){
set<string> accountSet = new set<string>();
Map<string,Account> accountMap = new Map<string,Account>();
for(Account ac:trigger.new){
accountSet.add(ac.name);
}
for(Account a:[SELECT id,name from Account where Name IN :accountSet]){
accountMap.put(a.Name,a);
}
for(Account account:trigger.new){
if(accountMap.containsKey(account.name) ){
//show error msg on the Account Name field
account.Name.addError('Account is Already Exist');
}
}
}
Thank you for sharing wonderful information with us to get some idea about that content.
ReplyDeleteCPQ Certification
CPQ Certification Salesforce