I have below code
public class AccountHandler {
public static void AccountValidation(list<account> acclist){
set<string>AccName=new set<string>();
set<string>dbnameset=new set<string>();
for(account acc:acclist){
AccName.add(acc.name);
}
for(account acc:[select name from account where name in :AccName])
{
dbnameset.add(acc.name);
}
for(account acc:acclist){
if(dbnameset.contains(acc.Name)){
acc.adderror('you cannot create account with Same name');
}
}
}
}
But if my existing record is like (BrahmanAccount) and i insert a record with name brahmanAccount then its not checking the duplicate.
Please suggest
test Class
@isTest
private class AccountHandler_Test{
@testSetup
static void setupTestData(){
test.startTest();
Account account_Obj = new Account(Name = 'Name395', Type = 'Prospect', Match_Billing_Address__c = false);
Insert account_Obj;
test.stopTest();
}
static testMethod void test_AccountValidation_UseCase1(){
List<Account> account_Obj = [SELECT Name,Type,Match_Billing_Address__c from Account];
System.assertEquals(true,account_Obj.size()>0);
AccountHandler obj01 = new AccountHandler();
AccountHandler.AccountValidation(account_Obj);
}
}