How to delete selected sys_history_line records but not all audit entries?

Natalie H
Tera Contributor

We have a couple of custom fields we're disabling audit for, and want to delete the historical audit entries for these fields only - not all audit records for the table. 

 

Querying sys_history_line to identify the records by field name/table doesn't work because I haven't accessed these records before (i.e. History line does not show results when trying to ... - ServiceNow Community).

 

Is there any way we can get around the above challenge with a script?

 

Thanks.

 

 

 

6 REPLIES 6

Taha Habib
Tera Guru

Hello Natalie, I hope you are doing well.

 

If you know the sys_id of the record, you can delete the specific field for that record in the sys_history_line table and it will not affect other field record.

 

var history = new GlideRecord("sys_history_line");
history.addQuery('set.id', "157454272f8c21102482802df699b651"); // sys_id of the record
history.query();
while (history.next()) {
  if(history.field == "caller_id") // field you wanna delete
   {
        history.deleteRecord();
   }

}

 

I hope this helps you, if this doesn't help with your requirement let me know. Thank you.

Hi Taha,

Thanks for your reply. Script makes sense but it doesn't return any results for me unless I have brought up the history of the record in the system beforehand. If I choose sys_id for record I've never viewed history of, no results (I changed the delete to print info from sys_history_line, as per below script). 

 

After I have viewed the history record, then the script prints results. Any thoughts? 

 

var history = new GlideRecord("sys_history_line");
history.addQuery('set.id', "0f3f04471bae511033e1dbd6b04bcbbd"); // sys_id of the record
history.query();
while (history.next()) {
gs.print(history.user_id);
gs.print(history.sys_id);
gs.print(history.new_value);

}

Hey Natalie,

I created a new record in Incident table and used the script. It's giving me results. I didn't see the record in history table after creating it.

Also, if you are working in a PDI and trying the sys_id of records that came in as dummy data and you haven't modified it before. It will not work as they are not present in history table by default, until you make some changes to it. I hope this makes sense.

 

Let me know, if I am missing something. Thank you.

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

Please read through this. It will help you understand how History Sets are created and maybe you understand what is missing from your script.

History sets - How are they generated - Support and Troubleshooting (servicenow.com)

-Anurag