How do I use an Onchange to insert the value of a reference field from the table I'm querying
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2017 06:00 PM
Hi all,
I'm relatively new to servicenow and javascript, any help would be amazing. I'm making a service catalog form and I'd like to populate a bunch of fields based on the input from a reference field (server table). However, 2 of the fields in the server record are reference fields, and my script is returning the sys_id and not the value of the field.
Example:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var server = new GlideRecord('cmdb_ci_server');
server.addQuery('sys_id', newValue);
server.query();
if(server.next()){
var cpu = server.cpu_count;
var ip = server.ip_address;
var drive = server.u_disk_volumes;
var ram = server.u_ram__gb_;
var assgn_to = server.assigned_to;
var owner = server.owned_by;
g_form.setValue('cpu_count_ken', cpu);
g_form.setValue('server_ip_address', ip);
g_form.setValue('disk_drives', drive);
g_form.setValue('server_ram', ram);
g_form.setValue('it_owner', assgn_to);
g_form.setValue('business_owner', owner);
}
}
The bolded items are returning the sys_id and not the value I'm looking for
Can someone help a newbie out?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2017 09:00 PM
Hi Ken,
As per best practices you should not use GlideRecord directly in the client scripts. Prefer to use Client Side GlideRecord instead.
Client Side GlideRecord - ServiceNow Wiki
Further you could try to get display value for the reference fields in below instructions.
var assgn_to = server.assigned_to.getDisplayValue();
var owner = server.owned_by.getDisplayValue();
If above is not working(May not work Client Side) then you need to use GlideAjax for this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2017 11:35 AM
The ServiceNow Wiki content is no longer supported. Updated information about this topic is located here:
Visit http://docs.servicenow.com for the latest product documentation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2017 09:24 PM
Hi Ken,
you can try getDisplayValue();
like,
g_form.setValue('it_owner', assgn_to.getDisplayValue());
g_form.setValue('business_owner', owner.getDisplayValue());
But writting GlideRecord() in client script is not a not practice .You try to do is using GlideAjax()
Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2017 09:36 PM
Hello kan,
try Replacing
var assgn_to = server.assigned_to;
var owner = server.owned_by;
with
var assgn_to = server.assigned_to.getDisplayValue();
var owner = server.owned_by.getDisplayValue();
Thanks,
Nitesh