The CreatorCon Call for Content is officially open! Get started here.

Business rule to check and validate timeframe

Fabricio4
Mega Sage

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.

5 REPLIES 5

Mohith Devatte
Tera Sage
Tera Sage

Hey,

can you please try with this code?

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);

}

 

Please accept the solution if it solves the issue

Thanks

Allen Andreas
Administrator
Administrator

Hi,

"gr" isn't associated to a GlideRecord object until after you execute the query. So your lines where you're creating the start_check and end_check JavaScript variables, don't actually result to anything. So those would need to come after the if/while next section. Please review GlideRecord formatting and also consider reviewing GlideDateTime API for subtracting, etc. to figure out how to compare dates, etc.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi!

Tried that, now the code look like this:

var start = new GlideDateTime ((current.planned_start_date).getNumericValue());
var end = new GlideDateTime ((current.planned_end_date).getNumericValue());
    
var gr = new GlideRecord ('change_task');
    gr.query();
    var start_check = new GlideDateTime ((gr.planned_start_date).getNumericValue());
    var end_check = new GlideDateTime ((gr.planned_end_date).getNumericValue());

    while (gr.next()){
        if ((start.onOrAfter(start_check)) && (start.onOrBefore(end_check))){
            gs.addErrorMessage('CONFLITO!!!');    
            current.setAbortAction(true);
        }
    }

 

Still doesn't work, but it always triggers the if condition even if there's no conflict. Maybe it's related to the fact that some records have the dates empty, so I'd have to filter to not empty. Also, the message is displayed multiple times, which I assume is because I'm not applying the while statement correctly.

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi  Fabricio,

Was able to execute the following script in my instance. I needed to check if there is a value entered in planned_start_date and planned_end_date

if (current.planned_start_date != '' && current.planned_end_date != '') {
    var gdtStart = new GlideDateTime(current.planned_start_date);
    var gdtEnd = new GlideDateTime(current.planned_end_date);

    var gr = new GlideRecord('change_task');
    gr.query();
    while (gr.next()) {
        if (gr.planned_start_date != '' && gr.planned_end_date != '') { // check if there is a value
            var gdtPlannedStart = new GlideDateTime(gr.planned_start_date);
            var gdtPlannedEnd = new GlideDateTime(gr.planned_end_date);
            if (gdtStart.onOrAfter(gdtPlannedStart) && gdtEnd.onOrBefore(gdtPlannedEnd)) {
                gs.info('start:' + gr.planned_start_date + ' end:' + gr.planned_end_date + ' number:' + gr.number);
                gs.addErrorMessage('message');
                current.setAbortAction(true);
            }
        }
    }
}