HistoryWalker : Update number 0 cannot be missing from the history error

Prakyath
Tera Contributor

Hi all,

 

Im using the History Walker to get the data from the audit table and i get the below error.

 

Prakyath_0-1709540692490.png

 

This is my code:

 

var hw = new sn_hw.HistoryWalker('<form_name>', '<sys_id>');

hw.walkTo(0);

do {
       var walkedGr = hw.getWalkedRecord();
       gs.print('Status '+walkedGr.getDisplayValue('u_status'));
} while (hw.walkForward());

 

Below provided is the data i see in my table:

 

Prakyath_3-1709541377800.png

 

I understand that my update count 2,3,4.

If i start the hw.walkto(5) from 5. it works as expected... 

is there any particular reason for this... 

and how to overcome it.

1 REPLY 1

Sohithanjan G
Kilo Sage
Kilo Sage

Hi @Prakyath ,

 

The issue you're encountering seems to be related to the starting point of the History Walker (hw.walkTo(0)). When you start the walker at position 0, it begins from the beginning of the history, which may not include the current state of the record.

The update count you're observing (2, 3, 4) indicates that there have been multiple updates to the record, and it seems that the current state of the record is not included in the history until after the third update.

To overcome this issue and ensure that you're accessing the most recent state of the record, you can start the History Walker from a higher update count, such as 5, as you mentioned, or even higher if necessary. This ensures that you start from a point in the history where the current state of the record is included.

Alternatively, you can try starting the walker from a more recent update count dynamically. Here's how you can modify your code to achieve this:

 

var hw = new sn_hw.HistoryWalker('<form_name>', '<sys_id>');

// Get the latest update count
var latestUpdateCount = new GlideRecord('<table_name>');
latestUpdateCount.get('<sys_id>'); // Assuming <table_name> is the table where your record is stored
var updateCount = latestUpdateCount.sys_update_count;

hw.walkTo(updateCount);

do {
    var walkedGr = hw.getWalkedRecord();
    gs.print('Status ' + walkedGr.getDisplayValue('u_status'));
} while (hw.walkForward());

 

 

This code retrieves the latest update count for the record and starts the History Walker from that update count, ensuring that you start from the most recent state of the record. Make sure to replace <table_name> with the actual table name where your record is stored.

By dynamically determining the starting point based on the latest update count, you can ensure that you're accessing the most recent state of the record in the history.

 

🙂

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)