How to show a choice list option only to two groups and for rest groups remove that option. It is for a catalog item, based on variable requested_for, need to decide and remove the option if user is group of those two groups.

priya130
Kilo Contributor

Hi All,

 

How to show a choice list option only to two groups and for rest groups remove that option. It is for a catalog item, based on variable requested_for, need to decide and remove the option if user is group of those two groups.

I tried this client script and script include, not getting result. Can anyone help me on this

onChange catalog client script

onchange field = Requested_for

var ga = new GlideAjax('GetUserGroup');
ga.addParam('sysparm_name', 'isGrpMember');
ga.addParam('sysparm_username', newValue);
ga.addParam('sysparm_groupName','Service Desk');
ga.getXML(hideField);

function hideField(response) {


var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'false') {
g_form.removeOption('team_selection', 'desktop_ assistance');

}
}
--------------------

And script include

var GetUserGroup = Class.create();
GetUserGroup.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isGrpMember:function()
{
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user',this.getparameter('sysparm_username'));
gr.addQuery('group',this.getparameter('sysparm_groupName'));
gr.query();
if(gr.next())
{
return true;
}
},

type: 'GetUserGroup'
});

 

I did tried with display br and client script

code below:

Table= sc_cat_item

g_scratchpad.grp = gs.getUser().isMemberOf('grp1');

g_scratchpad.grp2 = gs.getUser().isMemberOf('grp2');

 

Onload Client script:-

Table= sc_cat_item

function onLoad() {

if (!g_scratchpad.grp && !g_scratchpad.grp2)
{
g_form.removeOption('field_name', 'option');
}
}

 

Still no result. Please someone help me here, give some suggestions. Please let me know if my code is wrong.

Thanks in Advance!

1 ACCEPTED SOLUTION

Jan Cernocky
Tera Guru

Hi priya,

I found some issues in your script.

a. you define "sysparm_groupName" as a name of group, whereas the gliderecord in script include expects sys ID, either provide sys ID in glideAjax or change the query definition in script include

b. your script include only returns true, but you check for false in client script - I recommend to define else and return false

Interesting fact: seems that even if the script include should return boolean in my testing I discovered it actually returns string so your condition == "false" is correct although I initially thought it is not

If you get into troubles I always recommend to add some logging, so you know if your script reaches certain point and what the values are, in script include I use gs.log in client script I use g_form.addInfoMessage

I tried your scenario with fixes mentioned above and it works as expected, have a look at my code (slightly different as I had different variables)

Client script:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
	
	var ga = new GlideAjax('GetUserGroup');
	ga.addParam('sysparm_name', 'isGrpMember');
	ga.addParam('sysparm_username', newValue);
	ga.addParam('sysparm_groupName','287ebd7da1fe198100f92cc8d1d2154e');
	ga.getXML(hideField);

	function hideField(response) {
		var answer = response.responseXML.documentElement.getAttribute("answer");
		if (answer == "false") {
		g_form.removeOption('team_selection', '2');
		}
	}

}

Script include:

var GetUserGroup = Class.create();
GetUserGroup.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	isGrpMember:function() {
		
		var gr = new GlideRecord('sys_user_grmember');
		var usrName = this.getParameter('sysparm_username');
		var grpName = this.getParameter('sysparm_groupName');
		gr.addQuery('user',usrName);
		gr.addQuery('group',grpName);
		gr.query();
		if(gr.next()) {
			gs.log('JC: match');
			return true;
		}
		else {
			gs.log('JC: no match');
			return false;
		}
	},
    type: 'GetUserGroup'
});

View solution in original post

2 REPLIES 2

Jan Cernocky
Tera Guru

Hi priya,

I found some issues in your script.

a. you define "sysparm_groupName" as a name of group, whereas the gliderecord in script include expects sys ID, either provide sys ID in glideAjax or change the query definition in script include

b. your script include only returns true, but you check for false in client script - I recommend to define else and return false

Interesting fact: seems that even if the script include should return boolean in my testing I discovered it actually returns string so your condition == "false" is correct although I initially thought it is not

If you get into troubles I always recommend to add some logging, so you know if your script reaches certain point and what the values are, in script include I use gs.log in client script I use g_form.addInfoMessage

I tried your scenario with fixes mentioned above and it works as expected, have a look at my code (slightly different as I had different variables)

Client script:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
	
	var ga = new GlideAjax('GetUserGroup');
	ga.addParam('sysparm_name', 'isGrpMember');
	ga.addParam('sysparm_username', newValue);
	ga.addParam('sysparm_groupName','287ebd7da1fe198100f92cc8d1d2154e');
	ga.getXML(hideField);

	function hideField(response) {
		var answer = response.responseXML.documentElement.getAttribute("answer");
		if (answer == "false") {
		g_form.removeOption('team_selection', '2');
		}
	}

}

Script include:

var GetUserGroup = Class.create();
GetUserGroup.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	isGrpMember:function() {
		
		var gr = new GlideRecord('sys_user_grmember');
		var usrName = this.getParameter('sysparm_username');
		var grpName = this.getParameter('sysparm_groupName');
		gr.addQuery('user',usrName);
		gr.addQuery('group',grpName);
		gr.query();
		if(gr.next()) {
			gs.log('JC: match');
			return true;
		}
		else {
			gs.log('JC: no match');
			return false;
		}
	},
    type: 'GetUserGroup'
});

It is working now, thanks a lot!