GlideRecord getting the right value

khairilar
Tera Contributor

Hi,

 

I have issue with the script below where:

 

1. The Actual Start and Actual End return value is on GMT +0. I have tried getDisplayValue and it's only works with gs_print and it doesn works if I used g_form.setValue. How do I resolve this display value to follow the user timezone?

2. The Task Number return value is shown sys_id. I have tried getDisplayValue but it still doesn't work. How do I get the display value instead of sys_id? Display value such as 'CHG000001' or 'INC000001' etc.

 

 

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

   //Type appropriate comment here, and begin script below
var ga = new GlideRecord('cmdb_ci_outage');
var ou = g_form.getValue('outage_no');

ga.addQuery('number', ou);
ga.query();

while(ga.next()) {

   g_form.setValue('outage_actual_start', ga.begin);
   g_form.setValue('outage_actual_end', ga.end);
   g_form.setValue('task_num', ga.task_number);
 }
}

 

4 REPLIES 4

AnveshKumar M
Tera Sage
Tera Sage

Hi @khairilar 

Using GlideRecord in client script is not a best practice. You can try client callable script include with GlideAjax for this.

 

However, the task_number field is a reference field in cmdb_ci_outage table so it returns sys_id only and there is no getDisplayValue method in client side GlideRecord.

 

For handling date times in client script in user preferred date time format refer the following article.

 

https://servicenowguru.com/scripting/client-scripts-scripting/client-side-dates-in-servicenow/

 

Please mark my answer helpful and accept as a solution if it helped 👍

Thanks,
Anvesh

Tai Vu
Kilo Patron
Kilo Patron

Hi @khairilar 

To get display value of a reference field in the core UI, you can try the API below.

getDisplayBox(String fieldName)

Sample

var task_number = g_form.getDisplayBox('task_number').value;

 

And don't use GlideRecord directly in your Client Sript.

You can call a function from an AJAX Script Include, or use the getReference API.

getReference(String fieldName, Function callBack)

 

Cheers,

Tai Vu

Ankur Bawiskar
Tera Patron
Tera Patron

@khairilar 

why not have a reference field/variable rather than asking user to enter number?

Users are not expected to remember the number

Once you make the field as reference you can use getReference() with callback to set the value

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thanks all for your recommendations, let me try based on suggestions.