Set an Incident field as true or false via business rule

Reddy
Kilo Sage

Hello,

 

I have a field named 'Non Business' on the Incident table, and I am trying to set it to true when an Incident is created between 5 PM to 8 AM using a business rule. However, it is not working as expected.

Reddy_0-1703011776768.png

 

 

(function executeRule(current, previous /*null when async*/) {
    gs.info("Script is executing. Current time: " + new GlideDateTime());

    // Check if incident creation time is after 5 PM and before 8 AM
    var createdTime = new GlideDateTime(current.sys_created_on);
    var fivePM = new GlideDateTime();
    fivePM.setDisplayValue('17:00:00');
    var eightAM = new GlideDateTime();
    eightAM.setDisplayValue('08:00:00');

    // Log the values for debugging
    gs.info("Incident creation time: " + createdTime.getDisplayValueInternal());
    gs.info("5 PM time: " + fivePM.getDisplayValueInternal());
    gs.info("8 AM time: " + eightAM.getDisplayValueInternal());

    if (createdTime.after(fivePM) || createdTime.before(eightAM)) {
        // Set the Non Business field to true
        current.u_non_business = true;
        gs.info("Incident created during non-business hours. Setting u_non_business to true.");
    } else {
        // Set the Non Business field to false
        current.u_non_business = false;
        gs.info("Incident created during business hours. Setting u_non_business to false.");
    }

    gs.info("Script execution completed.");

})(current, previous);

 

 

9 REPLIES 9

Implement this script and also match "when to run".

Umair_Malik_0-1703014353745.png

 

(function executeRule(current, previous /*null when async*/) {
    gs.info("Script is executing. Current time: " + new GlideDateTime());

    // Check if incident creation time is after 5 PM and before 8 AM
    var createdTime = new GlideDateTime(current.sys_created_on);
    var fivePM = new GlideDateTime();
    fivePM.setDisplayValue('17:00:00');
    var eightAM = new GlideDateTime();
    eightAM.setDisplayValue('08:00:00');

    // Log the values for debugging
    gs.info("Incident creation time: " + createdTime.getDisplayValueInternal());
    gs.info("5 PM time: " + fivePM.getDisplayValueInternal());
    gs.info("8 AM time: " + eightAM.getDisplayValueInternal());

    if (createdTime.after(fivePM) || createdTime.before(eightAM)) {
        gs.info("Entering if block");
        // Set the Non Business field to true
        current.u_non_business = true;
        gs.info("Incident created during non-business hours. Setting u_non_business to true.");
    }
    else {
        gs.info("Entering else block")
        // Set the Non Business field to false
        current.u_non_business = false;
        gs.info("Incident created during business hours. Setting u_non_business to false.");
    }

    gs.info("Script execution completed.");

})(current, previous);

Umair_Malik
Tera Contributor

Try setting up the business rule as follow:

Screenshot 2023-12-20 021101.png


Code for "advance" tab:

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

    var createdTime = new GlideDateTime(current.sys_created_on);
    var fivePM = new GlideDateTime();
    fivePM.setDisplayValue('17:00:00');
    var eightAM = new GlideDateTime();
    eightAM.setDisplayValue('08:00:00');
 
    if (createdTime.after(fivePM) || createdTime.before(eightAM)) {
 
        current.u_non_business = true;
    }
    else {
        current.u_non_business = false;
        gs.info("Incident created during business hours. Setting u_non_business to false.");
    }
})(current, previous);

@Umair_MalikNon-business field is set to true irrespective of the created time.

Set "when to run" to "before" and check "Insert" field as in SS below. 

Umair_Malik_0-1703093765817.png


Use This Code instead of the last one:

(function executeRule(current, previous /*null when async*/) {
    // Get the current local time
    var currentTime = new GlideDateTime().getLocalTime().getHourOfDayUTC();
    // Create GlideDateTime objects for 5:00 PM and 8:00 AM in the local time zone
    var fivePM = new GlideDateTime();
    fivePM.setDisplayValue('17:00:00');
    var eightAM = new GlideDateTime();
    eightAM.setDisplayValue('08:00:00');

    // Check if the current time (in UTC) is after 5:00 PM or before 8:00 AM (in UTC)
    var isNonBusinessHours = currentTime >= 17 || currentTime < 8;

    // Set u_non_business based on whether it's non-business hours
    current.u_non_business = isNonBusinessHours;

})(current, previous);

Note: Im using Instance time zone to get and compare the time of the incident created. You can test this BR by changing the time zone of the einstance from: System Admisnistration/ preferences/ display/ timezone.

Mark this as helpful if it works for you.

Sainath N
Mega Sage
Mega Sage

@Reddy : Based on your screenshots, I see that you have configured the "After Insert" business rule, and in your script, you are just assigning the "u_non_business" value and not updating it. (Note: Using current.update() in an After business rule is not advisable as it re-triggers all the logic again.)

 

Make this a "Before Insert" business rule and check it once so that the flag also gets updated at the same time during insert.

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.