How to set values from a reference field into a single line and get the name value instead of sys_id?

Bruno S_
Giga Contributor

I am trying to set values from a reference field into two single line text fields with the following client script:

function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '') {
return;
}

var c1 = g_form.getReference('u_software_model', callbackfunction);

function callbackfunction(c1) {
g_form.setValue('current_manufacturer', c1.manufacturer);
g_form.setValue('current_product_owner', c1.owner);
}

}

 

The single line text fields are getting the sys_id instead of manufacturer name and owner name references, I already tried like the following change on this client script:

g_form.setValue('current_manufacturer', c1.manufacturer.name);

g_form.setValue('current_manufacturer', c1.manufacturer.name.getValue());

 

Can anyone help me to get this working?

2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

Hi Bruno,

It sounds like not only is u_software_model a reference type field, but so also are the manufacturer and owner fields on that table.  What you need to use in this case is a GlideAjax call to a Script Include instead of getReference.

This post does an excellent job of explaining how a GA works:

https://community.servicenow.com/community?id=community_article&sys_id=9f7ce2e1dbd0dbc01dcaf3231f961...

Once you have the structure down, the contents of the Script Include will need to do a GlideRecord query to return the u_software_model record that you passed in from the Client Script.  Inside of the GR results, you can add the manufacturer name and owner name fields to an object (or an array would be easier in this case, but the post linked above shows a JSON object in the example, so just follow that) via dot-walking like you tried in the getReference, then back in the Client Script with the answer from the SI, you can set the values of the 2 text fields.

Let me know if you need help working out the bugs.

Thanks Brad, I am going to try that.