- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-14-2016 01:52 PM
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):
MM-dd-yyyy:
yyyy-MM-dd:
Does NOT work for:
dd/MM/yyyy:
Error seen:
Once you click OK, it shows this;
dd-MM-yyyy: (see same error as above, then this after clicking OK):
dd.MM.yyyy: (see same error as above, then this after clicking OK):
I'm seeing no difference in the definitions of the variable fields:
Has anyone seen this previously and know how to fix it?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-21-2016 02:43 PM
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-21-2016 02:43 PM
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;