Using the HistoryWalker API

Community Alums
Not applicable

In true Holiday Spirit, it's time to try and give back a little 🎄

Recently I've been asked by a customer to have an API function available that returns the previous assignment group.

Looking at the documentation and examples found in the community, I still found it confusing and was even about to open a support case to report a bug for this API.

 

However, after a few tries and more cups of coffee, here's what I've found...

 

To prepare for the script below, let's create a new incident (in your Personal Developer Instance!) and...

  1. Complete all the mandatory fields
  2. Set the value for Assignment group to 'Hardware'
  3. Submit the incident
  4. Set the value for Assignment Group to 'Software'
  5. Save the incident
  6. Set or change the value of some other field (I added something to the description field)
  7. Save the incident
  8. Set the value for Assignment Group to 'Database'
  9. Save the incident one last time
  10. Select the 'Additional Actions' menu (you know, that little hamburger icon in the top left corner 🍔)
  11. Select History > List
  12. In the Audit History list, find the first 'Assignment Group' value in the Label column
  13. Right click (or click with 2 fingers 🍏) and select 'Show Matching'
  14. Sort by 'Update number'
  15. You should see something like this:

Incident History List Screenshot.png

 

Now, let's see how we can reproduce this list by using the HistoryWalker API

  1. Copy the code below and paste it in a System Definition > Script - Background window
  2. Copy the number from the incident created above and paste it to replace the number in the script
  3. Run the background script 
var incidentNumber = 'INC0010187';
var field = 'assignment_group';
var grIncident = new GlideRecord('incident');
grIncident.get('number', incidentNumber);
var fieldLabel = grIncident.getElement(field).getLabel();
var history = new sn_hw.HistoryWalker(grIncident.getTableName(), grIncident.getUniqueValue());

while (history.walkBackward()) {
	var updateNumber = history.getUpdateNumber();
	var grHistoryIncident =  history.getWalkedRecord();
	if (grHistoryIncident[field].changes()) {
		// this is the update where the value changed
		var currentValue = grHistoryIncident.getDisplayValue(field);
		// walk back one more
		if (history.walkBackward()) {
			var grPreviousIncident =  history.getWalkedRecord();
			// to get the previous value
			var previousValue = grPreviousIncident.getDisplayValue(field);
			gs.info(`${fieldLabel} changed from ${previousValue} to ${currentValue} in update ${updateNumber}.`);
			// walk forward to re-align with  while loop
			history.walkForward();
		} else {
			gs.info(`${fieldLabel} changed to ${currentValue}, but was previously empty in update ${updateNumber}.`);
		}
	} else {
		gs.info(`${fieldLabel} did NOT change in update ${updateNumber}.`);
	}
}

 The result should look something like this:

*** Script: Assignment group changed from Software to Database in update 3.
*** Script: Assignment group did NOT change in update 2.
*** Script: Assignment group changed from Hardware to Software in update 1.
*** Script: Assignment group changed to Hardware, but was previously empty in update 0.

Note that we are walking backwards in the script, starting from the most recent update (3) all the way to the first one (0). If we're only interested in the previous value, we should probably stop (break the while loop) on line 21.

What confused me (and apparently many others) is that the change is detected (line 11 in the script), but the 'walked record' (lines 10 and 13 in the script) will still have the current value and not the previous one.

So you will have to walk (back) one more time to get to the previous value (lines 15 and 16 in the script) 💡

 

Hope this will give you a better understanding of how this API works!

1 REPLY 1

Pavel4
Tera Expert

Our need was to get snapshot of specific record to specfic datetime and for this purpose I found this HistoryWalker as not very usable, so we have had to build custom API over it.