Hide Question Choice based on logged in user belonging to group

Dazler
Mega Sage

Hi,

I am trying to figure out how to hide a Question Choice on a Select Box type variable on a catalog item form based on whether the logged in user belongs to a specific group.

I created a Business Rule and a Catalog Client Script but it is not working.

find_real_file.png

 

find_real_file.png

 

I am unsure what I am doing wrong.  The goal for this is that when a user views this form, if the logged in user does not belong to this specific group then certain options in the dropdown will not be visible to them.

find_real_file.png

 

Any assistance would be greatly appreciated.

1 ACCEPTED SOLUTION

You cannot do it by using display business rule for catalog items. You need to do it by GlideAjax

Here is are scripts for script include and client scripts

Script Include

find_real_file.png

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

	
	isMemeber: function(){
			
		return gs.getUser().isMemberOf('Managers');
	},
	
    type: 'AjaxGroupMember'
});

 

Client script

function onLoad() {
	//Type appropriate comment here, and begin script below
	var cat = new GlideAjax('AjaxGroupMember');
	cat.addParam('sysparm_name','isMemeber');
	
	cat.getXML(isMemofGrp);
	
	
	function isMemofGrp(response){
		var answer = response.responseXML.documentElement.getAttribute("answer");
		
		if(answer == 'true'){
			g_form.removeOption('st_requestchoice', 'Wholesale');
			
		}
	}
}

View solution in original post

6 REPLIES 6

Alikutty A
Tera Sage

Hello,

Display business rules does not work on catalog item forms. This would be easier if you can tag a role to your group. You need to write an OnLoad client script with following script.

if(g_user.hasRole('your_role')){

g_form.removeOption('choice_var', 'choice_value');

}else{

g_form.addOption('choice_var', 'choice_value', 'choice_lablel');

}

Or else this would require an Ajax call on server side to validate group membership.

Thanks

 

Hi, thank you for replying back.

We cannot add a role to this group due to licenses. How would I go about adding the ajax call to validate the group membership?

 

You need a server side function within a script include which checks for the group membership of current user. You can use the same function from your display BR. This will called from your onLoad client script using GlideAjax asynchronously and you can use the same script I added above. You should add an validation on the value returned instead of the roles check.

I am still quite new to ServiceNow javascript coding. It helps me when I see examples.

Below is my code for my client script.  Would this works?

function onLoad() {
   //Type appropriate comment here, and begin script below

   if(g_scratchpad.grp('Managers'))
	   {
		   g_form.removeOption('st_requestchoice', 'Wholesale');
	   }
	else
		{
			g_form.addOption('st_requestchoice', '');
		}

}