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
t j 5t j 5 

stuck in test case for duplicate rule

Hi,
please provide test case for class which having duplicate rule.

global class CopyAcc implements Database.Batchable<sObject> {
    Set<Id> setDuplicateIds = new Set<id>();
    global Database.QueryLocator start(Database.BatchableContext bc)
    {
        String query= 'select id,name,website,billingcity,phone,billingstreet from account';
        return Database.getQueryLocator(query); 
    }
    global void execute(Database.BatchableContext bc, List<Account> scope)
    {
     system.debug('account '+scope);
        for(Account a : scope)
        {
          
   Database.SaveResult sr = Database.insert(account, false);

    if (!sr.isSuccess()) {
        Datacloud.DuplicateResult duplicateResult;
        // Insertion failed due to duplicate detected
        for(Database.Error duplicateError : sr.getErrors()){

            duplicateResult = ((Database.DuplicateError)duplicateError).getDuplicateResult();
        }

    // Fetch the Ids of the existing duplicate records
        for(Datacloud.MatchResult duplicateMatchResult : duplicateResult.getMatchResults()) {

            for(Datacloud.MatchRecord duplicateMatchRecord : duplicateMatchResult.getMatchRecords()) {
customobj_c ob = new customobj_c (name_c=a.name);
  insert ob;
            }
        }
  
       // System.debug('Duplicate records ' + setDuplicateIds);
    }
else
{
}
        }
          
    }
    
    global void finish(Database.BatchableContext bc)
    {
        
    }
}

Thanks
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for test classes for batch job
1) http://amitsalesforce.blogspot.com/search/label/Batch%20Job
@isTest
public class AccountUpdateBatchJobTest
{
    static testMethod void testMethod1()
    {
        List<Account> lstAccount= new List<Account>();
        for(Integer i=0 ;i <200;i++)
        {
            Account acc = new Account();
            acc.Name ='Name'+i;
            lstLead.add(acc);
        }
       
        insert lstAccount;
       
        Test.startTest();

            CopyAcc  obj = new CopyAcc();
            DataBase.executeBatch(obj);
           
        Test.stopTest();
    }
}
Let us know if this will help you