What is the best way to get current user's email from GlideSystem?

Philippe Casidy
Tera Guru

Hi Community!

I initialize a catalogue item variable using javascript:gs.getUser().getEmail() to get current user's email.

But if the user updates the email address, I don't get the new one.

It seems I can reproduce this using a background script:

gs.print("Current user's email "+gs.getUser().getEmail());

And following this scenario

  1. run the script
    1. it will return your email address
  2. change the email address in your profile
  3. run the script again
    1. it returns the same email address as in 1.1

How can I get the new email address? Client-side friendly.
Note that I have the same behaviour with another field like 'last_name'

Thank you

Philippe

1 ACCEPTED SOLUTION

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

when you use gs.getUser().getEmail()) the values are set for the session and will change only when the user logs out and logs back in, that is when you will get the new email id at the time of user logging in.

Something like below will give you the accurate value always

var gr = new GlideRecord('sys_user')
gr.get(gs.getUserID());
gs.print("Current user's email "+gr.email);

You can add this in a function on script include and just call it from client script via glide ajax.

 

-Anurag

-Anurag

View solution in original post

8 REPLIES 8

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi  Philippe,

Something like below.

Client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ajax = new GlideAjax('UserUtil');
    ajax.addParam('sysparm_name', 'getUserEmail');
    ajax.addParam('sysparm_user_id', newValue);
    ajax.getXMLAnswer(function(answer) {
        if (answer.length > 0) {
            g_form.setValue('email', answer);
        }
    });
}

Script Include

var UserUtil = Class.create();
UserUtil.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    getUserEmail: function() {
        var userSysId = this.getParameter('sysparm_user_id');
        var grUser = new GlideRecord('sys_user');
        if (grUser.get(userSysId)) {
            return grUser.email;
        }
    },
    type: 'UserUtil'
});

Execution result:

find_real_file.png

Ankur Bawiskar
Tera Patron
Tera Patron

@Philippe Casidy 

yes you need to logout and login again to see the changes

But I would like to understand the business use-case why would logged in user update his/her email address on the fly?

You can still use default value and query user

javascript: var email; var rec = new GlideRecord('sys_user'); rec.get(gs.getUserID()); email = rec.email; email;

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Simon Christens
Kilo Sage

You can also use

gs.print(gs.getUser().getRecord().getValue('email'))

Philippe Casidy
Tera Guru

Hi @Anurag Tripathi , @Hitoshi Ozawa , @Ankur Bawiskar ,

Thank you all for your valuable feedback.

I would have hoped there was a way to "update" the current session and avoid querying the server from the client. But anyway, should have it been for another table than sys_user, likely I would have to do that anyway.

 

And for the business use: actually, this is for a custom field we have on sys_user but it was clearer to use the email. Let's say a user can request through a service catalogue to update their email address. Once approved, email address is updated in the profile. When they go to the service catalogue, old email is displayed.

The mentioned issue, is kind of an edge scenario, it occurs when the user is

- making the request

- have the request approved

- making a new request

All in the same session, let's see if this is ever happening. And actually, from a session perspective this is the correct information.

 

Thanks again for your answers, this helped me to reflect more about this situation.

 

Kind regards,

Philippe