How to stop running business rule on specific period of time

Supriya25
Tera Guru

Hi Team,

 

we have two business(insert, update operation) , which plays major role in daily activity.

This two Business rules should work only between morning 6AM-afternoon 3PM.  after 3PM/before 6AM if any insertion/updating happens on record these BR's should not respond..

 

Every day we can't deactivate/Activate , so How can we achieve it ? any possibility ?

16 REPLIES 16

Sorry about that.  Let's break it up a bit:

var nowDT = new GlideDateTime(new GlideDateTime().getDisplayValue());
var schedule = new GlideSchedule("090eecae0a0a0b260077e1dfa71da828");
schedule.isInSchedule(nowDT);

That works for me in both Global and a custom scope.  nowDT will be a GlideDateTime object with the current date/time/timezone and it can be used with isInSchedule.

 instance is US/Eastern TimeZone

schedule 
090eecae0a0a0b260077e1dfa71da828
time zone : US/Eastern
08:00:00 to 17:00:00
until : Empty

 

var nowDT = new GlideDateTime(new GlideDateTime().getDisplayValue());
gs.addInfoMessage(nowDT);
var schedule = new GlideSchedule("090eecae0a0a0b260077e1dfa71da828");
if(schedule.isInSchedule(nowDT)){
gs.addInfoMessage("isInSchedule");
}else{
gs.addInfoMessage("Not isInSchedule");
}

Result : 
2024-04-18 15:44:09
isInSchedule

 

 



2024-04-18 19:49:19 is Out of schedule, but still it is showing "isInSchedule"

 

 

var nowDT = new GlideDateTime(new GlideDateTime("2024-04-18 19:49:19"));
gs.addInfoMessage(nowDT);
var schedule = new GlideSchedule("090eecae0a0a0b260077e1dfa71da828");
if(schedule.isInSchedule(nowDT)){
gs.addInfoMessage("isInSchedule");
}else{
gs.addInfoMessage("Not isInSchedule");
}

Result : 
2024-04-18 19:49:19
isInSchedule

 

 


 

OK, wait, we are overcomplicating this.

 

var schedule = new GlideSchedule("090eecae0a0a0b260077e1dfa71da828");  //8-5 weekdays excluding holidays

var nowDT = new GlideDateTime();  //creates new current date/time in UTC
gs.addInfoMessage("nowDT UTC = " + nowDT);
gs.addInfoMessage("nowDT.getDisplayValue() = " + nowDT.getDisplayValue());  //will show in Eastern time

if (schedule.isInSchedule(nowDT)){
  gs.addInfoMessage("isInSchedule");
} else {
  gs.addInfoMessage("NOT isInSchedule");
}

 

Result:

JimCoyne_0-1713471384579.png

I'm in the Eastern time zone as well.  If I change the Schedule to be 8-4, I get the following result:

JimCoyne_1-1713471645332.png

 

I understand that we are doing it complicated, but not sure how to fix this one.

Kindly let me know here what kind of details need for you.
 my Instance always runs in EST time-zone and my schedules also got created with EST time-zone. 

then how come this system always returning UTC value  even after using .getDisplayValue(); 

Even if we notice your Result for 1st result and 2nd result just 5min difference has come.

20:15:57 - 20:20:20
I believe your scheduled (8AM-16PM) , But 20PM means it's out of Scheduled right ?

 

The first result showed the time in both UTC at 20:15:57 and Eastern (using getDisplayValue) at 16:15:57, which was in the 8-5 Schedule.

 

I manually modified the 8-5 Schedule to be 8-4 for the 2nd test instead of waiting an hour and the 2nd result shows the time as 20:20:20 in UTC and 16:20:20 Eastern (again with getDisplayValue which converts to the current user's timezone) which was no longer within the Schedule.

 

So in summary:

- simply create the date/time object with var nowDT = new GlideDateTime()

- the date is stored in UTC

- using isInSchedule will validate whether the UTC time is within the Schedule time, which is in Eastern

- conversions are automatically made for timezones when comparing. 20:15:57 UTC is the same point in time as 16:15:57 Eastern and would be within the 8-5 Schedule

 

In your example, when you set nowDT to "2024-04-18 19:49:19", that is "2024-04-18 19:49:19 UTC" and NOT Eastern because there is no timezone specified so the default timezone of UTC is used/assumed.  That time equates to "2024-04-18 15:49:19 Eastern" when using isInSchedule so it does in fact fall within the Schedule.

 

You just need to realize that GlideDateTime objects are going to be using UTC as the timezone by default.