Business Rule with manipulation on Date/Time fields

Alon Grod
Tera Expert

I have 2 fields on the change_request table:
1. u_starting_time Date/Time
2. u_expected_time Date/Time


I need to write on insert BR on the change_request table that checks:

1.a. if u_starting_time < Thursday at 09:00 on the exact same week like the current week (12/05/2024 13:00 < 16/05/2024 09:00), Then check:
    1.b. u_expected_time > Monday of next week at 14:00 (20/5/2024)

Else (if u starting_time > Thursday at 09:00 on the exact same week like the current week (For example,        17/05/2024 16:00 > 16/05/2024 09:00), Then check:

2.a. u_expected_time > Monday after two weeks at 14:00 (27/5/2024 14:00)

How can I achieve that?

3 REPLIES 3

Amitoj Wadhera
Kilo Sage

Hi @Alon Grod ,

 

Please find below script for your requirement:

(function executeRule(current, previous /*null when async*/) {

    var currentDate = new GlideDate();
    var currentTime = new GlideDateTime();

    var dayOfWeek = currentTime.getDayOfWeek();
    var currentWeek = gs.dateNumeric(currentDate);

    var thursdayThisWeek = new GlideDateTime();
    thursdayThisWeek.setDayOfWeek(4);
    thursdayThisWeek.setWeekOfYear(currentWeek);
    thursdayThisWeek.setHourOfDay(9);
    thursdayThisWeek.setMinutes(0);
    thursdayThisWeek.setSeconds(0);

    var mondayNextWeek = new GlideDateTime();
    mondayNextWeek.setDayOfWeek(1);
    mondayNextWeek.setWeekOfYear(currentWeek + 1);
    mondayNextWeek.setHourOfDay(14);
    mondayNextWeek.setMinutes(0);
    mondayNextWeek.setSeconds(0);

    var mondayAfterTwoWeeks = new GlideDateTime();
    mondayAfterTwoWeeks.setDayOfWeek(1);
    mondayAfterTwoWeeks.setWeekOfYear(currentWeek + 2);
    mondayAfterTwoWeeks.setHourOfDay(14);
    mondayAfterTwoWeeks.setMinutes(0);
    mondayAfterTwoWeeks.setSeconds(0);

    var startingTime = new GlideDateTime(current.u_starting_time.getDisplayValue());
    var expectedTime = new GlideDateTime(current.u_expected_time.getDisplayValue());

    if (startingTime.before(thursdayThisWeek)) {
        if (expectedTime.compareTo(mondayNextWeek) <= 0) {
        } else {
            gs.addErrorMessage("Expected time should be after Monday of next week at 14:00");
        }
    } else {
        if (expectedTime.compareTo(mondayAfterTwoWeeks) <= 0) {
        } else {
            gs.addErrorMessage("Expected time should be after Monday after two weeks at 14:00");
        }
    }
})(current, previous);

 

If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.

 

Thanks,

Amitoj Wadhera

Community Alums
Not applicable

Hi @Alon Grod ,

 

Here you can make use of GlideDateTime Class to achieve this.

Please find the below script which should help in getting your requirement done.

(function executeRule(current, previous /*null when async*/) {

    // function to get the specified weekday and time of a given week
    function getWeekdayTime(date, dayOfWeek, time) {
        var weekDate = new GlideDateTime(date);
        weekDate.setDayOfWeek(dayOfWeek);  // Set to required day of week
        weekDate.setHourOfDayUTC(parseInt(time.split(":")[0]));  // Set hour from time
        weekDate.setMinuteUTC(parseInt(time.split(":")[1]));  // Set minute from time
        return weekDate;
    }

    // Get current date and time
    var now = new GlideDateTime();

    // Calculate Thursday 09:00 of the current week
    var thursdayThisWeek = getWeekdayTime(now, 5, "09:00");

    // Calculate Monday 14:00 of the next week
    var nextMonday = getWeekdayTime(now, 2, "14:00");
    nextMonday.addDays(7); // Add 7 days to get next week's Monday

    // Calculate Monday 14:00 after two weeks
    var mondayAfterTwoWeeks = getWeekdayTime(now, 2, "14:00");
    mondayAfterTwoWeeks.addDays(14); // Add 14 days to get Monday after two weeks

    // Retrieve the values from the form
    var startingTime = new GlideDateTime(current.u_starting_time);
    var expectedTime = new GlideDateTime(current.u_expected_time);

    if (startingTime.before(thursdayThisWeek)) {
        // Check if expectedTime is greater than next Monday 14:00
        if (expectedTime.after(nextMonday)) {
            gs.addInfoMessage("Time conditions are met for the first scenario.");
        } else {
            gs.addErrorMessage("Expected time must be later than Monday of next week at 14:00.");
            current.setAbortAction(true);
        }
    } else {
        // Check if expectedTime is greater than Monday after two weeks at 14:00
        if (expectedTime.after(mondayAfterTwoWeeks)) {
            gs.addInfoMessage("Time conditions are met for the second scenario.");
        } else {
            gs.addErrorMessage("Expected time must be later than Monday after two weeks at 14:00.");
            current.setAbortAction(true);
        }
    }

})(current, previous);

 

Also if you can share your BR if already created we can take a look in that as well.

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar

Krushna R Birla
Kilo Sage

Hi @Alon Grod 

 

Add below script in your business rule to fulfil your requirement:

 

(function executeRule(current, previous /*null when async*/ ) {

var now = new GlideDateTime();
var dayThisWeek = now.getDayOfWeek();
var daysUntilThursday = (4 - dayThisWeek + 7) % 7;
now.addDaysUTC(daysUntilThursday);
var nextThursday = new GlideDateTime(now);
var thursdayThisWeek = new GlideDateTime(nextThursday.getDisplayValue().split(' ')[0] + " 09:00:00");
var newNow = new GlideDateTime();

var comingMonday = (1 - dayThisWeek + 7) % 7;
if (comingMonday === 1) {
var nextMondayy = new GlideDateTime(newNow);
nextMondayy.addDays(comingMonday);
var nextMondayAfterNext = new GlideDateTime(nextMondayy);
nextMondayAfterNext.addDays(7);
var mondayComingWeek = new GlideDateTime(nextMondayAfterNext.getDisplayValue().split(' ')[0] + " 14:00:00");
} else {
var nextMon = new GlideDateTime(newNow);
nextMon.addDays(comingMonday);
var nextMonAfterNext = new GlideDateTime(nextMon);
var mondayComingWeek = new GlideDateTime(nextMonAfterNext.getDisplayValue().split(' ')[0] + " 14:00:00");
}

var thisMonday = new GlideDateTime(mondayComingWeek);
var mondayAfterTwoWeeks = thisMonday.addDays(7);
var thisMondayFinal = new GlideDateTime(mondayComingWeek);
var startingTime = new GlideDateTime(current.u_starting_time);
var expectedTime = new GlideDateTime(current.u_expected_time);

if (startingTime.before(thursdayThisWeek)) {
// Check if expectedTime is greater than next Monday 14:00
if (expectedTime.after(thisMondayFinal)) {
gs.addInfoMessage("Time conditions are met for the first scenario.");
} else {
gs.addErrorMessage("Expected time must be later than Monday of next week at 14:00.");
current.setAbortAction(true);
}
} else {
// Check if expectedTime is greater than Monday after two weeks at 14:00
if (expectedTime.after(mondayAfterTwoWeeks)) {
gs.addInfoMessage("Time conditions are met for the second scenario.");
} else {
gs.addErrorMessage("Expected time must be later than Monday after two weeks at 14:00.");
current.setAbortAction(true);
}
}
})(current, previous);
 

This will definitely helps you to resolved your issue. Let me know in case you need to understand the flow or you can DM on LinkedIn.

 

If this solution resolves your query, kindly mark it as the accepted solution and give it a thumbs up.

 

Best Regards,
Krushna Birla