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

Audit Trial History in the Test Instance

hi friends,

How are you all doing. I hope everybody is rocking in there jobs. I have an issue and i need some help from you guys. I have Batch Class which basically queries the Audit history of an Object and do some operation on the other Object. I willl provide you the code of my Batch class Below.


global class ProcessCorpTerrMapAudit  implements Database.Batchable<sObject>{

private Datetime startTime;
private Datetime lastRunTime;
private Batch_Scheduler_Configuration__c config;
private String lastRunTimeString;
public String recId;

global ProcessCorpTerrMapAudit(){
    startTime =;
    config = [select id, Last_Run_Date_Time__c from Batch_Scheduler_Configuration__c where Job_Type__c='ProcessCorpTerrMapAudit'];
    lastRunTime = config.Last_Run_Date_Time__c;
    lastRunTimeString = ''+lastRunTime;
    lastRunTimeString = lastRunTimeString.replace(' ', 'T')+'.000Z';
    system.debug('now ' + lastRunTime);
    system.debug('nowstring ' + lastRunTimeString);

global Database.QueryLocator start(Database.BatchableContext BC){
    String query='Select c.ParentId, c.OldValue, c.NewValue, c.Id, c.Field, c.Division, c.CreatedDate, c.CreatedById From Corp_Terr_Map__History c where c.CreatedDate>'+lastRunTimeString;
        query +=' and c.Id = \''+recId+'\'';
    return Database.getQueryLocator(query);

global void execute(Database.BatchableContext BC, List<sObject> scope){
        List<Corp_Terr_Map_Audit__c> audits = new List<Corp_Terr_Map_Audit__c>();
        for(sobject s : scope){
            Corp_Terr_Map__History hist = (Corp_Terr_Map__History)s;
            Corp_Terr_Map_Audit__c audit = new Corp_Terr_Map_Audit__c();
            audit.ParentId__c = hist.ParentId;
            } else {
                audit.OldValue__c = ''+hist.Oldvalue;
            } else {
                audit.NewValue__c = ''+hist.Newvalue;
            audit.Field__c = hist.Field;
            audit.Unique_Id__c = hist.Id;
            audit.CreatedById__c = hist.CreatedById;
            audit.CreatedDateTime__c = hist.CreatedDate;
            upsert audits Unique_Id__c;

global void finish(Database.BatchableContext BC){

    AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed,
                        TotalJobItems, CreatedBy.Email
                        from AsyncApexJob where Id =:BC.getJobId()];

        config.Last_Run_Date_Time__c = startTime;
        update config;     
    // Send an email to the Apex job's submitter notifying of job completion.
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    String[] toAddresses = label.System_Admin_Email_Notification.split(';');
    mail.setSubject('Process Corp Terr Map process completed with status  - '+ a.Status);
    mail.setPlainTextBody('Process Corp Terr Map : Total Job Items ' + a.TotalJobItems + ' batches with '+ a.NumberOfErrors + ' failures.');
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });  





For this class i have written the Test class where i found an issue. I am posting the code of the test class below:



private class ProcessCorpTerrMapAuditTest {

    static testMethod void myUnitTest() {
        Corp_Terr_Map__c cmp = new Corp_Terr_Map__c(name = 'testterr1',Geo__c = 'AMS'  ,Super_Org__c ='CA_SO', Organization__c = 'CANADA_FIELD', Region__c = 'CANADA', Area__c = 'CA_ENTERPRISE_1', District_Team__c = 'CA_DISTRICT_1', Sector_Vertical__c = 'HEALTHCARE', Segment__c = 'COMMERCIAL', Territory_Type__c = 'HEALTHCARE_ISV_TERRITORY', Active_Flag__c = 'Active', Fiscal_Year__c = 'Old', Open_Territory__c = false , Effective_Start_Date__c =, Effective_End_Date__c = null, Business_Reason__c = 'Test Purpose', Quota_Bearing__c = true , OIC_Territory_Flag__c = false );
        insert cmp;
        Corp_Terr_Map__c cup = [select id,name,quota_bearing__c from Corp_Terr_Map__c where];
        cup.Quota_Bearing__c = false;
        update cup;
        ProcessCorpTerrMapAudit pctm = new ProcessCorpTerrMapAudit();
        Id batchProcessid = Database.executeBatch(pctm);



the issue is when i am doing the insert or update on the object it should update the Audit History of that object. But here in the test class its not doing it.

I want to ask you guys whether the operations done in the test Instance updates the audit History of that object? If not how can i make this test class to be covered the execution part of the batch class.