Scheduled job not triggering

Arun_Manoj
Mega Sage

hi,

 

I'm facing an issue with scheduled job, it is not triggering .

 

(function() {

 

    var gr = new GlideRecord('sn_grc_task');

 

    gr.addQuery('active', true);

 

    gr.addQuery('state', 'NOT IN', '3,4,7,9');

 

    gr.addQuery('end_date', '!=', '');

 

    gr.query();

 

    var now = new GlideDateTime();

 

    var currentDate = now.getDate().toString(); // e.g., '2025-08-18'

 

    var sevenDaysBefore = new GlideDate();

 

    sevenDaysBefore.addDays(-7);

 

    var sevenDaysBeforeDate = sevenDaysBefore.toString(); // e.g., '2025-08-11'

 

    var sevenDaysAfter = new GlideDate();

 

    sevenDaysAfter.addDays(7);

 

    var sevenDaysAfterDate = sevenDaysAfter.toString(); // e.g., '2025-08-25'

 

    while (gr.next()) {

 

        var endDateStr = gr.getValue('end_date'); // e.g., '2025-08-18 00:00:00'

 

        var dueDateOnly = endDateStr.split(' ')[0]; // extract '2025-08-18'

 

        if (dueDateOnly == sevenDaysBeforeDate || dueDateOnly == currentDate || dueDateOnly == sevenDaysAfterDate) {

 

            var assignedTo = gr.getValue('assigned_to'); // sys_id of assigned user

 

            gs.eventQueue('sn_grc.due.date.overdue', gr, assignedTo, dueDateOnly);

 

        }

 

    }

 

})();

 

running daily

24 REPLIES 24

@Arun_Manoj 

when you added log in scheduled job did those logs come in system logs?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Arun_Manoj 

 

Did you try to move this in flow designer which is low code and easy to test.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

kaushal_snow
Mega Sage

Hi @Arun_Manoj ,

 

Try adding logs in between..

 

Use gs.info()  or gs.log() for global scope to confirm whether the script is running:

 

Sample>>> gs.info("Scheduled job started at " + new Date());


Check System Logs → System Log → All for these outputs.

 

Additionally, Verify Run As User's Permission, Check that the user specified in the Run as field is still active and has the necessary roles for executing the job.

 

If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.

 

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/

Bhuvan
Kilo Patron

@Arun_Manoj 

 

You are converting date to string and in the while loop trying to compare date fields using string and that could  be the reason for script not working. Make sure you are comparing end date with correct comparison criteria for seven days before, after and current date.

 

Also, make sure you have defined the event registry and names are matching before adding it to the event queue.

 

You can try to use below script [change the date comparison section as per your requirement],

(function() {
    var gr = new GlideRecord('sn_grc_task');
    gr.addQuery('active', true);
    gr.addQuery('state', 'NOT IN', '3,4,7,9');
    gr.addQuery('end_date', '!=', '');
    gr.query();
    var now = new GlideDate(); 
    var sevenDaysBefore = new GlideDate();
    sevenDaysBefore.addDays(-7);
    var sevenDaysAfter = new GlideDate();
    sevenDaysAfter.addDays(7);
    while (gr.next()) 
{
    var endDate = new GlideDateTime(gr.getValue('end_date')).getDate(); 
    if (endDate > sevenDaysBefore || endDate > now || endDate < sevenDaysAfter) 
 {
    var assignedTo = gr.getValue('assigned_to'); // sys_id of assigned user
    gs.eventQueue('sn_grc.due.date.overdue', gr, assignedTo, endDate.toString());
 }
}
})();

Below is a test from script background and I modified the end date comparison criteria and printing 'assigned_to' field of the tasks for testing purposes.

Bhuvan_2-1755753630791.png

Bhuvan_3-1755753719737.png

 

Bhuvan_6-1755753746987.png

If this helped to answer your query, please mark it helpful & accept the solution.

 

Thanks,

Bhuvan

@Bhuvan  ,


I tried executing the script using a scheduled job, but the email wasn't triggered.
However, when I ran the same script from a background script, the email was sent successfully.

Do you know why it works in a background script but not through the scheduled job?