Formatting Date in Client Script - Losing 1 day

Steven Parker
Giga Sage

So I am using a call back function in an onChange Client Script to grab some data from a table and populated fields on the form.  Well the Date fields from the table were coming in the wrong format (yyyy/DD/mm I believe) so I worked to fixed that and while this does fix the formatting, it causes the two dates to be off by 1 day (So if it's 12/17/2022 in the table, it will populate the form with 12/16/2022).  I know it has to do with the system time I believe from using new Date(), but how can I resolve this?  The last_review_date and next_review_date variables below are both doing it.  I just want the dates to pull straight from the table without losing a day and be formatted properly so the form can be submitted if nothing is changed.

 

 

 

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
	
		var processDetails = g_form.getReference('existing_process_name', getDetails);  //Callback function is geDetails this allows the browser to continue to run through the script.  
  
  function getDetails(processDetails) {  
   if(processDetails)
		var last_review_date = formatDate(new Date(processDetails.u_last_review_date), 'MM/dd/yyyy');
		var next_review_date = formatDate(new Date(processDetails.u_next_review_date), 'MM/dd/yyyy');
		g_form.setValue('type', processDetails.u_type);
		g_form.setValue('description', processDetails.u_description);
		g_form.setValue('last_review_date', last_review_date);
		g_form.setValue('next_review_date', next_review_date);
		g_form.setValue('process_owner', processDetails.u_process_owner);
		g_form.setValue('notes', processDetails.u_notes);
}

   
}

 

 

 

 

 

 


Please mark this response as correct and/or helpful if it assisted you with your question.
Steven
1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Hi Steven,

I would recommend a GlideAjax approach so that the dates can be fetched from the server.  Here's an excellent example on how to do this returning multiple values:

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2... 

so you would pass newValue to the Script Include, then do a GlideRecord on the referenced table to return each field in an object.  For the 2 date fields, use lines like this:

 

var lrd = new GlideDateTime(gr.u_last_review_date);
var nrd = new GlideDateTime(gr.u_next_review_date); 
...
var results = {
    "last_review_date" : lrd.getDisplayValue(),
    "next_review_date" : nrd.getDisplayValue(),
...
}; 

View solution in original post

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

Hi Steven,

I would recommend a GlideAjax approach so that the dates can be fetched from the server.  Here's an excellent example on how to do this returning multiple values:

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2... 

so you would pass newValue to the Script Include, then do a GlideRecord on the referenced table to return each field in an object.  For the 2 date fields, use lines like this:

 

var lrd = new GlideDateTime(gr.u_last_review_date);
var nrd = new GlideDateTime(gr.u_next_review_date); 
...
var results = {
    "last_review_date" : lrd.getDisplayValue(),
    "next_review_date" : nrd.getDisplayValue(),
...
}; 

Edited reply:

 

That worked!  I forgot to use "getDisplayValue".  Thank you for your help!  I will make a separate reply showing everyone the scripts to accomplish this.


Please mark this response as correct and/or helpful if it assisted you with your question.
Steven

Steven Parker
Giga Sage

For anyone reading this in the future and having issues with pulling dates in the correct format from a table using a Client Script and Call Back or getRefence, here is the solution that worked for me.  This allows us to use .getDisplayValue0 which brings the dates over in the same format they are displayed.  You will need to create a Client Script with Glide Ajax and a Script Include.

 

Client Script ("Existing Process Name" was a reference field on my request form)

 

 

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

var ga = new GlideAjax('reoccurringProcessData'); //Name of Class/Script Include
ga.addParam('sysparm_name','getData'); //Function in Script Include
ga.addParam('sysparm_req', g_form.getValue("existing_process_name")); //Field to query in Script Include
ga.getXML(ajaxProcessor);

function ajaxProcessor(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");

if (answer) {
		var returneddata = JSON.parse(answer);
		g_form.setValue("type", returneddata.type);
		g_form.setValue("description", returneddata.description);
		g_form.setValue("last_review_date", returneddata.last_review_date);
		g_form.setValue("next_review_date", returneddata.next_review_date);
		g_form.setValue("process_owner", returneddata.process_owner);
		g_form.setValue("notes", returneddata.notes);
    } 
	
 }
}

 

 

 

Script Include (Make sure to check "Client Callable" when first creating it)

Name:  reoccurringProcessData

 

 

var reoccurringProcessData = Class.create();
reoccurringProcessData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getData: function () {
	
var processID = this.getParameter('sysparm_req'); //you need to pass this from client script
var gr = new GlideRecord('u_founders_reoccurring_processes');
gr.addQuery('sys_id', processID);
gr.query();

if(gr.next()) {
var results = {
        "type": gr.getValue("u_type"),
		"description": gr.getValue("u_description"),
		"last_review_date": gr.getDisplayValue("u_last_review_date"),
		"next_review_date": gr.getDisplayValue("u_next_review_date"),
		"process_owner": gr.getValue("u_process_owner"),
		"notes": gr.getValue("u_notes")
      };
    return JSON.stringify(results);
}
},
	
    type: 'reoccurringProcessData'

	
});

 

 

 


Please mark this response as correct and/or helpful if it assisted you with your question.
Steven