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

Actually it is not a good practice to have GlideRecord calls in client scripts.   It is better to use Ajax or getReference with a function.   Based on what you shared I don't know the variable name of the change reference so you will need to fill that in, but everything else should be set.



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


    if (isLoading || newValue == '')


          return;



    var changeRec = g_form.getReference('VARIABLE-NAME-HERE', setDeployDate);


}



function setDeployDate(changeRec) {


    if (changeRec)


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


}


Thanks for taking the time to answer. I've tried the getDisplayValue() method and it returns an error "object does not support method or property   'getDisplayValue'" in both IE and Chrome.


Well I just tried in my instance and you are right, not working.   I could have sworn I have used getDisplayValue() in client scripts, but I guess not.   So you only option to get this to work is to use GlideAjax and have the date "translated" server side.   If you are interested, I can help walk you through that.


Incredibly kind of you to offer but I don't want to take more of your time if it can be avoided. I've opened an incident with support - they created the limitations so I'm putting the onus on them to at least point me in the right direction. If I get a working solution I'll post it. If not I'll be back hat in hand to accept your offer.


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!