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

I tried doing that by updating gr.addQuery('role.name',role);

Now,If the count is two i.e more than one,the alert answer in client script is giving 2 sys_id's.

But if the count is one then the alert for answer is throwing null.

Tried by updating the while loop as below,in this case it's setting the group value if the count is one.

But if the count is 2,then only one sys_id its giving in the alert.

So as per my understanding,what ever the return is there after while(gr.next()) that is only giving the accurate result!

 

 

Hi,

got your point;

1) in case when there are more than 1 it is returning the group sys_id and setting

2) but when more than 1 it is entering while and forming the array and returning something like this 'sys_idINsys_id1,sys_id2'

so what you can check is whether the answer variable contains a string with 32 characters it means it is single sys_id

when there are more than 1 records it would be a string with length more than 32

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

	});

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

Hi Kumar,

Let me know if that answered your question.

If so, please mark my response as correct & 👍 helpful so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.

Regards
Ankur

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

Hello Ankur,

A small change in the code returning the expected results.

below is the modified line code:

*****Code change in Script Include*****

gr.query();

if(gr.next()){//wrap in if,will fix the issue

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

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

}//If condition close

******Code change in Script Include******