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
kavya.ax1320kavya.ax1320 

Test method for Object History

Anyone knows how to create and insert a record into the history object(custom object's history) inside test method?

@anilbathula@@anilbathula@

Hi kavya,

 

I think u want to write a test method for ur trigger.

which is on history object.

@isTestprivate class TriggerTestMethod { 

   //test method for history

    static testMethod void history() {   

     History__c h= new History__c (Name='test');      Add if there are any mandatory fields in the object and pass values.

  insert H;

}

}

 

Thanks

Anil.B

kavya.ax1320kavya.ax1320

Thanks for the reply Anil. But the above method didn't work for me. I am writing test method for apex class. Below is the code: 

 

public list<cHistories> getHistories()
    {
        // Initialize list to be returned
         list<cHistories> list_ch = new list<cHistories>();
    
        // Loop through all field history records
       
         for (Mapping_Object__History fh: [select ParentId,OldValue,NewValue,IsDeleted,Id,Field,CreatedDate,CreatedById,CreatedBy.Name
              From Mapping_Object__History
              where ParentId =:mapping.id
              order by CreatedDate desc])
              {
               // Create a new wrapper object
               cHistories ch = new cHistories();
      
               // Set the Date
               ch.theDate = String.valueOf(fh.createddate);
      
               // Set who performed the action
               ch.who = fh.createdby.name;
      
               // Set the Action value
               if (String.valueOf(fh.Field) == 'created')
               {    // on Creation
                    ch.action = 'Created.';
               }
               else if (fh.OldValue != null && fh.NewValue == null)
               { // when deleting a value from a field
                 // Format the Date and if there's an error, catch it and re
                 try {
                 ch.action = 'Deleted ' + Date.valueOf(fh.OldValue).format() + ' in <b>' + Schema.getGlobalDescribe().get('Mapping_Object__c').getDescribe().fields.getMap().get(String.valueOf(fh.Field)).getDescribe().getLabel() + '</b>.';
               
                 } catch (Exception e){
                   ch.action = 'Deleted ' + String.valueOf(fh.OldValue) + ' in <b>' + Schema.getGlobalDescribe().get('Mapping_Object__c').getDescribe().fields.getMap().get(String.valueOf(fh.Field)).getDescribe().getLabel() + '</b>.';
                   }
               }
               else
               {             // all other scenarios
                String fromText = '';
                if (fh.OldValue != null)
                {
                 try {
                      fromText = ' from ' + Date.valueOf(fh.OldValue).format();
                     } catch (Exception e) {
                      fromText = ' from ' + String.valueOf(fh.OldValue);
                      }
                }
       
                String toText = '';
                try {
                 toText = ' ' + Date.valueOf(fh.NewValue).format();
                } catch (Exception e) {
                 toText = ' ' + String.valueOf(fh.NewValue);
                   }
                               
          
          ch.action = 'Changed <b>' + Schema.getGlobalDescribe().get('Mapping_Object__c').getDescribe().fields.getMap().get(String.valueOf(fh.Field)).getDescribe().getLabel() + '</b>' + fromText + ' to <b>' + toText + '</b>.';    
               }
               list_ch.add(ch);   
             }
     return list_ch;
    }
   
    public class cHistories
    {
        // Class properties
        public String theDate {get; set;}
        public String who {get; set;}
        public String action {get; set;}
     }

 

Any idea on how to write test method for the above code?