- 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:17 AM
Hello Matthew,
Is the ID field a reference field?
Note: Client scripting uses either data available on the client or data retrieved from the server. Use client data as much as possible to eliminate the need for time-consuming server lookups. The top ways to get information from the server are g_scratchpad, and asynchronous GlideAjax lookup.
http://wiki.servicenow.com/index.php?title=Client_Script_Best_Practices#gsc.tab=0
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2017 11:22 AM
The id field is the variable I stored the reference field. The name field is the field I am trying to populate with the current user/logged in user. Whenever myself is selected, I want the name field to populate with the logged in user.

- 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
05-02-2017 04:21 PM
If I have answered your question, please mark my response as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.
If you are viewing this from the community inbox you will not see the correct answer button. If so, please review How to Mark Answers Correct From Inbox View