How to retrieve sys_history_line records for a specific record?

NAVAYUVANS
Tera Contributor

Hi Everyone,

I have a question regarding retrieving records from the sys_history_line table.

In my scenario, a record was created and updated a long time ago. Over time, the related entries in sys_history_line were deleted. However, when I open the record again or navigate to History → List, the system regenerates the history lines, and I’m able to fetch that data using a background script.

My requirement is to retrieve those sys_history_line records without opening the record or navigating to the History UI, i.e., without triggering regeneration manually.

Is there a way to access or regenerate these history records programmatically?

Any guidance would be appreciated. Thanks in advance!

4 REPLIES 4

Tanushree Maiti
Kilo Patron

Hi @NAVAYUVANS 

 

The Audit [sys_audit] and History Sets capture the same data, but data is managed differently. The major difference between them is persistence:

  • The Audit table [sys_audit] records persist forever.
  • The History Set [sys_history_set] records are generated on use and are removed by the table cleaner 30 days after their most recent use.
  • The History Set Line [sys_history_line] records are on four tables that are managed using Table Rotation, which is customizable. From the base system, the tables are rotated on a seven-day basis, meaning that the records are dropped 28 days after generation unless they are requested again.

Once you Open the record -> right-click on the header or the hamburger menu icon->select History from the menu->List : History set and history line records will be generated from the sys_audit table.

 

Refer following KBs:

History sets - How are they generated 

KB0547662:Auditing and history sets | How they work together 

https://www.servicenow.com/community/developer-forum/how-to-fix-deleted-record/m-p/3474488

 

 

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin:

Thanks @Tanushree Maiti for your response.

Could you please clarify how the [sys_history_set ] and [sys_history_line] records are generated in the backend? I’d like to understand the mechanism so I can implement a similar approach and retrieve the data to display on the Service Portal side.

navayuvan29
Tera Contributor

Thanks @Tanushree Maiti for your response.

Could you please clarify how the sys_history_set and sys_history_line records are generated in the backend? I’d like to understand the mechanism so I can implement a similar approach and retrieve the data to display on the Service Portal side.

In ServiceNow, records in the sys_history_set and sys_history_line tables are not stored permanently; they are generated dynamically when a user accesses a record’s history. The system constructs these entries on demand by processing raw audit data from the sys_audit table, presenting it as a clear, chronological, and user-friendly view of changes.

 

ref: https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0744473#:~:text=To%20avoid%20....

 

https://www.servicenow.com/community/developer-forum/regenerating-sys-history-line-and-sys-history-s...

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin: