query sys_history_line with set value from sys_history_set

kkronborg
Giga Contributor

Hi,

Can somebody help with the following query where I use a set value from sys_history_set and try to find all history lines in sys_history_line 

var hiset = new GlideRecord('sys_history_set');
hiset.addQuery('id', test_sysid);
hiset.query();
while(hiset.next()){

gs.print('Set Id = ' + hiset.getValue('sys_id'));

var hiset = hiset.sys_id;
gs.print('Hiset sysid = ' + hiset);

var line = new GlideRecord('sys_history_line');
line.addQuery('set', hiset);
line.query();
while(line.next()){
  gs.print('History Line Id = ' + hiline.getValue('sys_id'));
}
}

It just doesn't return anything - what is wrong with this query ?

Regards

Kim

1 ACCEPTED SOLUTION

It' because you are trying to query 'id' on the sys_history_set table instead of sys_id.

 

Try this code and make sure you have a sys ID value for the test_sysid variable:

 

var hiset = new GlideRecord('sys_history_set');
hiset.addQuery('sys_id', test_sysid);
hiset.query();

while(hiset.next())
{
	var his = hiset.sys_id;
	//gs.print(his);

	 var li = new GlideRecord('sys_history_line');
	 li.addQuery('set', his);
	 li.query();

	while(li.next())
	{
	 gs.print('History Line Id = ' + li.sys_id);
	}
	
}

View solution in original post

4 REPLIES 4

ServiceNowSteve
Giga Guru

You're line var hiset = hiset.sys_id; is going to cause some conflicts...give that variable a new name

Hi Steve,

Thanks for answering.

I changed the variable name but it still doesn't return anything

 

Here's the code :

 

var hiset = new GlideRecord('sys_history_set');
hiset.addQuery('id', test_sysid);
hiset.query();
while(hiset.next()){

var his = hiset.sys_id;
gs.print('Hiset sysid = ' + his);

var li = new GlideRecord('sys_history_line');
li.addQuery('set', his);
li.query();
while(li.next()){
gs.print('History Line Id = ' + li.sys_id);
}
}

 

Does it take something special to query on the set field in sys_history_line ?

Where is this script running? is it a fix script, business rule, etc...

It' because you are trying to query 'id' on the sys_history_set table instead of sys_id.

 

Try this code and make sure you have a sys ID value for the test_sysid variable:

 

var hiset = new GlideRecord('sys_history_set');
hiset.addQuery('sys_id', test_sysid);
hiset.query();

while(hiset.next())
{
	var his = hiset.sys_id;
	//gs.print(his);

	 var li = new GlideRecord('sys_history_line');
	 li.addQuery('set', his);
	 li.query();

	while(li.next())
	{
	 gs.print('History Line Id = ' + li.sys_id);
	}
	
}