function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Angel30Angel30 

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.
ArleneArlene
Hi, Deepshika!  Hope you're having fun with Apex.   You are looping through all the records and adding an error to each record.  If you only want an error on the record that has a duplicate, you'll need to add an "if" statement inside your loop to check whether each account has a duplicate.  I would have written something like the following for the 2nd half of the method (warning: not tested!):

// 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');
    }
}
Narender Singh(Nads)Narender Singh(Nads)
Hi Deepshika,
You can simplify your code like this:
public static void preventduplicateAccountsMethod(List<Account> accList){
    
    account[] AllAccountList=[select name from account];// Getting all the account records from database
    for(account a:acclist ){
        if(AllAccountList.contains(a.name)){
            a.addError('cannot create account with same name');            
        }
    }
}

Let me know if it helps
Thanks!
Ajay K DubediAjay K Dubedi
Hi Deepshikha 

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