function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RarLopzRarLopz 

bypass duplicate rule

I have two active duplicate rules on Account in production . 
Now when i am trying to deploy my code for merging duplicate accounts, the deployment fails for  a class that already exists in production. If i disable the duplicate rule, the  test class that I am trying to deploy will fail because it check for active duplicate rules. 

I did some research and found a workaround as follows. 
My question is, do i have to add this workaround in my test class or the main class.  Thank you !

Database.DMLOptions dml = new Database.DMLOptions(); dml.DuplicateRuleHeader.AllowSave = true; Account duplicateAccount = new Account(Name='dupe'); Database.SaveResult sr = Database.insert(duplicateAccount, dml); if (sr.isSuccess()) { System.debug('Duplicate account has been inserted in Salesforce!'); }
Best Answer chosen by RarLopz
Raj VakatiRaj Vakati
Yes .. You are correct ..If you want you can add same code to test class also 

Test method example
 
public static testMethod void testFirstSituation(){


        DuplicateRule dR = [select id from DuplicateRule where DeveloperName = 'Dup_Accounts_2' LIMIT 1];
        DuplicateRecordSet dupRS = new DuplicateRecordSet(DuplicateRuleId = dR.id);
        insert dupRS;

        Test.startTest();
        Account acc = new Account(name = 'TestAccount', phone='9898787878');
        Database.DMLOptions insertDML = new Database.DMLOptions(); 
        insertDML.DuplicateRuleHeader.AllowSave = true; 
        Database.SaveResult sr = Database.insert(acc, insertDML);
        DuplicateRecordItem dup = new DuplicateRecordItem(DuplicateRecordSetId = dupRS.id, RecordId=acc.id);
        insert dup;
        Test.stopTest();

    }




https://salesforce.stackexchange.com/questions/169601/test-class-for-trigger-on-duplicaterecorditem-object

All Answers

Raj VakatiRaj Vakati
You need to do it main class .. but test class when you are checking the duplicate result you can use try and catch 
 
Raj VakatiRaj Vakati
Database.DMLOptions dml = new Database.DMLOptions();
dml.DuplicateRuleHeader.AllowSave = true; 
Account duplicateAccount = new Account(Name='dupe'); 
Database.SaveResult sr = Database.insert(duplicateAccount, dml); 
if (sr.isSuccess()) {   
 System.debug('Duplicate account has been inserted in Salesforce!'); 
}

 
RarLopzRarLopz
Thanks @RajVakati

Do you mean I should put that code snippet in the man class? 
And in test class, I should put the rey catch block when asserting the result? 

Can you please give an example? Thanks!
 
Raj VakatiRaj Vakati
Yes .. You are correct ..If you want you can add same code to test class also 

Test method example
 
public static testMethod void testFirstSituation(){


        DuplicateRule dR = [select id from DuplicateRule where DeveloperName = 'Dup_Accounts_2' LIMIT 1];
        DuplicateRecordSet dupRS = new DuplicateRecordSet(DuplicateRuleId = dR.id);
        insert dupRS;

        Test.startTest();
        Account acc = new Account(name = 'TestAccount', phone='9898787878');
        Database.DMLOptions insertDML = new Database.DMLOptions(); 
        insertDML.DuplicateRuleHeader.AllowSave = true; 
        Database.SaveResult sr = Database.insert(acc, insertDML);
        DuplicateRecordItem dup = new DuplicateRecordItem(DuplicateRecordSetId = dupRS.id, RecordId=acc.id);
        insert dup;
        Test.stopTest();

    }




https://salesforce.stackexchange.com/questions/169601/test-class-for-trigger-on-duplicaterecorditem-object
This was selected as the best answer