Date manipulation: trigger a task based on staff end date entered in RITM

 - if the end date on the staff leavers service request < todays date, set zero seconds

- if the end date is today, set number of seconds as today date/time up to today at 18:00

- if the end date is future, set the wait time in seconds as end date at 18:00

scratchpad will then be passed to a wait until block in the workflow



//--see STRY0010592 for background var bDateInPast = false; //---SET SCRATCHPAD--TIMER 1----------------------------- workflow.scratchpad.ritmtimetowait = 0; //--value in seconds var sNumL = current.number + ': '; //--get the predetermined TIME to trigger the task********************* var sTimeToTrigger_prop = gs.getProperty('he.ritm.leaverenddate.timetotrigger'); //normally set to 18:00, (can be set to lower in test environments) var gtTriggerTime = new GlideTime(); gtTriggerTime.setValue(sTimeToTrigger_prop); //gs.log(sNumL + gt.getByFormat("HH:mm"), 'wf:StaffLeaver'); var iTriggerTime = gtTriggerTime.getNumericValue(); //--64800000 corresponds to 18:00:00) //****************************************** //--if today is the end date, include the trigger time for today var iTodaysDate =; //gs.log(sNumL+'iTodaysDate:' + iTodaysDate.toString(), 'wf:StaffLeaver'); var sTodaysDateTimeZero = new GlideDateTime(iTodaysDate.toString()); var iTodaysDateTimeZero = sTodaysDateTimeZero.getNumericValue(); //gs.log(sNumL+iTodaysDateTimeZero.getNumericValue(), 'wf:StaffLeaver'); var iTriggerDateTime = iTodaysDateTimeZero + iTriggerTime; var sTODAYDATEARR = sTodaysDateTimeZero.toString().split(" "); //--now set the trigger time for today: var sDateCombined = sTODAYDATEARR[0] + " " + gtTriggerTime.getByFormat("HH:mm:ss").toString(); var triggerDateTime_final = new GlideDateTime(sDateCombined); var triggerDateTime_final_numeric = triggerDateTime_final.getNumericValue(); var dDayOfWeek = triggerDateTime_final.getDayOfWeek(); //gs.log(sNumL+'iTriggerDateTime: ' + iTriggerDateTime, 'wf:StaffLeaver'); //****************************************** //--get the time right now********************* var dateNow = new GlideDateTime(); var dateNowF = (dateNow.getDisplayValue().toString()); var dateNowT_arr = dateNowF.split(" "); var dateNowT_F = dateNowT_arr[1]; //gs.log(sNumL+'dateNowT_F:' +dateNowT_F, 'wf:StaffLeaver'); //--set the final trigger time-today's date var gtFormatTime = new GlideTime(); gtFormatTime.setValue(dateNowT_F); var iFormatTime = gtFormatTime.getNumericValue(); sDateCombined = sTODAYDATEARR[0] + " " + gtFormatTime.getByFormat("HH:mm:ss").toString(); var DateTimeNow_final = new GlideDateTime(sDateCombined); var DateTimeNow_final_numeric = DateTimeNow_final.getNumericValue(); //gs.log(sNumL+'iFormatTime:' +iFormatTime, 'wf:StaffLeaver'); //*************************************************************** //--get the RITM end date variable********************* var dEndDate = new GlideDateTime(current.variables.end_date); var iEndDateNum = dEndDate.getNumericValue(); //gs.log(sNumL + 'iEndDateNum: ' + iEndDateNum, 'wf:StaffLeaver'); //*************************************************************** //--set the future trigger date (used when end date in future)********************* var sENDDATEARR = dEndDate.toString().split(" "); var sFutureDateCombined = sENDDATEARR[0] + " " + gtTriggerTime.getByFormat("HH:mm:ss").toString(); var triggerDateTime_future = new GlideDateTime(sFutureDateCombined); gs.print(triggerDateTime_future); //*************************************************************** //--check if end date before today's date if (iEndDateNum < iTodaysDateTimeZero) { bDateInPast = true; gs.log(sNumL + 'end date in past', 'wf:StaffLeaver'); workflow.scratchpad.ritmtimetowait = 0; //--end date in past } else { //--check if end date=today's date if (iEndDateNum == iTodaysDateTimeZero) { //--end date is today gs.log(sNumL + 'end date today', 'wf:StaffLeaver'); workflow.scratchpad.ritmtimetowait = (parseInt(gs.dateDiff(DateTimeNow_final, triggerDateTime_final, true)) + 3600); //-- returns the number of seconds as String, but seems to deduct an hour due to database settings } else { //--check if end date in future if (iEndDateNum > iTodaysDateTimeZero) { gs.log(sNumL + 'end date future', 'wf:StaffLeaver'); workflow.scratchpad.ritmtimetowait = (parseInt(gs.dateDiff(DateTimeNow_final, triggerDateTime_future, true)) + 3600); //-- returns the number of seconds as String, but seems to deduct an hour due to database settings } } } gs.log(sNumL + 'wf scratchpad.ritmtimetowait: value in SECONDS to pass to waitFor block: ' + workflow.scratchpad.ritmtimetowait, 'wf:StaffLeaver'); //---SET SCRATCHPAD--TIMER 2----------------------------- if (!bDateInPast) { var i25days=parseInt(gs.getProperty('he.ritm.leaverenddate.finaltaskwaitduration'));//--normally 2160000, which is 25 days; var ifinalTaskStart= parseInt(workflow.scratchpad.ritmtimetowait)+i25days; gs.log(current.number + ': FINAL TASK wait time in SECONDS: ' + ifinalTaskStart, 'wf:StaffLeaver'); workflow.scratchpad.ritmtimetowait2 = ifinalTaskStart; }else{ var dateDiffPast=(parseInt(gs.dateDiff(dEndDate, dateNow, true))); if (dateDiffPast>2160000){//--end date was set to >25 days ago gs.log(sNumL + 'end date>25 days ago', 'wf:StaffLeaver'); workflow.scratchpad.ritmtimetowait2=0; }else{ //--add 25 days to the past date: dEndDate.addDays('25'); dEndDate.addSeconds('61200');//--bring to 18:00 on the future day var itimer2WaitTime=gs.dateDiff(DateTimeNow_final, dEndDate, true) ; workflow.scratchpad.ritmtimetowait2 =itimer2WaitTime; } }


Popular posts from this blog

GlideRecord setValue

Running transform maps asynchronously

Post a command to the ECC queue for the mid server to initiate a powershell file copy