Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Danish Bhairag2
Tera Sage

Hi @Reddy ,

 

As per ur ask I think the BR should only run after insert n u can try below code if it works or not.(untested)

 

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

    // Get the current date and time

    var currentDate = new GlideDateTime();

 

    // Set the time boundaries for Non-Business hours (5 PM to 8 AM)

    var nonBusinessStart = new GlideDateTime();

    nonBusinessStart.setDisplayValue('17:00:00'); // 5 PM

    var nonBusinessEnd = new GlideDateTime();

    nonBusinessEnd.setDisplayValue('08:00:00'); // 8 AM next day

 

    // Check if the current date and time is within Non-Business hours

    if (currentDate.compareTo(nonBusinessStart) >= 0 || currentDate.compareTo(nonBusinessEnd) < 0) {

        // Set 'Non Business' field to true

        current.non_business = true;

    }

})(current, previous);

 

Thanks,

Danish

 

Hello @Danish Bhairag2 

 

Thank you for your reply. It is not working.

Umair_Malik
Tera Contributor

Can you make it clear if you want this BR to work from 5pm-8am of from 8am-5pm?
Also in your scenerio you dont need the BR to run on update. It should work fine on Insert only.

@Umair_Malik

BR should only work from 5:00 PM to 8:00 AM and it should only work on insert.