How to get logged in user information in client script

Rajanmehta
Mega Guru

Hello,

We are on NewYork version. I have a need to find out about logged in user field value on client script (on INC table).

How can I get that information? Below is the custom field value I need in client script. (Service Type) 

I am able to get user Id, but nothing beyond that. 

Please advise.

Thanks,

find_real_file.png

 

Client Script on INC table.

find_real_file.png

 

 

1 ACCEPTED SOLUTION

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

You don't have access to everything about the user client side.  Given your script is an onLoad, you may be better off using a display business rule to set a g_scratchpad variable and utilizing that in your onLoad script. See the following article for an example:

https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/app-store/good_practices/client_s...

View solution in original post

6 REPLIES 6

Ben Rowny
Tera Guru

Assuming the database name of the ServiceType field is "u_servicetype", this should work:

var gr = new GlideRecord('sys_user');
gr.get(g_user.userID);
var svcType = gr.u_serviceType;

 

Note that this might have a slight UI delay since gr.get is doing a synchronous AJAX call.  You can do it asynchronously with a gr.query and a callback function, as:

var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id',g_user.userID);
gr.query(function(user){
   if(user.u_servicetype=="CMS"){
      return;
   } else {
      // all the rest of your code...
   }
});

Thanks Ben for your response. 

For some reason, it wouldn't give me the value when I tried using above steps, but it's good to know alternative way. (Without making AJAX call)

Thanks,

Rajan