Scheduled job to change the state

Hemamani Prabha
Tera Contributor

Hello all,

 

I have a requirement as such,

If 1 business day (min 24 hours) has passed since CHG Planned end date And CHG is in “Scheduled” state Any CTASKs (of CHG) that are in an active state (i.e. Not Closed Complete) need to be automatically changed to “Closed Failed - Untimely Closure

I need not trigger an email but change the state of the CR based on the condition.

Kindly help me with the scheduled job and script for this if anybody has implemented something similar.

 

Thanks,

Hema

1 ACCEPTED SOLUTION

Juhi Poddar
Kilo Patron

Hello @Hemamani Prabha 

To meet your requirement, you can create a scheduled job that runs daily.

Schedule job Script:

    var chgGR = new GlideRecord('change_request'); // Change Request table
    var ctasksGR = new GlideRecord('change_task'); // Change Task table

    // Set conditions to find 'Scheduled' changes with a planned end date that has passed
    var startDate = new GlideDateTime();
    startDate.addDaysUTC(-1); // This will ensure that we check for 24 hours ago (1 business day)

    chgGR.addQuery('state', -2); // Change state is 'Scheduled'
    chgGR.addQuery('end_date', '<', startDate); // Planned End date passed (before 24 hours)
    chgGR.query();

    while (chgGR.next()) {
        // Query Change Tasks for this Change Request
        ctasksGR.addQuery('change_request', chgGR.sys_id); // Reference to the Change Request
        ctasksGR.addQuery('state', 'IN', '1,2,5'); // open(1), in progress(2), pending(5)
        ctasksGR.query();

        while (ctasksGR.next()) {
            // Update each active CTASK to cancelled'
            ctasksGR.setValue('state', 4); //cancelled(4)
            ctasksGR.update();
            gs.log('CTASK ' + ctasksGR.number + ' for Change ' + chgGR.number + ' is marked as cancelled');
        }
    }

Note:

  • I have used the out-of-the-box (OOTB) state values in this script. Please modify them if your instance has customized state values.
  • The script checks for Change Requests in the "Scheduled" state that have passed their planned end date (over 24 hours ago).
  • Any active CTASKs (in states 1, 2, or 5) related to these Change Requests will be updated to "Cancelled" (state 4).

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

View solution in original post

10 REPLIES 10

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Hemamani Prabha 

 

You can try Flow Designer which is low-code.

*************************************************************************************************************
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]

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

Juhi Poddar
Kilo Patron

Hello @Hemamani Prabha 

To meet your requirement, you can create a scheduled job that runs daily.

Schedule job Script:

    var chgGR = new GlideRecord('change_request'); // Change Request table
    var ctasksGR = new GlideRecord('change_task'); // Change Task table

    // Set conditions to find 'Scheduled' changes with a planned end date that has passed
    var startDate = new GlideDateTime();
    startDate.addDaysUTC(-1); // This will ensure that we check for 24 hours ago (1 business day)

    chgGR.addQuery('state', -2); // Change state is 'Scheduled'
    chgGR.addQuery('end_date', '<', startDate); // Planned End date passed (before 24 hours)
    chgGR.query();

    while (chgGR.next()) {
        // Query Change Tasks for this Change Request
        ctasksGR.addQuery('change_request', chgGR.sys_id); // Reference to the Change Request
        ctasksGR.addQuery('state', 'IN', '1,2,5'); // open(1), in progress(2), pending(5)
        ctasksGR.query();

        while (ctasksGR.next()) {
            // Update each active CTASK to cancelled'
            ctasksGR.setValue('state', 4); //cancelled(4)
            ctasksGR.update();
            gs.log('CTASK ' + ctasksGR.number + ' for Change ' + chgGR.number + ' is marked as cancelled');
        }
    }

Note:

  • I have used the out-of-the-box (OOTB) state values in this script. Please modify them if your instance has customized state values.
  • The script checks for Change Requests in the "Scheduled" state that have passed their planned end date (over 24 hours ago).
  • Any active CTASKs (in states 1, 2, or 5) related to these Change Requests will be updated to "Cancelled" (state 4).

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

Thank you so much for your help @Juhi Poddar This code worked for me.. 🙂

Ravi Gaurav
Giga Sage
Giga Sage

Hi @Hemamani Prabha 
I agreed with @Dr Atul G- LNG  to go with Flow Designer but if you  need code .. you can try with Available AI sources like Blackbox.ai.. beacuse everyone here who will ping you the code will be from AI sources only.. So better try that..

Example :- blackbox.ai gives the  below result:-

Steps to Create the Scheduled Job

  1. Navigate to Scheduled Jobs:

    • Go to System Definition > Scheduled Jobs in the ServiceNow instance.
  2. Create a New Scheduled Job:

    • Click New and fill out the form:
      • Name: Close CTASKs for Untimely Closure
      • Run: Daily
      • Run Time: Choose a time outside of business hours (e.g., 2:00 AM)
      • Active: Checked
  3. Add the Script to the Job: Use the following script:

    (function executeScheduleJob() {
    // GlideDateTime for 1 business day (24 hours ago)
    var now = new GlideDateTime();
    now.addDaysUTC(-1); // Subtract 1 day (24 hours)

    // Query CHG records in "Scheduled" state and where planned_end_date is in the past
    var chgGr = new GlideRecord('change_request');
    chgGr.addQuery('state', 'Scheduled'); // Change is in "Scheduled" state
    chgGr.addQuery('planned_end_date', '<=', now); // Planned end date is at least 24 hours in the past
    chgGr.query();

    while (chgGr.next()) {
    // Query active CTASKs related to this CHG
    var ctaskGr = new GlideRecord('change_task');
    ctaskGr.addQuery('change_request', chgGr.sys_id); // Link to parent CHG
    ctaskGr.addQuery('state', '!=', 'Closed Complete'); // Not Closed Complete
    ctaskGr.addQuery('active', true); // Task is still active
    ctaskGr.query();

    while (ctaskGr.next()) {
    // Update state to "Closed Failed - Untimely Closure"
    ctaskGr.state = 'Closed Failed - Untimely Closure';
    ctaskGr.update();
    }
    }
    })();

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/