Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Getting the logged in user

yundlu316
Kilo Guru

Hi Everyone,

I'm creating a widget and need to query a table we created that extends HR Case, but don't know how to set it to be the logged in user.   For the HR Profile table, I know the code looks like this:

var gr = new GlideRecord('hr_profile');

      gr.addQuery('user', gs.getUserID());

      gr.query();

However, that obviously doesn't work for our table:

var hr = new GlideRecord('x_81991_hr_onboarding');

  hr.addQuery('hr_profile', gs.getUserID());

  hr.query();

What do I have to set hr_profile to in order for it to query the logged in user?

3 REPLIES 3

Michael Ritchie
ServiceNow Employee

Do you have any reference fields that are tied to the logged in user on your 'x_81991_hr_onboarding table?   Like Opened For or Subject Person? Or is the linkage between that record's link to the hr_profile record?   Out of the box, only specific roled users can query the hr_profile table anyway so querying through that table may be problematic.


Hi Michael, there are other fields tied to sys_user such as Opened For and Caller.   I have admin access to this instance so querying hr_profile shouldn't be a problem.   Do you know how I can query hr_profile to set the current user equal to that field?


OK.   Yes you can but again ACLs may prevent non-admins from querying.   But here is an example of doing it:



var hr = new GlideRecord('x_81991_hr_onboarding');


hr.addQuery('hr_profile.user', gs.getUserID());


hr.query();



As you can see in your addQuery statement you can "dot-walk" there as well.   So in my example above it expects there to be a hr_profile field on your x_81991_hr_onboarding table (which if you extended hr_case should exist).   Then there the HR Profile has a linkage to the user record via a field called user.