How do I use an Onchange to insert the value of a reference field from the table I'm querying

kenmoulden
Kilo Contributor

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?

6 REPLIES 6

Gurpreet07
Mega Sage

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.


GlideAjax - ServiceNow Wiki


johnram
ServiceNow Employee
ServiceNow Employee

The ServiceNow Wiki content is no longer supported. Updated information about this topic is located here:


AJAX
GlideRecord  



Visit http://docs.servicenow.com for the latest product documentation


ajay1234
Kilo Contributor

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


Nitesh Chandana
Kilo Expert

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