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

Yogi3
Kilo Guru

Try below

 

function onLoad() {

var userID = g_user.userID;

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

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

Raksha6
Kilo Contributor

Thanks for the response, but this script didn't work for me.

Ankush Jangle1
Kilo Guru

Hi,

 

 

You can use Display Business rule to check that user is member of Facilities group

 

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

 

And in OnLoad Client Script

if(g_scratchpad.user==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);

}

 

Please mark it is Helpful/Correct if it Help you.

Thanks and Regards,

Ankush.

 

And One more thing in catalog client script UI Type should be ALL so that it will work on Service Portal

 

Thanks and Regards,

Ankush.