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 user's email from 'sys_user' table.

rajanmurkut
Mega Guru

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 -

 

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?

6 REPLIES 6

dvp
Mega Sage

to get the email of caller_id you don't need to do glide query

gs.log('Email:'+ current.caller_id.email);

peter_repan
Kilo Sage

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()){

 

 

paulmorris
Giga Sage

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

swarnadip
ServiceNow Employee

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