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 ) {
    List<Lead> myLeadsWithMatchingEmail = [SELECT Id,
                                           FROM Lead
                                           WHERE Email = :myLeadMatchEmail];
    For(Lead l : myLeadsWithMatchingEmail ) {
        if (myLeadMatchEmail.contains(l.Email)) {
        if (myLeadsToDelete.size() > 0) {
            system.debug('# Leads Deleted: ' + myLeadsToDelete.size());
            delete myLeadsToDelete;

Test Class for it:

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 = '[email protected]'));
        Account a = new Account(name = 'Change',
                                Email_address__c = '[email protected]');
        a.name = 'test2';

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?

1 Answer 1


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.

  • Hi, I have changed the email address, but now my test has only 60%. How can I improve it? Thanks
    – Inna
    Commented Sep 10, 2015 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
    Commented Sep 10, 2015 at 20:31

You must log in to answer this question.

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