• Amit Hajare
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

I have created a custom history related list for a custom object. But I dont know how to write a test method for that. Here 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;}
          
    }

 

 

When I tried to insert a record into the History object in the test method, It is throwing error that insertion operation is not allowed for History objects. Does anyone know of a solution?