0

hi this is my trigger to test duplicate between two email address which is working fine and also it allows duplicate if the checkbox "allow_duplicate__c" is selected, but there is a problem with test method

my trigger

trigger AccountDuplicatePreventer2 on Account
                               (before insert, before update) {

    Map<String, Account> AccountMap = new Map<String, Account>();
    for (Account Account : System.Trigger.new) {

        // Make sure we don't treat an Email__c address that  
        // isn't changing during an update as a duplicate.  

        if ((Account.Email__c != null) &&
                (System.Trigger.isInsert ||
                (Account.Email__c != 
                    System.Trigger.oldMap.get(Account.Id).Email__c))) {

            // Make sure another new Account isn't also a duplicate  

            if (AccountMap.containsKey(Account.Email__c)) {
                Account.Email__c.addError('Another new Account has the '
                                    + 'same Email address.');
            } else {
                AccountMap.put(Account.Email__c, Account);
            }
       }

       if ((Account.Email1__c != null) &&
                (System.Trigger.isInsert ||
                (Account.Email1__c != 
                    System.Trigger.oldMap.get(Account.Id).Email1__c))) {

            // Make sure another new Account isn't also a duplicate  

            if (AccountMap.containsKey(Account.Email1__c)) {
                Account.Email1__c.addError('Another new Account has the '
                                    + 'same Email address.');
            } else {
                AccountMap.put(Account.Email1__c, Account);
            }
       }
    }

    // Using a single database query, find all the Accounts in  

    // the database that have the same Email__c address as any  

    // of the Accounts being inserted or updated.  

    for (Account Account : [SELECT Email__c FROM Account WHERE Email__c IN :AccountMap.KeySet()]) {

        Account newAccount = AccountMap.get(Account.Email__c);
        if(!Accountmap.get(Account.Email__c).Allow_Duplicate__c){
        newAccount.Allow_Duplicate__c.addError('A Account with this Email aldready exists. '+ 'Select this checkbox to proceed.');
         }                      
    }

    for (Account Account : [SELECT Email1__c FROM Account WHERE Email1__c IN :AccountMap.KeySet()]) {

        Account newAccount = AccountMap.get(Account.Email1__c);
        if(!Accountmap.get(Account.Email1__c).Allow_Duplicate__c){
        newAccount.Allow_Duplicate__c.addError('A Account with this Email aldready exists. '+ 'Select this checkbox to proceed.');
         }                      
    }
}

this is my test class and one method to check email__c

@isTest
private class email_dup1{

    static testmethod void ee_dup(){

      Set<String> testEmailAddress = new Set<String>();
      testEmailAddress.add('[email protected]');
      testEmailAddress.add('[email protected]');
      testEmailAddress.add('[email protected]');
      testEmailAddress.add('[email protected]');
      testEmailAddress.add('[email protected]');
      System.assert([SELECT count() FROM Account WHERE Email__c IN :testEmailAddress] == 0);

      Account Account1 = new Account(LastName='Test1', PersonMobilePhone='123',
                            Email__c='[email protected]');
      Account Account2 = new Account(LastName='Test2', PersonMobilePhone='123',
                            Email__c='[email protected]');
      Account Account3 = new Account(LastName='Test3', PersonMobilePhone='123',
                            Email__c='[email protected]');
      Account[] Accounts = new Account[] {Account1, Account2, Account3};
      insert Accounts;

      Account2.Email__c = '[email protected]';
      Account3.Email__c = '[email protected]';
      update Accounts;

       Account dup1 = new Account(LastName='Test1Dup',
                           PersonMobilePhone='Test1Dup Inc.',allow_duplicate__c = false,
                           Email__c='[email protected]');
      try {
         insert dup1;
         System.assert(false);
      } catch (DmlException e) {
         System.assert(e.getNumDml() == 1);
         System.assert(e.getDmlIndex(0) == 0);
         System.assert(e.getDmlFields(0).size() == 1);
         System.assert(e.getDmlFields(0)[0].getDescribe().getName() == 'Email__c');
         System.assert(e.getDmlMessage(0).indexOf(
            'A Account with this Email aldready exists. '+ 'Select this checkbox to proceed.') > -1);
      }

    }
}

System.AssertException: Assertion Failed

this the error which i'm facing for this line of code

System.assert(e.getDmlFields(0)[0].getDescribe().getName() == 'Email__c');
1
  • for loop has been covered already. Commented Jan 30, 2014 at 11:16

1 Answer 1

1

put Con_Email__pc field values same in all insert account statement. You have made the different.

  for (Account Account : [SELECT Con_Email__pc FROM Account WHERE Con_Email__pc IN :AccountMap.KeySet()]) 
      {
          Account newAccount = AccountMap.get(Account.Con_Email__pc);
          newAccount.Con_Email__pc.addError('Email is already exist');
      }

In this statement you are only checking duplicate for Con_Email__pc field and in test data you have not inserted any duplicate value in Con_Email__pc field.

acc.Con_Email__pc = '[email protected]';
insert acc;
acc1.Con_Email__pc = '[email protected]';
insert acc1;
acc2.Con_Email__pc = '[email protected]';
insert acc2;

Hope this will help.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .