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