<h2>HistoryWalker API is unable to recognise/detect field changes when a field is emptied out!!</h2><br/><div style="overflow-x:auto"><article><div ><h3 >Issue </h3><section><p style="padding-left: 40px;">1.Create an incident(You may fill all the Mandatory fields) [sys_mod_count = 0]<br />2.Fill Assigned To & save [sys_mod_count = 1]<br />3.Run the below script as a background script:</p> <p style="padding-left: 80px;"><span style="color: #000080;">var hw = new sn_hw.HistoryWalker("incident", <sys_id of the incident>);</span><br /><span style="color: #000080;">hw.walkTo(1);</span><br /><span style="color: #000080;">var walkedGR = hw.walkForward();</span><br /><span style="color: #000080;">var walkedGr = hw.getWalkedRecord();</span><br /><span style="color: #000080;">var fields = GlideScriptRecordUtil.get(walkedGr).getChangedFieldNames();</span><br /><span style="color: #000080;">gs.print("Fields changed at update " + hw.getUpdateNumber() + " were:");</span><br /><span style="color: #000080;">for (var j = 0; j < fields.size(); j++)</span><br /><span style="color: #000080;">gs.print(" " + fields.get(j));</span><br /></p> <p style="padding-left: 80px;"><span style="color: #333399;"><em>Actual:</em></span><br /><span style="color: #333399;"><em>*** Script: Fields changed at update 1 were:</em></span><br /><span style="color: #333399;"><em>*** Script: state</em></span><br /><span style="color: #333399;"><em>*** Script: incident_state</em></span><br /><span style="color: #333399;"><em>*** Script: assigned_to</em></span><br /></p> <p style="padding-left: 80px;"><span style="color: #333399;"><em>Expected:</em></span><br /><span style="color: #333399;"><em>*** Script: Fields changed at update 1 were:</em></span><br /><span style="color: #333399;"><em>*** Script: state</em></span><br /><span style="color: #333399;"><em>*** Script: incident_state</em></span><br /><span style="color: #333399;"><em>*** Script: assigned_to</em></span></p> <p style="padding-left: 40px;">4.Clear Assigned To & save [sys_mod_count = 2]<br />5.Run the below script as a background script:<br /></p> <p style="padding-left: 80px;"><span style="color: #000080;">var hw = new sn_hw.HistoryWalker("incident", <sys_id of the incident>);</span><br /><span style="color: #000080;">hw.walkTo(2);</span><br /><span style="color: #000080;">var walkedGR = hw.walkForward();</span><br /><span style="color: #000080;">var walkedGr = hw.getWalkedRecord();</span><br /><span style="color: #000080;">var fields = GlideScriptRecordUtil.get(walkedGr).getChangedFieldNames();</span><br /><span style="color: #000080;">gs.print("Fields changed at update " + hw.getUpdateNumber() + " were:");</span><br /><span style="color: #000080;">for (var j = 0; j < fields.size(); j++)</span><br /><span style="color: #000080;">gs.print(" " + fields.get(j));</span><br /></p> <p style="padding-left: 80px;"><span style="color: #333399;"><em>Actual:</em></span><br /><span style="color: #333399;"><em>*** Script: Fields changed at update 2 were:</em></span><br /></p> <p style="padding-left: 80px;"><span style="color: #333399;"><em>Expected:</em></span><br /><span style="color: #333399;"><em>*** Script: Fields changed at update 2 were:</em></span><br /><span style="color: #333399;"><em>*** Script: assigned_to</em></span><br /></p> <p style="padding-left: 40px;">6.Fill Assigned To & save (Like you did in step 3) [sys_mod_count = 3]<br />7.Fill/Modify description, clear Assigned To & Save [sys_mod_count = 4]<br />8.Run the below script as a background script:<br /></p> <p style="padding-left: 80px;"><span style="color: #000080;">var hw = new sn_hw.HistoryWalker("incident", <sys_id of the incident>);</span><br /><span style="color: #000080;">hw.walkTo(4);</span><br /><span style="color: #000080;">var walkedGR = hw.walkForward();</span><br /><span style="color: #000080;">var walkedGr = hw.getWalkedRecord();</span><br /><span style="color: #000080;">var fields = GlideScriptRecordUtil.get(walkedGr).getChangedFieldNames();</span><br /><span style="color: #000080;">gs.print("Fields changed at update " + hw.getUpdateNumber() + " were:");</span><br /><span style="color: #000080;">for (var j = 0; j < fields.size(); j++)</span><br /><span style="color: #000080;">gs.print(" " + fields.get(j));</span><br /></p> <p style="padding-left: 80px;"><span style="color: #333399;"><em>Actual:</em></span><br /><span style="color: #333399;"><em>*** Script: Fields changed at update 4 were:</em></span><br /><span style="color: #333399;"><em>*** Script: description</em></span><br /></p> <p style="padding-left: 80px;"><span style="color: #333399;"><em>Expected:</em></span><br /><span style="color: #333399;"><em>*** Script: Fields changed at update 4 were:</em></span><br /><span style="color: #333399;"><em>*** Script: description</em></span><br /><span style="color: #333399;"><em>*** Script: assigned_to</em></span></p></section></div><div ><h3 >Cause</h3><section><ul style="list-style-position: inside;"><li>This is the expected behavior.</li><li>If the element does not have a value it is not returned.</li></ul></section></div><div ><h3 >Resolution</h3><section><ul style="list-style-position: inside;"><li>You can try the following as a general work around:</li></ul> <p style="padding-left: 80px;"><span style="color: #000080;">var incGr = new GlideRecord('incident');</span><br /><span style="color: #000080;">incGr.get('number', 'INC0025769');</span><br /><span style="color: #000080;">var hw = new sn_hw.HistoryWalker(incGr.getTableName(), incGr.getUniqueValue());</span><br /><span style="color: #000080;">hw.walkTo(0);</span><br /><span style="color: #000080;">do {</span><br /><span style="color: #000080;">printChangedFields(hw);</span><br /><span style="color: #000080;">} while (hw.walkForward());</span><br /><span style="color: #000080;">function printChangedFields(hw) {</span><br /><span style="color: #000080;">var updatenumber = hw.getUpdateNumber();</span><br /><span style="color: #000080;">if (updatenumber == 0) {</span><br /><span style="color: #000080;">var walkedGr = hw.getWalkedRecord();</span><br /><span style="color: #000080;">var fields = GlideScriptRecordUtil.get(walkedGr).getChangedFieldNames();</span><br /><span style="color: #000080;">gs.print("Fields changed at update " + hw.getUpdateNumber() + " were:");</span><br /><span style="color: #000080;">for (var j = 0; j < fields.size(); j++)</span><br /><span style="color: #000080;">gs.print(" " + fields.get(j));</span><br /><span style="color: #000080;">gs.print("");</span><br /><span style="color: #000080;">}</span><br /><span style="color: #000080;">else {</span><br /><span style="color: #000080;">var walkedGr = hw.getWalkedRecord();</span><br /><span style="color: #000080;">fields = new GlideRecordUtil().getFields(walkedGr);</span></p> <p style="padding-left: 80px;"><span style="color: #000080;">var prevupdatenumber = updatenumber - 1;</span><br /><span style="color: #000080;">var hw2 = new sn_hw.HistoryWalker(incGr.getTableName(), incGr.getUniqueValue());</span><br /><span style="color: #000080;">hw2.walkTo(prevupdatenumber);</span></p> <p style="padding-left: 80px;"><span style="color: #000080;">prevwalkedGr = hw2.getWalkedRecord();</span><br /><span style="color: #000080;">fields2 = new GlideRecordUtil().getFields(prevwalkedGr);</span></p> <p style="padding-left: 80px;"><span style="color: #000080;">gs.print("Fields changed at update " + hw.getUpdateNumber() + " were:");</span><br /><span style="color: #000080;">for (var j = 0; j < fields2.length; j++) {</span><br /><span style="color: #000080;">if (walkedGr.getValue(fields[j]) != prevwalkedGr.getValue(fields2[j]) && !fields[j].startsWith("sys_") )</span><br /><span style="color: #000080;">gs.print("changed " +fields[j]);</span><br /><span style="color: #000080;">}</span><br /><span style="color: #000080;">gs.print("");</span><br /><span style="color: #000080;">}</span><br /><span style="color: #000080;">}</span></p></section></div></article></div>