+ Start a Discussion
Neha KakrooNeha Kakroo 

Insert failed: FIELD_CUSTOM_VALIDATION_EXCEPTION in Test Class

I am writing a test class for a trigger that tries to identify account duplicates in the system based on Account Name, Street & Phone matching or Name & Phone matching or Name & Street matching. The test class that I have written is giving me 100% coverage, but I am getting 1 test failure:

 

Message: System.AssertException: Assertion Failed: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, <BR/>Agency record with this Name/Phone/Physical Street exists in the system. If you wish to create agency record, change field value of "Create New Record" to YES.<BR/><BR/> Potential Duplicate Agencies Include:<BR/>Agencies with Name, Phone & Physical Street match: <a href=https://cs14.salesforce.com/001c0000004PPquAAG>Agency000</a> | <BR/>Agencies with Name & Phone match: <a href=https://cs14.salesforce.com/001c0000004PPquAAG>Agency000</a> | <BR/>Agencies with Phone & Physical Street match: <a href=https://cs14.salesforce.com/001c0000004PPquAAG>Agency000</a> | <BR/>Agencies with Name & Physical Street match: <a href=https://cs14.salesforce.com/001c0000004PPquAAG>Agency000</a> |

 

Stack Trace: Class.Test_trg_AccountDuplicatePreventer_Final.Test0_TestInsertWithValue: line 18, column 1

 

Test Class is:

 

@isTest(seeAllData = true)

public class Test_trg_AccountDuplicatePreventer_Final{
static testMethod void Test0_TestInsertWithValue()
{

//Set<Account> Accset = new Set<Account>();

Account acc1 = new Account(Is_record_near_to_duplicate__c='No', Name = 'Agency0', Phone='(981) 130-9977',Physical_Street__c = 'ABC0', Physical_State_Province__c = 'NY',Physical_City__c = 'NY',Physical_Zip_Postal_Code__c = '2010',Physical_Country__c= 'USA');
Account acc2 = new Account(Is_record_near_to_duplicate__c='No', Name = 'Agency00', Phone='(981) 130-9988',Physical_Street__c = 'ABC00', Physical_State_Province__c = 'NY',Physical_City__c = 'NY',Physical_Zip_Postal_Code__c = '2010',Physical_Country__c= 'USA');
Account acc3 = new Account(Is_record_near_to_duplicate__c='No', Name = 'Agency000', Phone='(981) 130-9999',Physical_Street__c = 'ABC000', Physical_State_Province__c = 'NY',Physical_City__c = 'NY',Physical_Zip_Postal_Code__c = '2010',Physical_Country__c= 'USA');

Account[] accs = new Account[]{acc1,acc2,acc3};
try{
insert accs;
}catch(DMLException ex)
{
System.assert(ex.getMessage().contains('Insert failed. First exception on'+ 'row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION,'+'<BR/>Agency record with this Name/Phone/Physical Street exists in the system. If you wish to create agency record, change field value of "Create New Record" to YES.<BR/><BR/> Potential Duplicate Agencies Include:<BR/>Agencies with Name, Phone & Physical Street match:<a href=https://cs14.salesforce.com/accs.id>accs.Name</a>'),
ex.getMessage());
}


acc2.Phone='(981) 130-7777';
acc3.Physical_Street__c='ABC0000';
acc3.Phone='(981) 130-8888';
update accs;

Account dupe1 = new Account(Name = 'Agency0', Phone='(981) 130-9977',Physical_Street__c = 'ABC0',
Physical_State_Province__c = 'NY',Physical_City__c = 'NY',Physical_Zip_Postal_Code__c = '2010',Physical_Country__c= 'USA');

try{
insert dupe1;
System.assert(false);
}catch(DMLException e){
for (Integer i = 0; i < e.getNumDml(); i++)
{
System.assert(e.getNumDml() == 1);
System.assert(e.getDmlIndex(i) == 0);
System.assert(e.getDmlFields(i).size() == 3);
System.assert(e.getDmlFields(i)[0] == Account.Name);
System.assert(e.getDmlFields(i)[1] == Account.Phone);
System.assert(e.getDmlFields(i)[2] == Account.Physical_Street__c);
System.assert(e.getDmlMessage(i).indexOf('An account with this name, phone, street already exists.') > -1);
}
}

Account dupe2 = new Account(Name = 'Agency00', Phone='(981) 130-9988',Physical_Street__c = 'ZZZ',
Physical_State_Province__c = 'NY',Physical_City__c = 'NY',Physical_Zip_Postal_Code__c = '2010',Physical_Country__c= 'USA');

try{
insert dupe2;
System.assert(false);
}catch(DMLException e){
for (Integer i = 0; i < e.getNumDml(); i++)
{
System.assert(e.getNumDml() == 1);
System.assert(e.getDmlIndex(i) == 0);
System.assert(e.getDmlFields(i).size() == 3);
System.assert(e.getDmlFields(i)[0] == Account.Name);
System.assert(e.getDmlFields(i)[1] == Account.Phone);
System.assert(e.getDmlFields(i)[2] == Account.Physical_Street__c);
System.assert(e.getDmlMessage(i).indexOf('An account with this name, phone already exists.') > -1);
}
}

dupe1 = new Account(Id=acc1.id, Name = 'Agency0', Phone='(981) 130-0000',Physical_Street__c = 'ABC0',
Physical_State_Province__c = 'NY',Physical_City__c = 'NY',Physical_Zip_Postal_Code__c = '2010',Physical_Country__c= 'USA');

update dupe1;
System.assert(false);
}
}

 

 

Can someone pls tell me why this test failure?

 

Afzal MohammadAfzal Mohammad

The culprit could be a validation rule on Account object, which is getting fired everytime you insert an Account record.

You may just disable that validation rule or change its conditions to avoid the error.

 

Hope that helps.

 

Afzal

 

Neha KakrooNeha Kakroo

There is only one validation rule on Accounts and I deactivated this rule as well. But still I keep getting the same error.

Don't know how to fix this.