+ Start a Discussion
ankitsrivastav7771.3911494159052937E12ankitsrivastav7771.3911494159052937E12 

Need Help Writing Test Class for Trigger Avoiding Duplicate Account

trigger AccountDuplicatePreventer on Account(before insert,before update)
{
    Set<String> vSetNewPersonEmail = new Set<String>();
    Map<String, Account> OldAccounttMap = new Map<String, Account>();
    Map<String, Account> NewAccountMap = new Map<String, Account>();
   
    for(Account a: trigger.new){
   
        NewAccountMap.put(a.PersonEmail,a);
    }
    List<Account> vLstOldAccount = [SELECT Id,name,PersonEmail FROM Account WHERE isPersonAccount = true AND PersonEmail in: NewAccountMap.keyset() ];
    system.debug('this is test'+vLstOldAccount );
    If(vLstOldAccount.size()>0){
       
    for(Account b: vLstOldAccount){
        OldAccounttMap.put(b.PersonEmail,b);
    }
    for(Account a: trigger.new){
  
           a.PersonEmail.adderror('There is already another Account with the same PersonEmail.' +
                'Refer: <a href=\'/' +
                OldAccounttMap.get(a.PersonEmail) + '\'>' +
                OldAccounttMap.get(a.PersonEmail).Name + '</a>',
                FALSE
            );

 
}
}


Test Class

@isTest(SeeAllData=True)
Public Class TestcontactDuplicatePreventer
{
Static testMethod Void contactDuplicatePreventer()
{
   Account ac= new Account();
   ac.LastName='xyz';
   ac.Email__c='test@sales.com';
   ac.PersonEmail='test@ibm.com';
}
}
Best Answer chosen by ankitsrivastav7771.3911494159052937E12
Denis VakulishinDenis Vakulishin
I am talking about "best practices". You should use SeeAllData=true only when you have to access existing data.
Try test mehod like this 
Public Class TestcontactDuplicatePreventer
{
   Static testMethod Void contactDuplicatePreventer()
   {
      Boolean result = false;
      Account firstAcc = new Account(LastName='xyz',Email__c='test@testDomain.com',PersonEmail='tes@smwElse.com');
      insert firstAcc;
      try{
           Account secondAcc = new Account(LastName='xyz',Email__c='test@testDomain.com',PersonEmail='tes@smwElse.com');
           insert secondAcc;
       }catch(DmlException ex){ result = true;}
      System.assert(result);
   }
}

All Answers

Denis VakulishinDenis Vakulishin
Hi,
I recomend you to remove (SeeAllData=True) and create first Account manually in test.

The second(duplicated account) insert wrap into try-catch block and Assert that code reached catch statement.
Hope it helps.
Regards,
Denis Vakulishin
ankitsrivastav7771.3911494159052937E12ankitsrivastav7771.3911494159052937E12
hey Denis did not get that? Are you asking me to create one more account Record?
Denis VakulishinDenis Vakulishin
I am talking about "best practices". You should use SeeAllData=true only when you have to access existing data.
Try test mehod like this 
Public Class TestcontactDuplicatePreventer
{
   Static testMethod Void contactDuplicatePreventer()
   {
      Boolean result = false;
      Account firstAcc = new Account(LastName='xyz',Email__c='test@testDomain.com',PersonEmail='tes@smwElse.com');
      insert firstAcc;
      try{
           Account secondAcc = new Account(LastName='xyz',Email__c='test@testDomain.com',PersonEmail='tes@smwElse.com');
           insert secondAcc;
       }catch(DmlException ex){ result = true;}
      System.assert(result);
   }
}

This was selected as the best answer