Skip to main content

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

=============

script:

//--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.now(); //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; } }

Comments

Popular posts from this blog

ServiceNow check for null or nil or empty (or not)

Haven't tested these all recently within global/local scopes, so feel free to have a play! option 1 use an encoded query embedded in the GlideRecord , e.g.  var grProf = new GlideRecord ( 'x_cls_clear_skye_i_profile' ); grProf . addQuery ( 'status=1^ owner=NULL ' ); grProf . query (); even better use the glideRecord  addNotNullQuery or addNullQuery option 2 JSUtil.nil / notNil (this might be the most powerful. See this link ) example: if ( current . operation () == 'insert' && JSUtil . notNil ( current . parent ) && ! current . work_effort . nil ())  option 3 there might be times when you need to get inside the GlideRecord and perform the check there, for example if the code goes down 2 optional routes depending on null / not null can use gs.nil : var grAppr = new GlideRecord ( 'sysapproval_approver' ); var grUser = new GlideRecord ( 'sys_user' ); if ( grUser . get ( 'sys_id' , current . approver )){...

Code a pause/wait - gs.sleep or gs.wait alternative, pause script for specified seconds (timer)

Code a pause/wait - gs.sleep / gs.wait alternative, pause script for specified seconds (timer)  e.g. 10 seconds: do_sleep ( 10000 ); function do_sleep ( milliseconds ) { var start = new Date (). getTime (); for ( var i = 0 ; i < 1e7 ; i ++) { if (( new Date (). getTime () - start ) > milliseconds ){ gs . print ( 'waking up!' ); break ; } } }