You need to sign in to do that
Don't have an account?
Angel30
Duplicate Accounts by name bulkified solution
Hi All,
I am trying to learn apex.I was writing a scenario which verifies and prevents creation of account with the same name. i wrote the below code:
public static void preventduplicateAccountsMethod(List<Account> accList){
System.debug('accList' +accList);
Set<String> accNewSet = new Set<String>();
for(Account acc : accList){
accNewList.add(acc.Name) ;
accNewSet.add(acc.Name) ;
System.debug('accNewList' + accNewList);
}
List<Account> accDupeList = new List<Account>([SELECT Id,Name from Account where Name IN : accNewSet]) ;
System.debug('accDupeList' + accDupeList);
if(accDupeList.size() >0){
for(Account acc : accList){
acc.addError('cannot create account with same name') ;
}
}
}//end of method
This works fine.But when i try uploading few accounts from the csv into the system i get error for all the records which is wrong.I assume that it should throw error only for the record which is a duplicate and not for all.
Could you please help me in this.
I am trying to learn apex.I was writing a scenario which verifies and prevents creation of account with the same name. i wrote the below code:
public static void preventduplicateAccountsMethod(List<Account> accList){
System.debug('accList' +accList);
Set<String> accNewSet = new Set<String>();
for(Account acc : accList){
accNewList.add(acc.Name) ;
accNewSet.add(acc.Name) ;
System.debug('accNewList' + accNewList);
}
List<Account> accDupeList = new List<Account>([SELECT Id,Name from Account where Name IN : accNewSet]) ;
System.debug('accDupeList' + accDupeList);
if(accDupeList.size() >0){
for(Account acc : accList){
acc.addError('cannot create account with same name') ;
}
}
}//end of method
This works fine.But when i try uploading few accounts from the csv into the system i get error for all the records which is wrong.I assume that it should throw error only for the record which is a duplicate and not for all.
Could you please help me in this.
// make a set of names that are already in the database
Set<String> accDupeNames = new Set<String>();
for (Account a : [SELECT Name FROM Account WHERE Name IN :accNewSet]) {
accDupeNames.add(a.Name);
}
// loop through each new account passed in, check whether the name is already in the database, and add an error to the record if so
for (Account acc: accList) {
if (accDupeNames.contains(acc.Name) {
acc.addError('cannot create account with same name');
}
}
You can simplify your code like this:
Let me know if it helps
Thanks!
I was able to figure this out, hope this will help you too:
Helper Class///
public class PreventduplicateAccounts {
public static void preventduplicateAccountsMethod(List<Account> accList){
List<String> str = new List<String>();
for(Account a:accList){
str.add(a.Name);
}
if(str.size() > 0){
List<Account> accList2 = [select Name from Account where Name In:str];
if(acclist22.size() > 0){
accList[0].Name.addError('Duplicate Account Name found');
}
}
}
}
//Trigger Class
trigger AccountTrigger on Account (before insert,before update) {
PreventduplicateAccounts.preventduplicateAccountsMethod(Trigger.New);
}
Thank you
Ajay Dubedi