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.

onLoad client script - hide some fields

manoj9335
Kilo Contributor

function onLoad() {

  //Type appropriate comment here, and begin script below

  if(g_user.hasRole('u_pm_tracking_user'))

  {

  g_form.setDisplay('u_rate',false);

  g_form.setDisplay('u_project_sow',false);

  g_form.setSectionDisplay('january (next year)', false);

  g_form.setSectionDisplay('february', false);

  return false;

  }

Hi,

I need to hide above fields depending on the role, which is working well. But, wanted to display all fields if role is not equal to 'u_pm_tracking_user'. Please, someone help me out with this.

Thanks,

Manoj.

1 ACCEPTED SOLUTION

Geoffrey2
ServiceNow Employee
ServiceNow Employee

As an admin you automatically assume ALL roles.   Try the following condition:



function onLoad() {  


      if (g_user.hasRole('u_pm_tracking_user') && !g_user.hasRole('admin')) {  


              g_form.setDisplay('u_rate',false);  


              g_form.setDisplay('u_project_sow',false);  


              g_form.setSectionDisplay('january (next year)', false);  


              g_form.setSectionDisplay('february', false);    


      }  


}


View solution in original post

4 REPLIES 4

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Your code should work. Are you testing as an admin?   Admins inherit all roles by default so these fields would be hidden.


This is working well on different user. But as an admin, I need to see all fields. Client script is applying to me as well, those fields need to be displayed to me.


Geoffrey2
ServiceNow Employee
ServiceNow Employee

As an admin you automatically assume ALL roles.   Try the following condition:



function onLoad() {  


      if (g_user.hasRole('u_pm_tracking_user') && !g_user.hasRole('admin')) {  


              g_form.setDisplay('u_rate',false);  


              g_form.setDisplay('u_project_sow',false);  


              g_form.setSectionDisplay('january (next year)', false);  


              g_form.setSectionDisplay('february', false);    


      }  


}