Getting user's email from 'sys_user' table.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2019 02:32 PM
I am trying to get user's email from GlideRecord.
Here is the code (in Business Rule) -
gr = new GlideRecord ('sys_user');
var caller_name = current.caller_id.getDisplayValue();
gr.addQuery("name", caller_name);
gr.query();
if(gr.next){
gs.log('1A. Rec Count= '+ gr.getRowCount());
gs.log('1AA. gr.email '+ gr.email);
}
The log shows as -
Information | 1A. Rec Count= 1 |
Information | 1AA. gr.email undefined |
Table 'sys_user' has 'email' as a valid column name. I don't know why it can't fetch value, but shows 'undefiled as error.
Any suggestion, where I am going wrong?
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2019 02:37 PM
to get the email of caller_id you don't need to do glide query
gs.log('Email:'+ current.caller_id.email);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2019 03:04 PM
dvp is right, there is better way than do GlideRecord.
However, I think issue in your query is the following:
instead of
if(gr.next){
use
if(gr.next()){

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2019 05:36 PM
Query-based on Sys ID. It is more efficient.
I usually do this for reference fields:
var grUser = current.caller_id.getRefRecord();
if (grUser.isValidRecord()) {
gs.log(grUser.email);
}
//Alternative
var email = current.caller_id.email
gs.log(email);
I'm not sure which is more efficient.
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2019 06:55 PM
Seems like your gr is not defined.
First-line should have been:
var gr = new GlideRecord ('sys_user');
and then, as peter pointed out,
if(gr.next) should be - if(gr.next())
Thanks,
Swarnadip