Setting a Reference Qualifier using Script Includue

emmahunt
Kilo Contributor

Hi all,

Having an issue I hope people can help me out with!

I want to set a list collector reference qualifier based on a field on the request form.

I want to return all Configuration Items that have a Management Group that the user in the requested_for variable is a part of. To do this, I am attempting to set my reference qualifier query via a Client Script in a variable within a Script Include. I will use that same Script Include to get the saved value in the Reference Qualifier field of the variable.

My issue: It does not seem like variables are saving in the Script Include. It seems like it reinitializes every time the Script Include is called and deletes the value in the variable. I am wondering if I am doing this correctly, or if there is possibly a better way to do this? 

Here is my Client Script which

function onLoad() {
	
	var groupString = "";
	var newValue = 'requested_for';
	
	var gr = new GlideRecord ('sys_user');
	gr.addQuery ('sys_id', newValue);
	gr.query();
	while (gr.next()){
		var group = new GlideRecord ('sys_user_grmember');
		group.addQuery ('user', gr.sys_id);
		group.query();
		while (group.next()){
			groupString += group.sys_id+",";
		}
		groupString = groupString.substring(0,groupString.length-1);
	}
	var myLimit = "location=123c49b037d0200044e0bfc8bcbe5dd3^ORlocation=deeb202edbb31600f0727aa9bf9619f3^ORlocation=daeb202edbb31600f0727aa9bf9619f5^install_status!=7^virtual=true^u_management_group.sys_idIN"+groupString;
	
	var limit = new GlideAjax('ServerRebootLimitCIs');
	limit.addParam('sysparm_name','setLimit');
	limit.addParam('sysparm_limit',myLimit);
	limit.getXML(doSomething);

	function doSomething(response) {
		var answer = response.responseXML.documentElement.getAttribute("answer");
	}	
}

My Script Include:

var ServerRebootLimitCIs = Class.create();
ServerRebootLimitCIs.prototype = Object.extendsObject(AbstractAjaxProcessor, {
limit: "",
getLimit: function(){
	return this.limit + "";	
},
setLimit: function(){
		this.limit = this.getParameter('sysparm_limit');		
},
    type: 'ServerRebootLimitCIs'
});

I would love some guidance with this or if anyone has a better way of implementing this type of functionality 🙂 

 

Thanks!

Emma

1 ACCEPTED SOLUTION

Deepak Ingale1
Mega Sage

My bad,

 

I thought field is of reference type where you want to set the reference qualifier, but it turned out to be the list collector type.

Please see below link to configure the filter condition for list collector type of variable

 

https://community.servicenow.com/community?id=community_question&sys_id=08720feddb98dbc01dcaf3231f96196a

 

If you still want list collcetor to work with reference qualifier, you need to add "glide_list" attribute to variable

 

find_real_file.png

 

Once done, your variable will be looking list a watch list kind of field on incident form

 

find_real_file.png

Now,  select the user in requested for field and CI field should show you filtered data

 

 

find_real_file.png

 

 

find_real_file.png

View solution in original post

10 REPLIES 10

Thank you for the response, that is very helpful! However I am finding it is not changing when I change the requested_for user. Can you update the reference qualifier on the change of a field?

It should do that automatically, We do not require to write onchange or onload Please provide your current script include and make sure it is client callable

Sure, I will provide my Script Include below. Also I no longer am using a client script to call this Script Include as I don't think I need it if it should automatically run, right? 

var ServerRebootLimitCIs = Class.create();
ServerRebootLimitCIs.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getLimit: function(current){
	
	var groupString = "";
	
	var gr = new GlideRecord ('sys_user'); 
	gr.addQuery ('sys_id', current);
	gr.query();
	while (gr.next()){ // getting the requested_for user passed in through the ref qual
		var group = new GlideRecord ('sys_user_grmember');
		group.addQuery ('user', gr.sys_id);
		group.query();
		while (group.next()){ // cycling through all of the user's groups
			groupString += group.group +",";
		}
	}
	groupString = groupString.substring(0,groupString.length-1); // removing the last comma
	var myLimit = "location=123c49b037d0200044e0bfc8bcbe5dd3^ORlocation=deeb202edbb31600f0727aa9bf9619f3^ORlocation=daeb202edbb31600f0727aa9bf9619f5^install_status!=7^virtual=true^u_management_group.sys_idIN"+groupString; 
// myLimit returns a query to filter location data. the only part that changes is the u_management_group filter. However this does not seem to change when I change users in the requested_for variable
	return myLimit;
},
    type: 'ServerRebootLimitCIs'
});

Additionally, here is what I have listed in my Reference Qualifier for my list collector variable:

javascript:new ServerRebootLimitCIs().getLimit(current.variables.requested_for);

 

Hello,

 

Please find the update code, it should work for you

In reference qualifier condition of the variable we need to put

 

javascript: new ServerRebootLimitCIs().getLimit(current.variables.requested_for)

I assume requested for is a reference type pf variable from sys_user table

var ServerRebootLimitCIs = Class.create();
ServerRebootLimitCIs.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getLimit: function(requested_for){
	
	var groupString = '';
	var groups = [];
	var myLimit = '';

	var group = new GlideRecord ('sys_user_grmember');
	group.addQuery ('user', requested_for);
	group.query();

	while (group.next()){ // cycling through all of the user's groups
		groups.push(group.getValue("group"));
	}

	groupString = groups..join(",");
	myLimit = "location=123c49b037d0200044e0bfc8bcbe5dd3^ORlocation=deeb202edbb31600f0727aa9bf9619f3^ORlocation=daeb202edbb31600f0727aa9bf9619f5^install_status!=7^virtual=true^u_management_group.sys_idIN"+groupString; 

	return myLimit;
},
    type: 'ServerRebootLimitCIs'
});

Deepak Ingale1
Mega Sage

My bad,

 

I thought field is of reference type where you want to set the reference qualifier, but it turned out to be the list collector type.

Please see below link to configure the filter condition for list collector type of variable

 

https://community.servicenow.com/community?id=community_question&sys_id=08720feddb98dbc01dcaf3231f96196a

 

If you still want list collcetor to work with reference qualifier, you need to add "glide_list" attribute to variable

 

find_real_file.png

 

Once done, your variable will be looking list a watch list kind of field on incident form

 

find_real_file.png

Now,  select the user in requested for field and CI field should show you filtered data

 

 

find_real_file.png

 

 

find_real_file.png