2

I've got trigger:

trigger deleteRecordstest on Account (after insert, after update) {
    List<Lead> myLeadsToDelete = new List<Lead>();
    Set<String> myLeadMatchEmail = new Set<String>();
    for ( Account a : trigger.new ) {
        myLeadMatchEmail.add(a.Email_address__c);
    }
    List<Lead> myLeadsWithMatchingEmail = [SELECT Id,
                                                  Name,
                                                  Email 
                                           FROM Lead
                                           WHERE Email = :myLeadMatchEmail];
    For(Lead l : myLeadsWithMatchingEmail ) {
        if (myLeadMatchEmail.contains(l.Email)) {
            myLeadsToDelete.add(l);
        }
        if (myLeadsToDelete.size() > 0) {
            system.debug('# Leads Deleted: ' + myLeadsToDelete.size());
            delete myLeadsToDelete;
        }
    }
}

Test Class for it:

@isTest
private class testDeleteRecord_SFDC {
    static testMethod void testDeleteRecord() {
        List<Lead> l = new List<Lead>();
        l.add(new Lead(LastName = 'testlast',
                       FirstName = 'testfirst',
                       Company = 'Test',
                       Email = 'example@test.com'));
        insert(l);
        Account a = new Account(name = 'Change',
                                Email_address__c = 'example@test.com');
        insert(a);
        a.name = 'test2';
        update(a);
    }
}

However, in deployment class fails saying

System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATES_DETECTED, You're creating a duplicate record.: [] Stack Trace: Class.testDeleteRecord_SFDC.testDeleteRecord: line 9, column 1

I have changed account name, but still didn't work.

Any ideas where the mistake is?

3

In your test class, by specifying that the Account has the same email address as the lead you inserted earlier, you're attempting to create a duplicate record. It would appear that the new duplicate record rules have been enabled in your org and that's causing the issue you're having. There isn't enough additional information in your new Account record to distinguish it as being different from the lead you inserted at the beginning of your test class (address, business type (on the biz), phone numbers, etc.

2
  • Hi, I have changed the email address, but now my test has only 60%. How can I improve it? Thanks – Inna Sep 10 '15 at 12:24
  • The logic would seem to be that you'll need to change the email address of your Account, not it's name as your unit test code is currently showing since that's the criteria that you'r using to create a match. By updating the Account's email to match your lead, you'll presumably then have a lead to delete. Since you didn't tell me which lines aren't covered, that's my best guess on what to do. – crmprogdev Sep 10 '15 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.