Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Hide Multiple choice option for all but specific group

dagarson
Tera Guru

Hello I have a catalog item where I am trying to hide a multiple choice value from all users who access the form unless they are part of a specific group. However the script I am trying to run isnt working.

function onLoad() {
 
    var targetGroup = '09745cc9c3302200e7c7d44d81d3ae6f';

    // Check if the current user is a member of the target group
    if (!g_user.isMemberOf(targetGroup)) {
        // Remove "op_3" option if the user is not a member of the group
        g_form.removeOption('choice', 'op_3');
    }
}

Does anyone see an issue with this approach? It seems like it should be a pretty simple script. 

 

Thank you

1 ACCEPTED SOLUTION

Bhavani Shankar
Tera Guru

Hello @dagarson ,

 

I doubt if isMemberOf is even a method of g_form object. isMemberOf is a method available in GlideUser object which works on the server side. 

 

For your case I think you should try and create a client callable script include and do your validation there.

 

Please do leave a thumbs up if my response helped you

 

 

Regards,
Bhavani Shankar
Linked In

View solution in original post

5 REPLIES 5

Zach Koch
Giga Sage

Use the name of the group as your argument, not the sys_id.

If this information helped resolve your issue, please remember to mark response correct and thumbs up to help future community members on this information, thanks!

Thank you for responding. I gave that a try and it didnt seem to make a difference.

Bhavya11
Kilo Patron
Kilo Patron

Hi @dagarson ,

 

Step 1: Create a display Business rule with script as. Place the below script between function templates in script body.

 

g_scratchpad.grp = gs.getUser().isMemberOf('PASS GROUP NAME HERE');  



Now update the client script as

 

function onLoad() {

 

 

 

  if (!g_scratchpad.grp){

 

      g_form.removeOption('choice', 'op_3');

    }

 

}

 

 

Mark helpful if it really help you.

 

 

Thanks 

Bk

Thanks for responding. Does the Business Rule need to run on a specific table? I tried it with none and with the ritm table and it didnt work on either.