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

Show/hide fields based on logged in user assignment group

Raksha6
Kilo Contributor

Hi Community, I'm working on a catalog item request with the below requirements. My client script isn't working in the Service Portal and I believe it's because I need to use GlideAjax instead of GlideRecord. I'm on a deadline, so any input would be greatly appreciated. Thank you.

  1. Show 'Assigned To' variable if logged in user is a member of 'Facilities' group
  2. Show 'Close Notes' variable if logged in user is a member of 'Facilities' group
  3. Hide Assigned To and Close Notes variables if logged in user isn't member of Facilities group

onLoad Client Script:

function onLoad() {

var userID = g_user.userID;

if (GetGroupMembers(userID) == true){
g_form.setDisplay('variables.assigned_to', true);
g_form.setDisplay('variables.close_notes', true);
}
else{
g_form.setDisplay('variables.assigned_to',false);
g_form.setDisplay('variables.close_notes', false);
}
}

function GetGroupMembers(user) {
var usrGrp=new GlideRecord("sys_user_grmember");
usrGrp.addQuery('group', '9682088ddb230010df87ab7dca9619cd');
usrGrp.addQuery('user', user);
usrGrp.query();
if(usrGrp.next()){
return true;
}
else {
return false;
}
}

1 ACCEPTED SOLUTION

Hi,

Okay so this is for catalog client script and on the catalog item view while submitting the request; in this case display business rule won't work;

you would require to create script include and use GlideAjax in onload client script

Script Include Below:

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

	getGroupMembership: function(){
		var user = gs.getUserID();
		var gr = new GlideRecord("sys_user_grmember");
		gr.addQuery('group.name', 'Facilities');
                gr.addQuery('user', user);
                gr.query();
                return gr.hasNext();
		
	},

	type: 'u_userInfoAjax'
});

Client Script:

function onLoad(){

var ga = new GlideAjax('u_userInfoAjax');
ga.addParam('sysparm_name', "getGroupMembership");
ga.getXMLAnswer(function(answer){

if(answer.toString() == 'true'){
g_form.setDisplay('assigned_to', true);
g_form.setDisplay('close_notes', true);
}

else{
g_form.setDisplay('assigned_to',false);
g_form.setDisplay('close_notes', false);
}

});

}

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

10 REPLIES 10

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

it is not best practice to use GlideRecord in client script; better approach is to use display business rule and onload client script

BR: Script:

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

Client Script:

function onLoad(){

if(g_scratchpad.isMemberOf.toString() == 'true'){

g_form.setDisplay('assigned_to', true);
g_form.setDisplay('close_notes', true);

}

else{

g_form.setDisplay('assigned_to',false);
g_form.setDisplay('close_notes', false);

}

}

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

Thanks Ankur. Could you please share the table name and if there's any conditions you're using for the Business Rule?

Hi,

Okay so this is for catalog client script and on the catalog item view while submitting the request; in this case display business rule won't work;

you would require to create script include and use GlideAjax in onload client script

Script Include Below:

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

	getGroupMembership: function(){
		var user = gs.getUserID();
		var gr = new GlideRecord("sys_user_grmember");
		gr.addQuery('group.name', 'Facilities');
                gr.addQuery('user', user);
                gr.query();
                return gr.hasNext();
		
	},

	type: 'u_userInfoAjax'
});

Client Script:

function onLoad(){

var ga = new GlideAjax('u_userInfoAjax');
ga.addParam('sysparm_name', "getGroupMembership");
ga.getXMLAnswer(function(answer){

if(answer.toString() == 'true'){
g_form.setDisplay('assigned_to', true);
g_form.setDisplay('close_notes', true);
}

else{
g_form.setDisplay('assigned_to',false);
g_form.setDisplay('close_notes', false);
}

});

}

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 Ankur, these scripts worked perfectly. Thank you so much for all your help!

I'm receiving the below error message in the Service Portal:

find_real_file.png