The CreatorCon Call for Content is officially open! Get started here.

Client script setValue to local time

William Busby
Tera Guru

Trying to do some population of form field in catalog item via client script - pretty simple, just doing a GlideRecord lookup to the change_request table and pulling down the planned start date which I set in a field. Issue is the value being returned from the GlideRecord is UTC and I need to display it in local time. So far I haven't found a way to make the conversion. Any one cracked this nut before?

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {

          return;

    }

  var gr = new GlideRecord('change_request');

  gr.addQuery('sys_id', newValue);

  gr.query();

  if (gr.next()) {

  g_form.setValue('deploy_date_time', gr.start_date);

  }

}

1 ACCEPTED SOLUTION

No worries!   Here you go...



I designed this so you can reuse this script for other date fields in the change in case you ever need to.   What I mean there you will notice in the client script I am passing the AJAX Script Include "start_date" so it knows what field to convert.   If you ever have a need to translate other change dates like end_date this would still work.



Change your client script to the following:


function onChange(control, oldValue, newValue, isLoading) {


      if (isLoading || newValue == '')


              return;


   


      var ajax = new GlideAjax('LocalChangeDateTimeAjax');


      ajax.addParam('sysparm_name', 'getLocalTime');


      ajax.addParam('sysparm_dateField', 'start_date');


      ajax.addParam('sysparm_changeNumber', newValue);


      ajax.getXML(function () {


              g_form.setValue('deploy_date_time', ajax.getAnswer());


      });


}



Then navigate to System Definition\Script Includes and click New.


1. In Name enter: LocalChangeDateTimeAjax


2. Check the Client Callable checkbox - VERY important


3. In script, replace what's there with:


var LocalChangeDateTimeAjax = Class.create();



LocalChangeDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor , {


     


      getLocalTime: function() {


              var dateField = this.getParameter('sysparm_dateField');


              var changeID = this.getParameter('sysparm_changeNumber');


              var changeRec = new GlideRecord("change_request");


              changeRec.get(changeID);


              var dateValue = changeRec[dateField].getDisplayValue();



              return dateValue;


      },


     


      type: 'LocalChangeDateTimeAjax'


});


4. Click Submit.


5. Give it a test!


View solution in original post

14 REPLIES 14

Dilip Puligilla
Giga Guru

Hi Bill Busby



Hope the below link will help you


Set Current DateTime In Field - ServiceNow Wiki



Thanks


Dilip


Thanks for taking time to reply - I've seen this article but I'm not trying to get current date/time - I've got a date/time from the change request I need to display in local time.


The ServiceNow Wiki content is no longer supported. Updated information about this topic is located here: Date and Time Fields
 


  Visit http://docs.servicenow.com for the latest product documentation


Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Change this line:


g_form.setValue('deploy_date_time', gr.start_date);



TO:


g_form.setValue('deploy_date_time', gr.start_date.getDisplayValue());