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
Vigneshwaran LoganathanVigneshwaran Loganathan 

Getting History Records in Test class

Hi,

I have written a test class for getting History Records for the Purticular field. but it shows 'Null'. I have tried from some links but all will give the same result. Am i wrong somewhere?
 
public class batchDateplacedbackoncallTest
{
    public static Testmethod void mytest()
    {      
        VA_Matching__c vaMatchRec = new  VA_Matching__c(Client__c = accobj.id,status__c='Available',
                                                         VA__c = vaRec.id,
                                                         Date_added_to_On_Call_List__c=Date.parse('1/1/2015')
                                                         );
    insert vaMatchRec;
    vaMatchRec.status__c='Placed- Part time';
    update vaMatchRec;
    vaMatchRec.status__c='Available';
    update vaMatchRec; 

    List<VA_Matching__History> histList=new List<VA_Matching__History>();
     VA_Matching__History hist=new VA_Matching__History(Field='Status__c');
     histList.add(hist);
        
      List<String> results=example.processRows(histList);
     System.assertEquals(1, results.size());
     System.assertEquals('Name|null|null', results[0]);  
 }
}

 public List<VA_Matching__History> queryDatabase(id vaId)
    {
        return [select Id, OldValue, NewValue, Field, CreatedBy.Name 
                from VA_Matching__History
                where ParentId=:vaId order by createddate desc limit 1];
    }
   
     public List<String> ProcessRows(List<VA_Matching__History> historyList)
    {
        List<String> results=new List<String>();
        for (VA_Matching__History hist : historyList)
        {
            String field=hist.field;
            Object oldValue=hist.oldValue;
            Object newValue=hist.newValue;

            results.add(field + '|' + oldValue + '|' + newValue);
        }

        return results;
    }

Thank you so much.
 
Vishal_GuptaVishal_Gupta
Hi,

Please verify that history is enabled on Status_c field.

Thanks,
Vishal 
Vigneshwaran LoganathanVigneshwaran Loganathan
Hi Vishal,

Yes it is Enabled only..
N.V.V.L.Vinay KumarN.V.V.L.Vinay Kumar
Hi Vigneshwaran,

try this

public class batchDateplacedbackoncallTest
{
    public static Testmethod void mytest()
    {      
        VA_Matching__c vaMatchRec = new  VA_Matching__c(Client__c = accobj.id,status__c='Available',
                                                         VA__c = vaRec.id,
                                                         Date_added_to_On_Call_List__c=Date.parse('1/1/2015')
                                                         );
    insert vaMatchRec;
    vaMatchRec.status__c='Placed- Part time';
    update vaMatchRec;
    vaMatchRec.status__c='Available';
    update vaMatchRec; 

    List<VA_Matching__History> histList=new List<VA_Matching__History>();
     
     histList = this.queryDatabase(vaMatchRec.Id);
        
     List<String> results=this.processRows(histList);
     System.assertEquals(1, results.size());
     System.assertEquals('Name|null|null', results[0]);  
 }
}

 public List<VA_Matching__History> queryDatabase(id vaId)
    {
        return [select Id, OldValue, NewValue, Field, CreatedBy.Name 
                from VA_Matching__History
                where ParentId=:vaId order by createddate desc limit 1];
    }
   
     public List<String> ProcessRows(List<VA_Matching__History> historyList)
    {
        List<String> results=new List<String>();
        for (VA_Matching__History hist : historyList)
        {
            String field=hist.field;
            Object oldValue=hist.oldValue;
            Object newValue=hist.newValue;

            results.add(field + '|' + oldValue + '|' + newValue);
        }

        return results;
    }

Reagrds,
N. Vinay.