Explanation required reg getUserByID() method

Prasanthi1
Giga Contributor

Internal Explanation required for me.

 

var newUser = gs.getUser ();

gs.print (newUser.getUserByID ('abel.tuter').getFirstName ());

 

I didnot understand this, kindly explain line by line.

 

 

 

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Hi @Prasanthi 

 

 gs.getUser() -since it gives you a reference to the user object for the current user, it means you are getting control access to the whole user object.

If you use below code

var newUser = gs.getUser();

gs.print (newUser.getFirstName());

since you didn't mention any specific user for whom you need first name, it will give you your firstname i.e., current logged in user first name

 

but you have choosed the user_name in getUserByID ('abel.tuter'), so within the users list, it will identify which user has this UserID -abel.tuter and gives you that user firstname.

 

Methods does not serve always one purpose , based your requirement you extend your code to use it more.

Here is another link which might be useful for you

https://sn.jace.pro/docs/glideuser/

 

Kindly mark the response correct and helpful, once your issue is resolved and close the thread for others benefit in future

 

BR,

Harika

 

View solution in original post

7 REPLIES 7

Brad Bowman
Kilo Patron
Kilo Patron

Hi Prasanthi,

gs.getUser() method returns a user object. The user object is an internal representation of the currently logged in user and provides information about the user and various utility functions.

getUserByID method fetches a different user using the user_name field

­getFirstName() returns the first name of the user you fetched

Here is a reference to all of the functions (like getFirstName() available on the user object)

https://docs.servicenow.com/bundle/quebec-application-development/page/app-store/dev_portal/API_refe...

 

Harika Bhupathi
Giga Guru

Hello @Prashanti 

gs.getUser(); - Returns a reference to the user object for the current user/retrieve the current user

var newUser = gs.getUser (); - here you are storing the value in a variable called newUser

 

newUser.getUserByID ('abel.tuter').getFirstName () -

 

getUserByID(); - Use the getUserByID method to fetch a different user using the user_name field or sys_id on the target record.

here in your case you have used - getUserByID('abel.tuter'); //abel.tuter UserID(user_name) from which you are getting the user first name using getFirstName() method and that makes the below line which are trying to print using gs.print in background script

 

gs.print(newUser.getUserByID ('abel.tuter').getFirstName ());

 

Refer these URLS for more details

https://docs.servicenow.com/bundle/paris-application-development/page/script/server-scripting/task/t_GetAUserObject.html

https://community.servicenow.com/community?id=community_question&sys_id=bc4a47a9db5cdbc01dcaf3231f961910

https://developer.servicenow.com/dev.do#!/reference/api/quebec/server_legacy/c_GlideSystemAPI#r_GS-getUser

 

Please mark the response correct/helpful, if applicable

BR,

Harika

 

 

 

 

 

 

 

 

 

Why we require first line (var newUser = gs.getUser()) in that code.

By using current user object, can we access another user?

A bit of confusion is there, how can we get another user details using current logged in user object?

pls explain

Hi Prasanthi, you don't need to seperate your code. You can do this in a single line like below:

gs.getUser().getUserByID('abel.tuter').getFirstName();