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

Servicenow10
Kilo Guru

what i did is i created one group and have assigned one role to that grp....so once user logged in users with that role only should be able to edit that field

note: 1- onload client script is on change_request table

so how to get current logged in user also how to get the role of that user.

eg: 

function onLoad() {


if (g_user.hasRole('role_name') && g_user.hasRole('admin')) {

g_form.setDisplay('cab_required', true);
var field = g_form.getValue('cab_required');
if (field == true) {
g_form.setValue('u_type', 'u_normal', 'Normal');
}
}

}

thanks

 

Hi,

Use g_user.hasRoleExactly instead of hasRole, because hasRole will return true if the user has admin role. 

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

I suggest you create a Display BR and in that add your code.

if (gs.hasRole('role_name') && gs.hasRole('admin')) {
g_scratchpad.display=1;
else {
g_scratchpad.display=0;
}

Then write a onload client script, and in that check like this.

var display = g_scratchpad.display;
if(display==1) {
 g_form.setDisplay('cab_required', true); 
 var field = g_form.getValue('cab_required');
 if (field == true) {
  g_form.setValue('u_type', 'u_normal', 'Normal');
 }
} else {
 g_form.setMandatory('cab_required',false);
 g_form.setVisible('cab_required', false); 
}

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

hi asif

BR is working fine client script is not working properly....kindly check the below script:

function onLoad() {

//***********modified code************//

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

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);
  }
}