3

Does anybody know how to write testmethods for Duplication Rules?

I have created the Account Duplicate Rule with init conditions:

Current User: Profile not equal to System Administrator

with Alert and Report on Create.

Action on Create is Allow.

When the duplication appears, I have a choice (push Save button and save duplicated record). How to reflect this story in test method?

static void testMethod1() {
    User u = createUser('Profile Name');

    System.runAs(u) {
        Account acc1 = createAccount();
        insert acc1;

        Account acc2 = createAccount();
        Boolean isExceptionWasThrown = false;
        try {
            insert acc2;
        }
        catch(DmlException e) {
            System.assertEquals(StatusCode.DUPLICATES_DETECTED, e.getDmlType(0));
            System.assert(e.getMessage().contains('Message'));
            isExceptionWasThrown = true;
        }

        System.assert(isExceptionWasThrown);
    }   
}

Flag isExceptionWasThrown was correctly set as true. Process is blocked in Alert Text (in Account Duplication Rule without creating related objects - Duplicate Record Set and Items) and I do not have a possibility to approve this duplication.

1 Answer 1

2

I have done it as below and it works:

Database.DMLOptions insertDML = new Database.DMLOptions(); 
insertDML.DuplicateRuleHeader.AllowSave = true; 
Database.SaveResult sr = Database.insert(acc2, insertDML);
System.assert(sr.isSuccess());

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.