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.

How to hide fields through client script based on group?

Servicenow10
Kilo Guru

hi

i have to write one client script on load of chg req that :

if (logged in user is ammber of group ='abc') {

g_form.setDisplay('cab_required', true); //cab required field is a checkbox field
 var field = g_form.getValue('cab_required');
 if (field == true) {
 g_form.setValue('u_type', 'u_normal', 'Normal');

 }

 }

 

how to put this in syntax correctly

thanks

1 ACCEPTED SOLUTION

Hi,

Check the output and let me know what is it showing.

function onLoad() {
  var display = g_scratchpad.display;
  alert(display);
  if (display == 1) {
    alert("if1");
    g_form.setDisplay('cab_required', true);
    var field = g_form.getValue('cab_required');
    alert(field);
    if (field == true) { //is the syntax is proper
       
      g_form.setValue('u_type', 'u_normal', 'Normal');
    }
  } else {
    alert("else");
    g_form.setMandatory('cab_required', false);
    g_form.setVisible('cab_required', false);
  }
}

View solution in original post

10 REPLIES 10

Community Alums
Not applicable

Hi Dell,

You can write the following code to check group membership

if(gs.getUser().isMemberOf)

https://developer.servicenow.com/app.do#!/api_doc?v=london&id=GUser-isMemberOf_S

 

Let me know if you have any questions.

Please mark this comments as Correct Answer/Helpful if it helped you.

Cheers,

Hardit Singh

Shantharao
Kilo Sage

Hi

what is the type of "u_type" field, I mean is it choice or string type any other

you can create one display business rule and get the group members and pass the g_scratchpad value into your onload client script it will return boolean values like true or false, 

 

Display BR

g_scratchpad.group = gs.getUser().isMemberOf(current.assignment_group) 

 

Client script

var grp = g_scratchpad.group

if (grp ='true') {

g_form.setDisplay('cab_required', true); //cab required field is a checkbox field
 var field = g_form.getValue('cab_required');
 if (field == true) {
 g_form.setValue('u_type', 'u_normal', 'Normal');// setValue method we will pass only two parameters one is filed backend value and one is setting value

 }

 }

please hit like if it helps you and close the thread accordingly

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

group membership cannot be checked in client script; use display business rule on that table and use this

BR Script:

g_scratchpad.isMember = gs.getUser().isMemberOf('ABC');

Client Script:

function onLoad(){

if(g_scratchpad.isMember){

// show or hide fields you want

}

}

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

hi ankur,

 

can you explain your BR step by step and client script like on which table br is going to apply