Script - Query sys_history_line | Not matching change_request.sys_id and set.id

hanaphouse
Giga Guru
var gr = new GlideRecord("change_request")
gr.query();
while(gr.next()) {
    var line = new GlideRecord("sys_history_line");
    line.addQuery('set.id',gr.sys_id);
    line.setLimit(1);
    line.query();
    if(line.next()){
    gs.print('CHG: ' + gr.number.toString()  + ', Set ID: ' + line.set.id.toString());
    }
else{gs.print('CHG: ' + gr.number.toString()  + ', Set ID: ' + line.set.id.toString());
}
}

What could be wrong in this script? What I am trying to do is validate if all CHG number has a corresponding sys_history_line entry.

When I try to run this script, it doesn't match the set IDs.

1 ACCEPTED SOLUTION

AshishKM
Kilo Patron
Kilo Patron

Hi, 

Open any change and run the History->Line command and generate the record in sys_history_set table until the set table will not have any history record. 

Refer the below links 


Scripts: GlideRecord sys_history_set / sys_history_line / sys_audit

what is the exact difference or use of [sys_history_set] and [sys_history_line] tables with a simple...

 

Thanks,

Ashish


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

View solution in original post

5 REPLIES 5

Separating the history_set and history_line steps into 2 queries will help with troubleshooting and cover all scenarios.  The below script will more clearly show you every change record, and whether or not they have a history line (or a history set without a history line - possible, but not likely).

var chg = new GlideRecord('change_request');
chg.orderBy('number');
chg.query();
while(chg.next()){
	var hset = new GlideRecord('sys_history_set');
	hset.addQuery('table', 'change_request');
	hset.addQuery('id', chg.sys_id.toString());
	hset.query();
	if(hset.next()){
		var line = new GlideRecord('sys_history_line');
		line.addQuery('set', hset.sys_id.toString());
		line.setLimit(1);
		line.query();
		if(line.next()){
			gs.print('History line found CHG: ' + chg.number.toString()  + ', Set ID: ' + hset.id.toString());
		}
		else{
			gs.print('History set found, but no line CHG: ' + chg.number.toString());
		}
	}
	else{
		gs.print('No history set found CHG: ' + chg.number.toString());
	}
}