Script include only returning one value in reference field

alexcharleswort
Tera Expert

I have a use case where I need to filter a reference field based off of what is entered into another reference field. I have always been able to accomplish this through a script include, but this one seems to be tripping me up.

 

Basically the first field on the form u_name (ref to the sys_user table) will be filled in. Then I have another field u_computer_name (ref to the cmdb_ci table). I want to filter u_computer by assigned to that matches the value from u_name.

 

I got this to half work. When I put my name in there and then click the spyglass on u_computer_name it shows a record assigned to me... but only one record. There should be three in there. If I go straight to the cmdb_ci table and filter assigned to is me it shows me all three. So, I know the value is right, I just don't know why it is only returning one record per person. 

I have tested this out with several names that have more than one thing assigned to them and it only returns one record for them too. 

I have always gotten this to work in the past... what am I missing????

 

Here is my script includes script:

var FilterComputerNames = Class.create();
FilterComputerNames.prototype = {
    initialize: function() {
    },

	
	FilterComputerNames:function(){
		var gp = '';
		var a = current.u_name;
		
		if(!a)
			return;
		if(a){
	var gr = new GlideRecord('cmdb_ci');  
  gr.addQuery('assigned_to', current.u_name);  
  gr.query();  
  while(gr.next()) {  
  if (gp.length > 0) {  
  gp += (',' + gr.sys_id);  
  }  
  else {  
  gp = gr.sys_id;  
  }  
  }  
  return 'sys_idIN' + gp; 
	
	  
	  
		}  
	  
  },  
  type: 'FilterComputerNames'  
}; 
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Alex,

Can you update your script as belows:

var FilterComputerNames = Class.create();
FilterComputerNames.prototype = {
initialize: function() {
},


FilterComputerNames:function(){
var gp = [];
var a = current.u_name;

if(!a)
return;
if(a){
var gr = new GlideRecord('cmdb_ci');
gr.addQuery('assigned_to', current.u_name);
gr.query();
while(gr.next()) {
gp.push(gr.sys_id.toString());

}
return 'sys_idIN' + gp;
}
},
type: 'FilterComputerNames'
};

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Alex,

Can you update your script as belows:

var FilterComputerNames = Class.create();
FilterComputerNames.prototype = {
initialize: function() {
},


FilterComputerNames:function(){
var gp = [];
var a = current.u_name;

if(!a)
return;
if(a){
var gr = new GlideRecord('cmdb_ci');
gr.addQuery('assigned_to', current.u_name);
gr.query();
while(gr.next()) {
gp.push(gr.sys_id.toString());

}
return 'sys_idIN' + gp;
}
},
type: 'FilterComputerNames'
};

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

alexcharleswort
Tera Expert

Thank you Ankur!

 

Worked wonderfully!!

Jim Coyne
Kilo Patron

I know you already marked it as answered, but you can use a "simple" advanced reference qualifier instead:

javascript:"assigned_to=" + current.u_name;

..without using a Script Include.

and instead of reference the cmdb_ci table, you might want to reference cmdb_ci_computer instead if indeed the CI is supposed to be a Computer.