- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2019 02:29 PM
All,
My script below isn't working in the Service Portal. It is just a simple getReference with a callback to set the name of Requested For field.
Basically if TCRA is TRUE then set the name field to what is in the Requested For field. Seems simple enough, but Name field is coming up blank. Any help would be appreciated as I am not sure what I am missing here.
The client script has the UI type set to 'All'
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var requested_for = g_form.getReference("requested_for",getName);
}
function getName(requested_for){
var tcra = g_form.getValue('tcra');
if(tcra == 'true' && requested_for !=''){
g_form.setValue('user_name', requested_for.name);
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2019 04:12 PM
Hi Brett,
What type of variable is "user_name"? Your screenshot looks like you are using a reference variable which would normally be set with the sys_id of the record you are returning in your script. If you actually want the requested_for "user_name" from the sys_user table then you should amend the "user_name" variable to a type Single Line Text and use the following code:
Code Example:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading)
return;
if (newValue == '' || newValue == null) {
g_form.setValue('user_name', ''); //clear the user name variable if newValue is blank
return;
}
var requested_for = g_form.getReference("requested_for",setName); //getReference with call back.
}
function setName(requested_for) {
var tcra = g_form.getValue('tcra');
/*if a record is returned set the user_name variable using the user_name field from the sys_user table*/
if (requested_for && tcra == "true")
g_form.setValue('user_name', requested_for.user_name); //user_name variable should be type - string
}
Let me know if this worked for you.
Brent
P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2019 02:43 PM
By looking at the screenshot Name field looks like a Reference type not a Single line text.
For the reference field we can only set values by using a sys_id value
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2019 02:48 PM
That is correct and in my script below the screenshot I am setting the Name field to 'requested_for.name'

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2019 02:53 PM
Try the below script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var requested_for = g_form.getValue("requested_for");
var tcra = g_form.getValue('tcra');
if(tcra == 'true' && requested_for !=''){
g_form.setValue('user_name', requested_for);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2019 02:58 PM
unless things have changed getReference isn't eligible for mobile use.. works fine in classic but shouldn't work on the portal.
I take that back it will now work but only in a call back as an asynch call..
https://docs.servicenow.com/bundle/kingston-application-development/page/script/client-scripts/reference/r_MobilePlatformMigrationImpacts.html
so it SHOULD work..howeever you need to mark get name as a function so
var requested_for = g_form.getReference("requested_for",getName
should be var requested_for = g_form.getReference("requested_for",getName()