Pulling the display value of a reference field into a string field...

_-jasonr-_
Giga Contributor

Hello All,

I am currently working on a form where I need to get a sys_user reference and then display the values into string fields on said form.

The following code gets the reference data, but when I set the values into the form, the location and department reference fields place the sys_id into the field.

function onLoad() {

    //Type appropriate comment here, and begin script below

   

  var reqName = g_form.getReference('u_reference_1', setEnhUserFields);

}

function setEnhUserFields(reqName) {

  g_form.setValue('u_string_3', reqName.user_name);

  g_form.setValue('u_string_4', reqName.phone);

  g_form.setValue('u_string_5', reqName.email);

  g_form.setValue('u_string_6', reqName.department);

  g_form.setValue('u_string_7', reqName.location);

}

Is there any way to get the display value of the location and department reference field display values into the string fields on the enhancement? Do I need a script include?

Thanks.

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Jason,



You are right. You need to create a script include(client callable set to true) and call it via GlideAjax at the client side. More info here.


http://wiki.servicenow.com/index.php?title=GlideAjax#gsc.tab=0



At server side, you can use getDisplayValue() to get the display value of the reference field.


http://wiki.servicenow.com/index.php?title=GlideRecord#getDisplayValue


View solution in original post

5 REPLIES 5

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Jason,



You are right. You need to create a script include(client callable set to true) and call it via GlideAjax at the client side. More info here.


http://wiki.servicenow.com/index.php?title=GlideAjax#gsc.tab=0



At server side, you can use getDisplayValue() to get the display value of the reference field.


http://wiki.servicenow.com/index.php?title=GlideRecord#getDisplayValue


Let me know if that answered your question. If so, please mark it as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list. Thank you




Pradeep,



What I did was change the target fields from string fields to reference fields for their respective tables. When the code


var reqName = g_form.getReference('u_reference_1', setEnhUserFields);


fetches the user data record, it also gets the reference field information.



The fields now display the referenced display data. Not the "best practice" method, but it works for my needs.


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