Scheduled Job

MoeG82
Tera Contributor

I am creating a scheduled job to to send out daily emails to the assigned user for incidents not active in 3 days excluding weekends. I have the condition for the exclusion below:

 
var result = true;
var gdt = new GlideDateTime();
var day = gdt.getDayOfWeekLocalTime(); //The day of week value from 1 to 7. Monday equals 1, Sunday equals 7.
if(day==6||day==7)
result =false;
result ;
 
I am stuck on the actual script that I need to write below the condition.
3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@MoeG82 

are you saying you want to check if incident is assigned and still not updated for more than 3 business days?

what's your business requirement here? I am little confused

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Yes, I need to check for incidents assigned but have not been updated in 3 days.

@MoeG82 To build your scheduled job for sending daily email notifications to assigned users of incidents that have not been updated in 3 business days (excluding weekends), you can follow the below sample script:

    var scheduleId = '08a760a4a9fe198100ff1d8b29b9a71c'; // Replace with your actual schedule sys_id from cmn_scheduke other explicitly you will have to define date logic

    var glideSchedule = new GlideSchedule(scheduleId);

    // Get the current date and calculate the date 3 business days ago

    var now = new GlideDateTime();

    var threeBusinessDaysAgo = glideSchedule.subtract(now, 3, 'business');

    // Query for inactive incidents not updated in the last 3 business days

    var gr = new GlideRecord('incident');

    gr.addActiveQuery(false); // Only inactive incidents

    gr.addQuery('sys_updated_on', '<=', threeBusinessDaysAgo);

    gr.addNotNullQuery('assigned_to'); // Ensure it has an assigned user

    gr.query();

    // Trigger the existing notification event for each matching incident

    while (gr.next()) {

        // Trigger the event for the existing notification  gs.eventQueue('incident.reminder.inactive', gr, gr.assigned_to, gr.number);// if not then create a new notification and you can change param1 and 2 as per your requirement 

 gs.info("Notification event triggered for Incident: " + gr.number + " assigned to " + gr.assigned_to.getDisplayValue());

Please test on lower instance first.