The CreatorCon Call for Content is officially open! Get started here.

getting a sysid and using it to lookup a name

drichter
Kilo Contributor

why won't this script use the sys_id returned by gs.getUserID()) to look up the name ?
it is displaying the message within the next loop, which tells me it found the sys_id, but the user name is blank.

var u=new GlideRecord('sys_user');
u.addQuery('sys_id',gs.getUserID());
u.query();
while (u.next() ) {
gs.log (" u.name:",u.last_name);
}

Thank you.

5 REPLIES 5

CapaJC
ServiceNow Employee
ServiceNow Employee

Your log statement is wrong. It should probably read:
gs.log("u.name: " + u.last_name);


CapaJC
ServiceNow Employee
ServiceNow Employee

You can also simply by using a .get() like the following:

var u = new GlideRecord("sys_user");
if (u.get(gs.getUserID()))
gs.log("u.name: " + u.last_name);

And if you get the User object, there's already a method to return the last name, so to do this specific thing you could do it in one line:

gs.log("u.name: " + gs.getUser().getLastName());


drichter
Kilo Contributor

Thank you ! so if I use "get" to pull a glide record I must use the get method, not a query method.


That's correct. 'get' eliminates the 'addQuery', 'query()' and 'next()' arguments completely from the query. It simply finds the single record and populates it in the variable.

SNCGuru has great cheat sheets on all of this information. It's not too difficult once you get the hang of a few concepts.
http://www.servicenowguru.com/tag/cheat-sheet/