Hiding Form Sections Based on User Group

allison_holt
Giga Expert

I am trying to hide a form section based on a users group, which is not listed on the form itself.

 

I tried the following client script and it's doesn't hide the tab.

 

function on Load(){

var me = g_user.userID();

if (me.isMemberOf('7efcb8956fa21100dd3253eabb3ee40c'){

var sections = g_form.getSections();

sections[3].style.display = 'block';

} else {

    sections[3].style.display = 'none';

    }

}

 

Any help as to what I can do to get this to work would be appreciated!

31 REPLIES 31

Rashmi Bansal
Mega Guru

Hi Allison,



you can use the methods provided on wiki to hide the sections like



if (g_user.hasRole("Some Role")) {


                              g_form.setSectionDisplay('communication', false);


                              g_form.setSectionDisplay('availability', false);


}



here communication and availabiltiy are the section names.


Please mark my answer as correct if you find it correct.


Harish Kota
Kilo Sage

Hi @allison_holt 

Please use the below script to achieve your requirement

Script Include:

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

getUserMembership: function() {

if (gs.getUser().isMemberOf('sys_id')) //sys_id of the group
return "hide";
else return "no-hide";

},

type: 'getGroupDetails'
});

 

Client Script:

function onLoad() {

var assetGA = new GlideAjax("getGroupDetails");
assetGA.addParam("sysparm_name", 'getUserMembership');
assetGA.getXML(assetSectionDisplayResponse);

function assetSectionDisplayResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'hide') {
g_form.setSectionDisplay('section_name', false); //section name
}
}
}