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

Hi @Ravi Gaurav 

 

I was not aware about this blackbox.ai

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

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

Yes @Dr Atul G- LNG  chatGpt is now outdated we have lot of important AI tools that really helps people . But here in community most answers are copied from chatGPT which is ok only when people understand the concept .. 

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


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/

Thanks, buddy for the update.

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

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

Runjay Patel
Giga Sage

Hi @Hemamani Prabha ,

 

Recommended approach for this would writing flow designer as it require less code. You can use wait for condition to calculate 1 business day=24 hours rest you just need to configure it.

 

The drawback of writing a schedule job that you have to run in every minute to check where any change request fall under that criteria or not which will impact the performance.

 

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

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

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

The community now supports multiple accepted solutions, so you can accept more than one answer.