The CreatorCon Call for Content is officially open! Get started here.

Get the count of records in a reference variable

Kumar60
Kilo Expert

Hello All,

I am using a refernce qualifier on Group variable which works fine.

Now I want to count the "number of groups" that group variable is showing.If my group(s) count is one then I need to set the one group in the group field and it should be read only.if the group count is greater than 1 then user will select the groups.

How to achieve this?Below is my reference qualifier code:

Reference qualifier:javascript: new getgroupcontext().getgrpcontext(current.variables.role2);

var getgroupcontext = Class.create();
getgroupcontext.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getgrpcontext : function(role)
{

//var role = this.getParameter('sysparm_query');
var groups = [];
var gr = new GlideRecord('sys_group_has_role');
gr.addQuery('role.name',role);
gr.query();

while(gr.next())
{
//gs.log("@362363"+gr.group+"NOWROLE"+gr.role.name);
groups.push(gr.group);


}
return 'sys_idIN' + groups.toString();
},
type: 'getgroupcontext'
});

 

1 ACCEPTED SOLUTION

Hi Kumar,

you cannot set value to group variable if script include returns 1 value; reference qualifier will help in restricting the records and not setting the value

you can use this approach:

1) use onChange client script and use GlideAjax and check if only 1 group exist with that role; if yes then return that and set the value; if more than 1 return sys_ids

Same function you can use for both the purpose; one from ref qualifier and one from GlideAjax

2) keep the reference qualifier as it is -> this will help users to see restricted groups if more than 1

Note: I assume the role2 variable is a string variable

Client Script: onchange client script on role variable

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading) {
		return;
	}

if(newValue == ''){
g_form.clearValue('group_variable');
return;
}

	var ga = new GlideAjax('u_userInfoAjax');
	ga.addParam('sysparm_name', "getGroups");
	ga.addParam('sysparm_role', newValue);
	ga.getXMLAnswer(function(answer){
	
        if(answer != ''){
        g_form.setValue('group_variable', answer);	
        }

	});

}

Script Include:

var getgroupcontext = Class.create();
getgroupcontext.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getgrpcontext : function(role)
{

var role = JSUtil.nil(role) ? this.getParameter('sysparm_role') : role;

var groups = [];
var gr = new GlideRecord('sys_group_has_role');
gr.addQuery('role.name',role);
gr.query();

if(gr.getRowCount() == 1)
return gr.getValue('group');

else{
while(gr.next())
{
groups.push(gr.getValue('group'));
}
return 'sys_idIN' + groups.toString();
}
},


type: 'getgroupcontext'
});

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

13 REPLIES 13

Hi Kumar,

you cannot set value to group variable if script include returns 1 value; reference qualifier will help in restricting the records and not setting the value

you can use this approach:

1) use onChange client script and use GlideAjax and check if only 1 group exist with that role; if yes then return that and set the value; if more than 1 return sys_ids

Same function you can use for both the purpose; one from ref qualifier and one from GlideAjax

2) keep the reference qualifier as it is -> this will help users to see restricted groups if more than 1

Note: I assume the role2 variable is a string variable

Client Script: onchange client script on role variable

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading) {
		return;
	}

if(newValue == ''){
g_form.clearValue('group_variable');
return;
}

	var ga = new GlideAjax('u_userInfoAjax');
	ga.addParam('sysparm_name', "getGroups");
	ga.addParam('sysparm_role', newValue);
	ga.getXMLAnswer(function(answer){
	
        if(answer != ''){
        g_form.setValue('group_variable', answer);	
        }

	});

}

Script Include:

var getgroupcontext = Class.create();
getgroupcontext.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getgrpcontext : function(role)
{

var role = JSUtil.nil(role) ? this.getParameter('sysparm_role') : role;

var groups = [];
var gr = new GlideRecord('sys_group_has_role');
gr.addQuery('role.name',role);
gr.query();

if(gr.getRowCount() == 1)
return gr.getValue('group');

else{
while(gr.next())
{
groups.push(gr.getValue('group'));
}
return 'sys_idIN' + groups.toString();
}
},


type: 'getgroupcontext'
});

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

Hello Ankur,

If the count is 1,still the value is not getting set.I checked with logs,the answer value we are getting in client script is only 'sys_idIN'.
If the count is more than 1,scenario is working fine.

I tried in background script,I am getting the single groups sys_id,if the count is one.

Hi Kumar,

can you share your script include?

when onchange is working what you are getting in answer variable?

Did you print that?

Regards
Ankur

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

My script include is below.When printing answer in client script only 'sys_idIN' is printing.

var getgroupcontexttest2 = Class.create();
getgroupcontexttest2.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getgrpcontext : function(role1)
{

var role = JSUtil.nil(role1) ? this.getParameter('sysparm_role') : role1;

var groups = [];
var gr = new GlideRecord('sys_group_has_role');
gr.addQuery('role.name',role1);
gr.query();

if(gr.getRowCount() == 1){
return gr.getValue('group');
//return gr.group.sys_id;

//return gr.group.getDisplayValue();
}

else{
while(gr.next())
{
groups.push(gr.getValue('group'));
}
return 'sys_idIN' + groups.toString();
}
},

type: 'getgroupcontexttest2'
});

Client script as follows:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if(newValue == ''){
g_form.clearValue('group2');
return;
}

var ga = new GlideAjax('getgroupcontexttest2');
ga.addParam('sysparm_name', "getgrpcontext");
ga.addParam('sysparm_role', newValue);
//ga.getXMLAnswer(function(answer){This is also trowing the same output.just tried splitting the getXMLAnswer as below.
ga.getXML(GetDetails);
function GetDetails(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert("@362363 answer from Script include is " + answer);
if(answer != ''){
//if(answer){This returns the same result
g_form.setValue('group2', answer);

//g_form.setReadOnly('group2',true);
}
}
//});
//Type appropriate comment here, and begin script below

}

Hi Kumar,

please update script as below; you are storing role in this variable so use that

gr.addQuery('role.name',role);

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