- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2017 11:12 AM
I am working on a SC item and I need to populate the name field (reference field) with the current user when a certain item is picked on the SC item. This is my code so far but I can't seem to get it to work. Any suggestions?
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var myself = g_form.getValue('voicemail_for_who');
var id = g_form.getValue('name');
if(myself == 'Myself') {
var user = new GlideRecord('sys_user');
user.addQuery('sys_id',id);
user.query();
if (user.next()) {
g_form.setValue('name', user.name);
}
}
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2017 11:23 AM
You need not do GlideRecord in that case. You can fetch the logged in user info at client side with help of g_user.
Please try with below script.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var myself = g_form.getValue('voicemail_for_who');
var id = g_form.getValue('name');
alert(myself); //This is for testing purpose to check what value you get in myself variable.
if(myself == 'Myself') {
g_form.setValue('name', g_user.userID); //use this if name is a reference field
g_form.setValue('name', g_user.userName;); //use this line if name is a string field.
}
}
Reference:
GlideUser (g user) - ServiceNow Wiki
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2017 11:26 AM
Try using the client-side GlideUser, e.g.
g_form.setValue('name', g_user.userName);
https://developer.servicenow.com/app.do#!/api_doc?v=helsinki&id=c_GlideUserAPI