Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Fix Script to Update date/time field from date field

Brett21
Tera Guru

I am working on a requirement where we needed to update our existing date fields to date/time fields.  Since we need to keep the historical data in Production.  I am working on fix script that I want to execute to populate the new date/time fields with the existing data.  However, the fix script is resulting undefined when I am trying to grab the displayValue for date field.  Any tips would be appreciated.

var gr = new GlideRecord('change_request');
gr.EncodedQuery('u_outage_required=true^u_outage_start_dateISNOTEMPTY^ORu_outage_end_dateISNOTEMPTY');
gr.addQuery('u_outage_required', true);
gr.orderBy('sys_created_on');
gr.setLimit(2);
gr.query();
var count = 0;
while(gr.next())
{
count++;
var copystart = gr.getDisplayValue('u_outage_start');  //date field
var copyend = gr.getDisplayValue('u_outage_end'); //date field
gr.setValue('u_outage_startdate', copystart); // set date/time field
gr.setValue('u_outage_enddate', copyend); //set date/time field
gr.setWorkflow(false);
gr.update();   //Save it
}
gs.print(count + 'Start :'+ copystart);  //Log shows undefined for variable copystart

 

2 REPLIES 2

-Andrew-
Kilo Sage

Hi Brett,

I believe you would need to convert the GlideDate to GlideDateTime.

I'd recommending reading through this article from Chuck, it's very useful: Convert Date to Date Time - Blogs - Blog - ServiceNow Community

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

Try this

var gr = new GlideRecord('change_request');
gr.addEncodedQuery('u_outage_required=true^u_outage_start_dateISNOTEMPTY^ORu_outage_end_dateISNOTEMPTY');
gr.addQuery('u_outage_required', true);
gr.orderBy('sys_created_on');
gr.setLimit(2);
gr.query();
var count = 0;
while(gr.next())
{
count++;
var copystart = gr.getDisplayValue('u_outage_start');  //date field
var copyend = gr.getDisplayValue('u_outage_end'); //date field
gr.setValue('u_outage_startdate', copystart); // set date/time field
gr.setValue('u_outage_enddate', copyend); //set date/time field
gr.setWorkflow(false);
gr.update();   //Save it
}
gs.print(count + 'Start :'+ copystart);  //Log shows undefined for variable copystart

 

 

You used EncodedQuery instead of addEncodedQuery

-Anurag