Created
April 8, 2014 13:06
-
-
Save paulsena/10121245 to your computer and use it in GitHub Desktop.
ServiceNow Script Include, called by Client Script. This script calculates the difference between the current date and a date passed in as a parameter using 8X5 Business Workday Schedule.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Called by Client Script. This script calculates the difference between the current date and a date passed in as a parameter | |
* using 8X5 Business Workday Schedule. | |
*/ | |
var AjaxDateDiffCalcBusDays = Class.create(); | |
AjaxDateDiffCalcBusDays.prototype = Object.extendsObject(AbstractAjaxProcessor, { | |
dateDiffNow: function() | |
{ | |
// Get Catalog Item | |
var cat = new GlideRecord('sc_cat_item'); | |
cat.addQuery('sys_id',this.getParameter('sysparm_itemID')); | |
cat.query(); | |
cat.next(); | |
// Get Catalog Item Delivery Time Duration | |
var deliveryTime = new Packages.com.glide.glideobject.GlideDuration(); | |
deliveryTime.setValue(cat.delivery_time); | |
// Get Number of Days from Delivery Time (Milliseconds) | |
var deliveryTimeDays = deliveryTime.getNumericValue() / 86400000; //60*60*24*1000 = 1 day in milliseconds | |
// Get the User Timezone | |
var usr = new GlideRecord('sys_user'); | |
usr.get(gs.getUserID()); | |
var usrTZ = usr.time_zone; | |
// If User Timezone is Empty Get Timezone from System Default | |
if (usrTZ == '') | |
{ | |
usrTZ = gs.getProperty("glide.sys.default.tz"); | |
} | |
// Create schedule - pass in the sys_id of your standard 8X5 work day schedule and pass in the users timezone | |
var sched = new Packages.com.glide.schedules.Schedule('08fcd0830a0a0b2600079f56b1adb9ae',usrTZ); | |
// Calculate Delivery Date using 8X5 Business Working Hours Schedule | |
// specify the number of milliseconds to add, based on calendar | |
// this will add 1000ms*60sec*60mins*8hour(biz hours in a day)*(Catlog Item delivery days) | |
// the calculation makes more sense if you think of it as how many business hours to add, Not days. | |
var duration = new Packages.com.glide.glideobject.GlideDuration(); | |
duration.setValue(1000*60*60*8*deliveryTimeDays); | |
var actualDateTime = new Packages.com.glide.glideobject.GlideDateTime(); | |
var calcDeliverDateTime = new Packages.com.glide.glideobject.GlideDateTime(); | |
actualDateTime.setDisplayValue(gs.nowDateTime()); | |
calcDeliverDateTime.setValue(sched.add(actualDateTime,duration)); | |
var diff = gs.dateDiff(calcDeliverDateTime, this.getParameter('sysparm_newDate'), true); | |
return diff; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment