0
votes

I am writing a test class for my trigger that checks for account duplicates. But I get the following error in my test class :

Error: Compile Error: Comparison arguments must be compatible types: Schema.SObjectField, String at line 35 column 42

The test class is:

@isTest

public class trg_AccountDuplicatePreventer_FinalTest{
    static testMethod void Test0_TestInsertWithValue()
    {

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

        Account acc1 =  new Account(Name = 'Agency0', Phone='9811309977',Physical_Street__c = 'ABC0', Physical_State_Province__c = 'NY',Physical_Zip_Postal_Code__c = '2010',Physical_Country__c= 'USA');
        Account acc2 =  new Account(Name = 'Agency00', Phone='9811309988',Physical_Street__c = 'ABC00', Physical_State_Province__c = 'NY',Physical_Zip_Postal_Code__c = '2010',Physical_Country__c= 'USA');
        Account acc3 =  new Account(Name = 'Agency000', Phone='9811309999',Physical_Street__c = 'ABC000', Physical_State_Province__c = 'NY',Physical_Zip_Postal_Code__c = '2010',Physical_Country__c= 'USA');

        Account[] accs = new Account[]{acc1,acc2,acc3};
        insert accs;

        acc2.Phone='9811309999';
        acc3.Physical_Street__c='ABC0000';
        acc3.Phone='9811308888';
        update accs;

        Account dupe1 =  new Account(Name = 'Agency0', Phone='9811309977',Physical_Street__c = 'ABC0', Physical_State_Province__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] == 'Name');
                 System.assert(e.getDmlFields(i)[1] == 'Phone');
                 System.assert(e.getDmlFields(i)[2] == 'Physical_Street__c');
                 System.assert(e.getDmlMessage(i).indexOf('An account with this name, phone, street already exists.') > -1);
            }
        }
    }
}

How do I fix this error in my test code?

Thanks!

1
You might want to specify more details like what you're trying to achieve here. Also, can you highlight the line where it fails.Anup
Hi Anup, my trigger code 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:user1518186
Message:System.DmlException: 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=cs14.salesforce.com/001c0000003ncYmAAI>Agency000</a> | <BR/>Agencies with Name & Phone match: <a href=cs14.salesforce.com/001c0000003ncYmAAI>Agency000</a>user1518186
Stack Trace: Class.trg_AccountDuplicatePreventer_FinalTest.Test0_TestInsertWithValue: line 14, column 1user1518186

1 Answers

2
votes

getDmlFields returns a list of Schema.sObjectField objects so you would need to either compare them to other Schema.sObjectFields or get their name to compare them to a string.

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);