
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-03-2022 09:50 AM
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
- run the script
- it will return your email address
- change the email address in your profile
- run the script again
- 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
Solved! Go to Solution.
- Labels:
-
Multiple Versions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-03-2022 10:12 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-03-2022 09:43 PM
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:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-03-2022 10:10 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-03-2022 10:20 PM
You can also use
gs.print(gs.getUser().getRecord().getValue('email'))

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2022 10:52 PM
Hi
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