setValue on date field when returning date using getByFormat - Not working for some formats

kristenankeny
Tera Guru

We have two date variables in a variable set used by multiple forms. There are two possible ways the fields are populated - either they are on a form that is part of an order guide and we add a set number of days to the current date, or we are using the delivery_time on the catalog item to add days to the current date. We call a script include to get the right date and return the date in the date format for the current user:

Client script on variable set:

function onLoad() {

  if($('sysparm_id') !== null){

  var ga = new GlideAjax('TestingDate');

  ga.addParam('sysparm_name','itemAddDays');

  ga.addParam('sysparm_item',g_form.getParameter("sysparm_id"));

  ga.addParam('sysparm_cu',g_user.userID);

  ga.getXML(setDates);

  }

  else if($('sysparm_guide') !== null){

  //this works to set both to 14 days in future

  var gg = new GlideAjax('TestingDate');

  gg.addParam('sysparm_name','addDays');

  gg.addParam('sysparm_days',14);

  gg.addParam('sysparm_cu',g_user.userID);

  gg.getXML(setDates);

  }

}

function setDates(response){

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

  g_form.setValue('target_sla_completion_date',answer);

  g_form.setValue('requested_completion_date',answer);

  g_form.setReadOnly('target_sla_completion_date',true);

}

Script include:

var TestingDate = Class.create();

TestingDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {

  addDays: function(){

  var today = gs.now();

  var sla = new GlideDate();

  sla.setValue(today);

  sla.addDays(this.getParameter('sysparm_days'));

  //get user date format

  var format = '';

  var cu = new GlideRecord('sys_user');

  cu.addQuery('sys_id',this.getParameter('sysparm_cu'));

  cu.query();

  if(cu.next()){

  if(cu.date_format){

  format = cu.date_format;

  }

  }

  if(format != ''){

  return sla.getByFormat(format);

  }

  else{

  return sla;

  }

  },

  itemAddDays: function(){

  gs.info('itemAddDays called the function');

  var c = new GlideRecord('sc_cat_item');

  c.addQuery('sys_id',this.getParameter('sysparm_item'));

  c.query();

  if(c.next()){

  gs.info('itemAddDays found the catalog item');

  var date = gs.now();

  var dur = c.delivery_time.dateNumericValue();

  var days = dur/24/60/60/1000;

  gs.info('itemAddDays found the duration to be ' + days);

  //determine what the due date should be

  var dayOfTheWeek = date.getDay();

  var calendarDays = days;

  var deliveryDay = dayOfTheWeek + days;

  if(deliveryDay >= 6){

  days -= 6 - dayOfTheWeek;

  calendarDays += 2;

  var deliveryWeeks = Math.floor(days/5);

  calendarDays += deliveryWeeks * 2;

  }

  //gs.info('itemAddDays found the new date to be ' + date);

  var newDate = new GlideDate();

  newDate.setValue(date);

  newDate.addDays(calendarDays);

  gs.info('itemAddDays found new date to be ' + newDate.getDisplayValue());

  //get current user date format

  var format = '';

  var cu = new GlideRecord('sys_user');

  cu.addQuery('sys_id',this.getParameter('sysparm_cu'));

  cu.query();

  if(cu.next()){

  if(cu.date_format){

  format = cu.date_format;

  }

  }

  if(format != ''){

  return newDate.getByFormat(format);

  }

  else{

  return newDate;

  }

  }

  },

  type: 'TestingDate'

});

If we do not set the format, the "setValue" on the form fails if the user has set a date format. However, we are getting an error with some of the date formats.

When the script works, both date fields are populated with the date in the correct format.

Default (system):

find_real_file.png

MM-dd-yyyy:

find_real_file.png

yyyy-MM-dd:

find_real_file.png

Does NOT work for:

dd/MM/yyyy:

        Error seen:

        find_real_file.png

        Once you click OK, it shows this;

find_real_file.png

dd-MM-yyyy: (see same error as above, then this after clicking OK):

find_real_file.png

dd.MM.yyyy: (see same error as above, then this after clicking OK):

find_real_file.png

I'm seeing no difference in the definitions of the variable fields:

find_real_file.png

find_real_file.png

Has anyone seen this previously and know how to fix it?

1 ACCEPTED SOLUTION

kristenankeny
Tera Guru

After talking with British and then posting a follow up to the community, we have resolved the issue.



Goal: We have a variable set shared across multiple catalog items. We needed to set two date fields in this variable set to a date X number of days out based on a schedule. The number of days out is determined by the "Delivery time" on the catalog item.



Issue: Scripts were conflicting with OOB ServiceNow date formats end users can select.



Solution: Set the dates using the "Default value" field on the variable definition, putting javascript in this field. This is the script:



javascript:


// Get the datetime now


var nowGdt = new GlideDate();


// The name of the schedule


var myScheduleName = '9-5 weekdays excluding holidays';



// The basis of our calculation


var dueDays = 0;


var cat = new GlideRecord('sc_cat_item');


cat.addQuery('sys_id',gs.action.getGlideURI().getMap().get('sysparm_id'));


cat.query();


if(cat.next()){


var dur = cat.delivery_time.dateNumericValue();


dueDays = dur/24/60/60/1000;


}


var dueWorkingHours = 8;



// The amount of time we want for the duration


var dueSeconds = dueDays*dueWorkingHours*60*60;


var leadTime = new GlideDuration(dueSeconds*1000);



// Calculate the Due Date!


var dueDateGdt;


var schedRec = new GlideRecord('cmn_schedule');


if (schedRec.get('name', myScheduleName)) {          


        var sched = new GlideSchedule(schedRec.sys_id);


        dueDateGdt = sched.add(nowGdt, leadTime, '');


}


dueDateGdt;


View solution in original post

5 REPLIES 5

kristenankeny
Tera Guru

Bump in hopes someone might see this this morning and know how to resolve.


britwill
ServiceNow Employee
ServiceNow Employee

Hi Kristen:



Thanks for your community post.



Issue: GlideDateTime(g) method does not accept dd.MM.yyyy or dd//MM/yyyy user formats. Result: Throws an invalid date time error.



Background: If the incoming parameter value to "new GlideDateTime()" is a String, that String is expected to be formatted using the Glide system format of "yyyy-MM-dd HH:mm:ss", and the incoming value is interpreted as being in the Glide system time zone, UTC.



Issue Reproducible : Yes



Details (including steps to reproduce):


1.) Login to https://<your-instance>.service-now.com/navpage.do
2.) Self-Service->My Profile
3.) Change date format to "dd.MM.yyyy"
4.) Elevate to "Security Admin"
5.) Scripts - Background
6.) Execute the following script

var dtNow = new GlideDateTime(gs.nowDateTime());
gs.print(dtNow.getNumericValue());

Expected: numeric value for current date/time
Actual :


GlideDateTime: Invalid date time: '16.12.2014 14:18:22', ignored
*** Script: 0



Solution:


Using setDisplayValue for your 'Date' should produce your desired results regardless of format.



var dtNow = new GlideDateTime();
dtNow.setDisplayValue(gs.nowDateTime());
gs.print(dtNow.getNumericValue());



Regards,


Brit


Hi Brit,



What if the field we are setting is just a date field?



Thanks!


Hi Kristen-



This addresses all date fields using the GlideDateTime(g) method with dd.MM.yyyy and dd//MM/yyyy formats.



Regards,


Brit