The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Does the current time fall within a schedule?

will_smith
Mega Guru

The ask is to have the record producer send incidents to a different assignment group for after hours work in a different timezone. To begin work on this, I have set up a schedule [cmn_schedule] and am wanting to create a script that will run against a record producer to determine if it's after hours and update the assignment group accordingly.

Is there a way I can query the schedules table and see if the current date/time falls within the schedule?

Thanks for all your help! I'm also open to other ways to accomplish this, but I figured schedules was a good place to start since it would handle time zones nicely.

1 ACCEPTED SOLUTION

Hi William,



Upon closer inspection, your schedRec.get() will likely not work. With one argument, this method is looking for a sys_id. You gave it a name. You should change it to:



if (!schedRec.get('name', 'Overnight Hours (Sogeti RIM)'))


    return 'no';



As for the rest, I recomment testing it in scripts background to ensure your functionality before deploying it in a business rule, workflow script activity, etc.



This should help (I just made it last night because this comes up so often.)


Faster Server Side Script Development and Test




View solution in original post

11 REPLIES 11

FYI, the timezone chooser is not on the top menu bar any longer. It is under the gear menu (UI16 shown)



find_real_file.png



I just tested this on UI16. The default roles are admin,itil in the system property. I impersonated Joe Employee (no roles) and there was no selector. I impersonated Bow Ruggeri (itil) and he had the ability to see and select a timezone.


I am having some issues with the provided script. new GlideDateTime(); is outputting UTC time, but I am looking for EST. Can you help me understand what I'm missing?



*** Script: 2016-06-20 13:34:48


*** Script: Not in schedule - Route to Command Center



-- Screenshot of schedule --


find_real_file.png




answer = ifScript();



function ifScript() {


      var schedRec = new GlideRecord('cmn_schedule');               // Query table for schedule


      schedRec.get('Overnight Hours (Sogeti RIM)');       // Get the schedule name


      var sched = new GlideSchedule(schedRec.sys_id);               // Create variable schedule


    var schedTime = new GlideDateTime();                       // Create variable with current date/time



      // If date/time during business hours Eastern time zone


      if (sched.isInSchedule(schedTime,'US/Eastern')) {



              // Route task to Data Center


              //gs.addInfoMessage('In schedule - Route to Data Center');


  gs.print(schedTime);


  gs.print('In schedule - Route to Data Center');


              return 'yes';


      }


      // If datetime after business hours Pacific time zone


      else {



              // Router task to Command Center


              //gs.addInfoMessage('Not in schedule - Route to Command Center');


  gs.print(schedTime);


  gs.print('Not in schedule - Route to Command Center');


              return 'no';


      }


}


I threw this together as a quick example how you can get the timezone offset and apply it.



var gdt = new GlideDateTime();


gdt.getLocalTime();


var offset=gdt.getTZOffset() / 1000;


gs.print('offset=' + offset);


gdt.addSeconds(offset);


gs.print(gdt.getValue());


Thanks!



Does the schedule and the associated script above make sense?



Will the script return 'yes' when it's between 21:00 - 06:00 EST?


Hi William,



Upon closer inspection, your schedRec.get() will likely not work. With one argument, this method is looking for a sys_id. You gave it a name. You should change it to:



if (!schedRec.get('name', 'Overnight Hours (Sogeti RIM)'))


    return 'no';



As for the rest, I recomment testing it in scripts background to ensure your functionality before deploying it in a business rule, workflow script activity, etc.



This should help (I just made it last night because this comes up so often.)


Faster Server Side Script Development and Test