Business rule to check and validate timeframe
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-06-2022 01:07 PM
Hello!
I'm trying to come up with a business rule that checks the table to validate if a group assigned to a new task already has a task within the same timeframe. That is, if the new task start date and end date conflicts with another task assigned to the same group, an alert is displayed and the action aborts. Here's what I have so far (which hasn't worked, I still have not included the group checking, just the time frame):
var start = new GlideDateTime (current.planned_start_date);
var end = new GlideDateTime (current.planned_end_date);
var gr = new GlideRecord ('change_task');
var start_check = new GlideDateTime (gr.planned_start_date);
var end_check = new GlideDateTime (gr.planned_end_date);
gr.query();
while (gr.next())
if ((start.onOrAfter(start_check)) && (start.onOrBefore(end_check))){
gs.addErrorMessage('message');
current.setAbortAction(true);
Now, I know I have to better define the condition, but even at this minimal condition it is not working (the task is created and no message is displayed, even though the dates conflict) and I can't figure it out why. Coding is not my forte.
- Labels:
-
Script Debugger
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2022 11:54 AM
Hi, Hitoshi!
Thanks for the help. One of the issues I was having was that I wasn't filtering empty fields, which your code does. That part works fine.
However, what is not quite working yet is that the message is displayed multiple times, possibly because of how the while statement is being used, since it appears to displayed the message for every record that conflicts with the new record. Any other suggestion?
edit: apparently using "break" solves the issue!