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
Nagarjuna Reddy.PNagarjuna Reddy.P 

Test Class Code Coverage for History Records

Hello,

I have a batch class which updates Status field based on HistoryRecord Created Date.I'm unable to get the complete code coverage as History Record Field New Value is not writeble. Could any one guide me to get code coverage.
Here is my Batch and Test classes

global class BOSSWaiverStatusUpdateBatch implements Database.Batchable<sObject>  {
  
   global Database.QueryLocator Start(Database.BatchableContext bc){
      String query = 'select id,Name,Status__c from Account where Status__c = \'More Information Required\'';
       return Database.getQueryLocator(query);
   }
   global void execute(Database.BatchableContext bc, List<Account> scope){
       List<Account> records = new List<Account>();
       Map<id,Account> updateWaivers = new Map<id,Account>();
       List<Id> ids = new List<Id>();
       Date currentDate = Date.today();
       for(Account rec:scope){
          // if(rec.BOSS_Waiver_Status__c == 'More Information Required'){
           // today.add(rec.createddate);
               ids.add(rec.id);
          // }
       }
       List<AccountHistory> historyRec = [select id,field,createddate,newvalue,oldvalue,parentid from AccountHistory where field = 'Status__c' and parentid in:ids order by createddate desc];
        Date moreInfoReqDate;
       for(AccountHistory hrec : historyRec){
           moreInfoReqDate = Date.newInstance(hrec.CreatedDate.year(),hrec.CreatedDate.month(),hrec.CreatedDate.day());
           System.debug('Into History loop');
           System.debug('Scope Records=='+scope.size());
            System.debug('History New Value=='+hrec.NewValue);
           if(hrec.NewValue == 'More Information Required' && (moreInfoReqDate.daysBetween(currentDate))>1){
               for(Account wrec : scope){
                   System.debug('Into Status update loop');
                   wrec.Status__c = 'Not Applicable';
                   records.add(wrec);

               }
               
           }
       }
       updateAccs.putAll(records);
       System.debug('Account Records to update=='+updateAccs.size());
       if(updateAccs.size()>0){
           update updateAccs.values();
       }

      // update records;
   }
   global void finish(Database.BatchableContext bc){

   }
}
The highlighted bold lines are not covered any code coverage. following is the test class

Test Class : 

@isTest
public class BOSSWaiverStatusUpdateBatchTest {
  
    @TestSetup
       public static void setup(){
        List<Account> AccRecords = new List<Account>();
        List<Account> updateAccs = new List<Account>();
        for(integer i=0;i<200;i++){
            Account accounts = new Account();
            accounts.Name = 'Test Account'+i;
            accounts.Statuc__c = 'Test';
            AccRecords.add(accounts);
        }
        try{
            insert AccRecords;
            for(Account w : AccRecords){
                w.BOSS_Waiver_Status__c = 'More Information Required';
                updateAccs.add(w);
            }
            update updateAccs;
            
        }
        catch(Exception e){
            System.debug('Exception Message=='+e.getMessage());
        }
    }
   @isTest static void updateWaiverBatchTest(){
         
       List<Account> rec = [select id,Name,Status__c from Account where Status__c = 'More Information Required'];
       AccountHistory history = new AccountHistory();
       history.Field = 'Status__c';
       history.ParentId = rec[0].id;
      // history.NewValue = 'More Information Required';
       insert history;
                Test.startTest();
                 BOSSWaiverStatusUpdateBatchSchedule sbatch = new BOSSWaiverStatusUpdateBatchSchedule();
                    String sch = '0 0 5 * * ?'; 
                    system.schedule('Test Schedule Batch', sch, sbatch);
                Test.stopTest();
      
        List<Account> records = [select id,Status__c from Account where BOSS_Waiver_Status__c = 'Not Applicable'];
       if(records.size()>0){
        system.assertEquals('Not Applicable',records[0].BOSS_Waiver_Status__c);
       } 
    }

Thank you guys!
AnkaiahAnkaiah (Salesforce Developers) 
Hi Nagarjuna,

Refer the below link have solution for history records code coverage.

https://developer.salesforce.com/forums/?id=906F000000091uSIAQ

Thanks!!