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

Slava Savitsky
Giga Sage

Let alone the questions of performance and good practice, client-side GlideRecord is executed in the security context of the current user. With your initial approach, the results of your GlideRecord query depend on whether or not the current user has access to read from Group Members [sys_user_grmember] table.