How to call a schedule from a business rule to a scheduled job

Toon6543
Tera Expert

Hi there, I was looking for a solution. I have a script I am running within a scheduled job that triggers a query to a completed task. If the task still needs to be completed and the due date for the task has come and gone, it needs to send an email notification stating the task is past due.

 

What Im running into is that within ServiceNow, we have our business hours and days setup, and I want the script to run on those specific days. Is there a way to do this? Right now, I have it running with

 

//Return 'true' to run the job
var answer = false;

//Get the day of week. 1=Monday, 7=Sunday
var now = new GlideDateTime();

//Run only on weekdays
if(now.getDayOfWeek() < 6){

 

I'd like it to check our business rule for business days, times, and hours instad. Thanks!

1 ACCEPTED SOLUTION

Thank you for the response shortly after I posted this thread I figured out how to do it. Here was my solution! 

 

Thanks for your guidance

var gr = new GlideRecord('table_where_record_is');
var now = new GlideDateTime();
var sched = new GlideSchedule('normal_schedule_sys_id'); // replace with your business schedule sys_id
var holiday = new GlideSchedule('your_holiday_schedule_sys_id'); // replace with your holiday schedule sys_id

gs.log("UniqueIdentifier - Checking if current day and time falls within the business schedule and is a weekday, and not a holiday.");

// Get the day of week. 1=Monday, 7= Sunday
// Check if current time falls within the business schedule (sched) and does not fall within the holiday schedule (holiday)
if (now.getDayOfWeekLocalTime() >= 1 && now.getDayOfWeekLocalTime() <= 5 && sched.isInSchedule(now) && !holiday.isInSchedule(now)) {
    gs.log("UniqueIdentifier - Current day and time falls within the business schedule and is a weekday, and not a holiday. Proceeding to query onboarding tasks.");
    
    gr.addEncodedQuery('due_dateNOTISEMPTY^due_date<=' + now.getValue() + '^active=true'); 
    gr.query();

    gs.log("UniqueIdentifier - Query executed. Number of fetched records: " + gr.getRowCount());

    while (gr.next()){
        gs.log("UniqueIdentifier - Queueing 'duedate_pastdue_onboarding' event for task with sys_id: " + gr.getUniqueValue());
        gs.eventQueue('duedate_pastdue_onboarding', gr, gr.assigned_to,gr.due_date);
    }
} else {
    gs.log("UniqueIdentifier - Current day and time does not fall within the business schedule or is not a weekday, or it is a holiday. Exiting script.");
}

 

View solution in original post

3 REPLIES 3

Ratnakar7
Mega Sage
Mega Sage

Hi @Toon6543 ,

 

Here's the general approach:

  1. Create a Business Rule:

    • Create a new Business Rule on the table where the completed tasks are stored (e.g., Task table).
    • Configure the Business Rule to run on the "Update" event.
    • In the Business Rule script, check if the task is completed, and if the due date has passed. Also, check if the current date and time are within the business hours and days criteria. If all conditions are met, call the script to send the email notification.
  2. Create a Scheduled Job:

    • Go to System Scheduler > Scheduled Jobs.
    • Click on "New" to create a new Scheduled Job.
    • Set the "Run" field to "Every hour" or the desired interval.
    • In the script field, call the Business Rule you created in step 1.

Here's an example of how the Scheduled Job script might look like:

 

// Call the Business Rule to check and send email notifications for past due tasks
var gr = new GlideRecord('task'); // Replace 'task' with the table name where the completed tasks are stored
gr.addQuery('state', 'completed');
gr.addQuery('due_date', '<', gs.nowDateTime()); // Check if the due date has passed
gr.query();
while (gr.next()) {
    // Check if the current date and time are within business hours and days criteria
    if (checkBusinessHoursAndDays()) {
        // Call the Business Rule to send email notification
        gs.eventQueue('task.past_due', gr, gr.assigned_to.getDisplayValue(), gs.getUserID());
    }
}

 

In the above example, replace 'task' with the actual table name where the completed tasks are stored. The checkBusinessHoursAndDays() function is a custom function that you need to implement to check if the current date and time are within the business hours and days criteria.

 

Thanks,

Ratnakar

Thank you for the response shortly after I posted this thread I figured out how to do it. Here was my solution! 

 

Thanks for your guidance

var gr = new GlideRecord('table_where_record_is');
var now = new GlideDateTime();
var sched = new GlideSchedule('normal_schedule_sys_id'); // replace with your business schedule sys_id
var holiday = new GlideSchedule('your_holiday_schedule_sys_id'); // replace with your holiday schedule sys_id

gs.log("UniqueIdentifier - Checking if current day and time falls within the business schedule and is a weekday, and not a holiday.");

// Get the day of week. 1=Monday, 7= Sunday
// Check if current time falls within the business schedule (sched) and does not fall within the holiday schedule (holiday)
if (now.getDayOfWeekLocalTime() >= 1 && now.getDayOfWeekLocalTime() <= 5 && sched.isInSchedule(now) && !holiday.isInSchedule(now)) {
    gs.log("UniqueIdentifier - Current day and time falls within the business schedule and is a weekday, and not a holiday. Proceeding to query onboarding tasks.");
    
    gr.addEncodedQuery('due_dateNOTISEMPTY^due_date<=' + now.getValue() + '^active=true'); 
    gr.query();

    gs.log("UniqueIdentifier - Query executed. Number of fetched records: " + gr.getRowCount());

    while (gr.next()){
        gs.log("UniqueIdentifier - Queueing 'duedate_pastdue_onboarding' event for task with sys_id: " + gr.getUniqueValue());
        gs.eventQueue('duedate_pastdue_onboarding', gr, gr.assigned_to,gr.due_date);
    }
} else {
    gs.log("UniqueIdentifier - Current day and time does not fall within the business schedule or is not a weekday, or it is a holiday. Exiting script.");
}

 

Do you need to give holiday schedule also in the script.? What if I already attached it in main schedule as child schedule? Is it works?