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

Ankur Bawiskar
Tera Patron
Tera Patron

@Hemamani Prabha 

so what did you try so far and what didn't work?

Since you require 1 business day, you will have to use GlideSchedule class

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