Auto Closure of Change Requests

Ralton Stewart
Tera Guru

I am looking to implement a solution to Auto Close Change Requests that are older than 6months any help appreciated/

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@Ralton Stewart 

you can use scheduled job for this

something like this in daily scheduled job but please enhance

(function executeScheduledJob() {
    var sixMonthsAgo = new GlideDateTime();
    sixMonthsAgo.addMonthsUTC(-6);

    var grChangeRequest = new GlideRecord('change_request');
    grChangeRequest.addQuery('sys_created_on', '<', sixMonthsAgo);
    grChangeRequest.addActiveQuery();
    grChangeRequest.query();
    while (grChangeRequest.next()) {
        grChangeRequest.state = '3';
        grChangeRequest.active = 'false';
        grChangeRequest.close_code = 'Closed by Automation';
        grChangeRequest.close_notes = 'Automatically closed as it is older than six months.';
        grChangeRequest.update();
    }
})();

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

View solution in original post

Abhijeet_Pawar
Tera Guru

Hello @Ralton Stewart ,

You can achieve this requirement using Flow Designer without writing any script by leveraging a scheduled-based flow.

Steps to Implement:

  1. Lookup Records: Retrieve all Change Requests where the state is not closed and the creation date is older than 6 months.

  2. For Each Loop: Iterate through the retrieved records and update their state to Closed. You can also set the Close Notes and Close Code, if required.

This approach ensures that old Change Requests are automatically closed without manual intervention.

I hope this helps resolve your query.

Thank you!

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Ralton Stewart 

you can use scheduled job for this

something like this in daily scheduled job but please enhance

(function executeScheduledJob() {
    var sixMonthsAgo = new GlideDateTime();
    sixMonthsAgo.addMonthsUTC(-6);

    var grChangeRequest = new GlideRecord('change_request');
    grChangeRequest.addQuery('sys_created_on', '<', sixMonthsAgo);
    grChangeRequest.addActiveQuery();
    grChangeRequest.query();
    while (grChangeRequest.next()) {
        grChangeRequest.state = '3';
        grChangeRequest.active = 'false';
        grChangeRequest.close_code = 'Closed by Automation';
        grChangeRequest.close_notes = 'Automatically closed as it is older than six months.';
        grChangeRequest.update();
    }
})();

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

Abhijeet_Pawar
Tera Guru

Hello @Ralton Stewart ,

You can achieve this requirement using Flow Designer without writing any script by leveraging a scheduled-based flow.

Steps to Implement:

  1. Lookup Records: Retrieve all Change Requests where the state is not closed and the creation date is older than 6 months.

  2. For Each Loop: Iterate through the retrieved records and update their state to Closed. You can also set the Close Notes and Close Code, if required.

This approach ensures that old Change Requests are automatically closed without manual intervention.

I hope this helps resolve your query.

Thank you!