getDisplayValue() not working in a catalog client script

Matt Small1
Tera Expert

We are currently on Dublin patch3 and I am trying to set up a catalog client script where when a 'requested for' person is chosen I want to write some of the information from the user record to read only variables.   The problem it seems is that the getDisplayValue() for a reference field on the user record, like manager, does not seem to work.  

 

Here is my catalog client script -

var rq4 = g_form.getValue('requested_for');

  //   alert('The req for id is ' + rq4);

  var gr = new GlideRecord("sys_user");

  gr.addQuery("sys_id", rq4);

  gr.query();

  if (gr.next()) {

  var mgr = gr.manager.getDisplayValue();

  //alert (mgr);

  g_form.setValue('v_current_mgr', mgr);

  g_form.setReadOnly('v_current_mgr', true);

 

If I remove the 'getDisplayValue from the mgr variable   then the sys_id of that manager field will be displayed but I want the name. Other than doing another GlideRecord on the manager   does anyone have an idea on why this would not work?   I can use this code in a backgroup sript and I get the manager name ok.   Just wondering if anyone else has run into this issue.

7 REPLIES 7

harikrish_v
Mega Guru

Hi Matthew,



You can have a look at this thread, your issue is similar to that:-



https://community.servicenow.com/message/711880#711880



Thanks & Regards,


Hari


tltoulson
Kilo Sage

Hi Matthew,



As indicated by neetusingh, getDisplayValue is not available on the client side GlideRecord, only server side.   If v_current_mgr is a reference field, it should set the sys_id and the display value automatically.   If it is a string field, it will display the sys_id.



However, the reference field comes at the cost of another query to the server.   My recommendation is to use GlideAjax either way.   Create a Script Include that obtains the sys_id and the display name and pass it back to the client.   You can find a lot of info on this from the following links.



GlideAjax - ServiceNow Wiki


Client Script Best Practices - ServiceNow Wiki



Also check out the following thread, particularly the scripts I show for using GlideAjax.



Re: Query Catalog Item Variable


adiddigi
Tera Guru

Just to summarize all the options you see here:



Let's get into a bit of history as far as I think 2012. g_form.getReference function wasn't available then, so we had to rely on GlideAjax as some comments here mentioned. You can use it, but you will have to write code to retrieve the information from ScriptInclude yourself.



A Glide Ajax again has two sections : A synchronous Glide Ajax and an Async Glide Ajax. If you are planning to go by this route, then I would suggest you use asynchronous Glide Ajax as it wouldn't make the user wait for the response back



The second option that was created specifically for the situations like yours is g_form.getReference. As someone above already gave you that option too. But what that comment stated was :



  1. var rq4 = g_form.getReference('requested_for');  
  2. g_form.setValue('v_current_mgr',rq4.manager);



This was the way g_form.getReference functioned for quite some time. But this had a very bad response time because g_form.getReference without a "call back" really waits for the Client - Server communication completes.




Hence couple of months later, ServiceNow introduced a Callback function to g_form.getReference so that the transaction become asynchronous and user didn't have to wait for the response from the server and so there will be no page freezing.




As a best practice, always remember, when you use getReference, you ALWAYS use it with callback. You can read about the getReference here :


GlideForm (g form) - ServiceNow Wiki