getReference displays sys_id for reference field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-24-2015 08:58 AM
Hi,
I'm trying to populate a set of variables based on the imput of another variable and can't quite find the answer i'm looking for.
for these 2 lines, i get the sys_id but would like the display value. (both fields are reference fields on the user record)
g_form.setValue('location', user.location);
g_form.setValue('cost_code',user.cost_center);
here is my variable script.
----
function onChange(control, oldValue, newValue, isLoading) {
if(!isLoading){
if (oldValue != newValue) {
var User = g_form.getReference('name', fillcustomer);
}
}
}
function fillcustomer(user) {
g_form.setValue('phone',user.phone);
g_form.setValue('location', user.location);
g_form.setValue('desk', user.u_desk);
g_form.setValue('email',user.email);
g_form.setValue('cost_code',user.cost_center);
}
-----
I have trid the following but no joy.
//g_form.setValue('cost_code',user.cost_center.name);
//g_form.setValue('cost_code',user.cost_center.getDisplayValue());
//g_form.setValue('cost_code',user.code);
any help would be greatly appreciated

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-24-2015 09:18 AM
Hi Alan,
This is beacuse you have created the field as string type. If you create it as referenced field and point it to the table to what the value is supposed to be set then it will show the display value.
If your req is to have only string field in that case you can do client script + GlideAjax. Please let me know if you need any help.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-24-2015 09:29 AM
Thanks Pradeep,
changing the fields to type reference works.
interested to see how the glideajax and client script would work? can you help with this?
which is best practice?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-24-2015 09:34 AM
Hi Alan,
The best practice to use would be client script + GlideAjax. I am pointing you to the links for more info.
http://wiki.servicenow.com/index.php?title=GlideAjax#gsc.tab=0
If you are struck, I would pitch in to help you out.
Please mark the response if your query is answered.