Calling getUserByID() works in global scope but not in application scope

pathat
Kilo Expert

Hello.

I have a business rule (BR) which has a custom script which gets information about a user other than the user I am currently logged in as, just like the following:

var ourUser = gs.getUser();

gs.print(ourUser.getFirstName()); //should print out the first name of the user you are currently logged in as

ourUser = ourUser.getUserByID('abel.tuter'); //fetched a different user   --> Exception thrown here when in APPLICATION scope

gs. ­print(ourUser. ­getFirstName()); //this should be the first name of the user you fetched above

This script was working fine when the BR has a GLOBAL scope. But the same script fails when it calls ourUser.getUserByID() in another BR which has an APPLICATION scope. The error is: TypeError: undefined is not a function

Thoughts anybody?

10 REPLIES 10

Hi Pat,



I am able to reproduce this issue. I will check with the platform team and get back to you.


Thanks for your patience.


Hi Pradeep,



This thread branched off and talked about .getFirstName( for scoped application however I am   having issues with the same. When I enter the code myUser. in my helper class getUserByID does not show in autocomplete. This seems a bit different than the the problem and solution you described. I am a relative beginner. Is my issue related to scoping as well?



My Code


var myUser = gs.getUser();


myUser = myUser .getUserByID('abel.tuter'); //when I remove this line from of my helper class it runs. Also autocomplete does not show getUserByID for myUser. not supported?



Thanks,


Doug


Hi Pat,



Scoped API doesn't support User#getFirstName. I will raise a Problem with the platform team.


For a workaround you should be able to GlideRecord and fetch the field.



Please let me know if you have any questions.



Thanks,


Pradeep Shamra


Hi Pradeep.


Is there documentation showing all the exact field names for the sys_user table, which is what I would query to get a GlideRecord. For example, to get the GlideRecord for the current user, I would code this as follows, but what if I wanted to get the department the user belongs to (which I normally would have gotten using ourUser.getDepartmentID)?


 


user = new GlideRecord('sys_user');


  user.addQuery('sys_id', current.sys_id);


Grant Hulbert
ServiceNow Employee
ServiceNow Employee

Hi Pat, try this:



var gr = new GlideRecord('sys_user');


gr.get('5137153cc611227c000bbd1bd8cd2005'); // easier way to find a specific sys_id; see https://developer.servicenow.com/app.do#!/api_doc?to=method_class__get_object_name__object_value__gl...


gs.info( gr.name );


gs.info( gr.department );


gs.info( gr.department.getDisplayValue() );