Set form section only for specific group readonly

JohnDF
Mega Sage

Hi everyone,

 

my requirement is that only one specific group has readonly access on a form section and the fields on this section. How this possible to hide the section or the fields for all other users except the users in the specific group. Its about 20 fields on the form.

 

Thanks for your help.

1 ACCEPTED SOLUTION

jaheerhattiwale
Mega Sage

@JohnDF Tried and tested solution. Make use of GlideAjax to find out if the logged in user is member of that specific group or not.

 

1. Create a onload client script

Code:

function onLoad() {
var ga = new GlideAjax('UserAjaxUtil'); //UserAjaxUtil is a client callable script include, you can create new or use existing one
ga.addParam('sysparm_name', 'isUserMemberOfGroup');
ga.getXML(hideSection);
}

function hideSection(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");

//If user is not member of group then hide section
if(answer == "false"){
g_form.setSectionDisplay('notes', false);
}
}

 

2. Create or use existing client callable script include

Code:

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

isUserMemberOfGroup: function(){
return gs.getUser().isMemberOf("<GROUP NAME HERE>");
},

type: 'UserAjaxUtil'
});

 

Please mark as correct answer if this solves your issue

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

View solution in original post

7 REPLIES 7

@Community Alums thanks for reply,

 

here is my client script and br so far. But its not working.

 

the client script should check if the current user is in the group who are allowed to see the section:

function onLoad(){

   //Type appropriate comment here, and begin script below



	if((g_scratchpad.grp.toString() == 'true'))
		{	
var section = $$('span[tab_caption_raw="ILV"]')[0].select('span[id*=section.]')[0];
section.show();
//Show the tab
$$('.tab_caption_text').each(function(caption) {
    if(caption.innerHTML == 'ILV'){
        caption.up('.tab_header').show();
    }
});
		}
}

 

An here is the display query business rule which check the current user if he is in the group:

 

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	g_scratchpad.grp = gs.getUser().isMemberOf('4645767947af5110c72593be436d4324');
g_scratchpad.usrid = gs.getUserID(); // gets current logged in user

})(current, previous);

 

But its not working. Still all users can see the setion and the fields.

 

 

Not applicable

Hi,

 

Are you getting any error? Is it going inside if (in client script)? Put an alert and see?

 

And also did you check the isolate script field checkbox on client script?

jaheerhattiwale
Mega Sage

@JohnDF Tried and tested solution. Make use of GlideAjax to find out if the logged in user is member of that specific group or not.

 

1. Create a onload client script

Code:

function onLoad() {
var ga = new GlideAjax('UserAjaxUtil'); //UserAjaxUtil is a client callable script include, you can create new or use existing one
ga.addParam('sysparm_name', 'isUserMemberOfGroup');
ga.getXML(hideSection);
}

function hideSection(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");

//If user is not member of group then hide section
if(answer == "false"){
g_form.setSectionDisplay('notes', false);
}
}

 

2. Create or use existing client callable script include

Code:

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

isUserMemberOfGroup: function(){
return gs.getUser().isMemberOf("<GROUP NAME HERE>");
},

type: 'UserAjaxUtil'
});

 

Please mark as correct answer if this solves your issue

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023