getReference displays sys_id for reference field

Alan Norris
ServiceNow Employee
ServiceNow Employee

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

7 REPLIES 7

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.


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?


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.