Checking the history data

Vikram Vasudev
Tera Contributor

Is there a specific code or any automation process to track and retrieve the initial description and all the changes made to a short description of incident

1 ACCEPTED SOLUTION

Anand Kumar P
Giga Patron
Giga Patron

Hi Vikram,

There is an api in ServiceNow History Walker newly implemented in ServiceNow to check the initial changes of fields.

var current = new GlideRecord("incident");
if (current. get ("12ca184735123002728660c4cf6a7ef")) {
var hw = new sn_hw. HistoryWalker (current. getTableName (), current . getUniquevalue ()) ;
hw.walkTo(0);
var shortDesc = hw. getwalkedRecord() . short_description;
gs.info('Incident short description in update number ' + hw. getUpdateNumber () + ' was ' + shortDesc);
while (hw.walkForward())

 

Try above script in background script.

Please mark it as solution proposed and helpful if it serves your purpose.

Thanks,

Anand 

View solution in original post

3 REPLIES 3

Anand Kumar P
Giga Patron
Giga Patron

Hi Vikram,

There is an api in ServiceNow History Walker newly implemented in ServiceNow to check the initial changes of fields.

var current = new GlideRecord("incident");
if (current. get ("12ca184735123002728660c4cf6a7ef")) {
var hw = new sn_hw. HistoryWalker (current. getTableName (), current . getUniquevalue ()) ;
hw.walkTo(0);
var shortDesc = hw. getwalkedRecord() . short_description;
gs.info('Incident short description in update number ' + hw. getUpdateNumber () + ' was ' + shortDesc);
while (hw.walkForward())

 

Try above script in background script.

Please mark it as solution proposed and helpful if it serves your purpose.

Thanks,

Anand 

Anand Kumar P
Giga Patron
Giga Patron

You can also query the log table to get historical data for short description

var initialShortDescription = '';
var changes = [];

var gr = new GlideRecord('sys_audit');
gr.addQuery('tablename', 'incident');
gr.addQuery('documentkey', current.incident.sys_id);
gr.addQuery('fieldname', 'short_description');
gr.orderBy('sys_created_on');
gr.query();

while (gr.next()) {
if (initialShortDescription === '') {
initialShortDescription = gr.oldvalue;
}
changes.push({
timestamp: gr.sys_created_on.getDisplayValue(),
description: gr.newvalue
});
}

current.short_description= 'Initial: ' + initialShortDescription + '\n';
for (var i = 0; i < changes.length; i++) {
current.short_description_history += changes[i].timestamp + ': ' + changes[i].description + '\n';
}

Thanks,

Anand

AnveshKumar M
Tera Sage
Tera Sage

Hi @Vikram Vasudev 

 

You can also try creating a metric for short_deacription field in incident table. This way you can have all the values that were there in the field in the entire life cycle of Incident ticket.

 

Let me know, If you need more help in this approach.

 

Please mark my answer helpful and accept as solution if it helped you 👍

Thanks,
Anvesh