How to autoclose change requests in ServiceNow

 

Business Requirement

This business requirement is to automatically close change requests that have been in the Review state for more than a certain number of days (i.e., 14 days).

Solution

Steps:

FIRST AND FOREMOST, ALWAYS CHECK HOW SN HAS IMPLEMENTED OTHER OOTB FUNCTIONALITIES SIMILAR TO YOUR BUSINESS REQUIREMENT!!

So, in our case: have a look at the Autoclose Incidents in the Schedule (sys_trigger) table. God damn it!

Copy & paste that shit and modified according to your needs!

If you cannot find an OOTB implementation similar to your business requirement, then push forward with the following steps:

  • Navigate to > Schedule (sys_trigger) table

Create a new Schedule Job

Give it a name: autoclose changes

Save

  • Navigate to > Business Rules (sys_script) table

Create a new BR

Give it a name: change autoclose

Select Change Request (change_request) table.

Select Advanced field.

  • Navigate to > When to run tab

When: after

  • Navigate to > Advanced tab

Put this script in the script editor:

// This script automatically closes changes that in review state and haven't been updated in the specified // number of days. This number is a property in System Properties.
// To place a comment in the change, uncomment the "gr.comments" line.

autoCloseChanges();

function autoCloseChanges() {

var ps = gs.getProperty('glide.ui.autoclose.change.time');

var pn = parseInt(ps);

var queryTime = new GlideDateTime();

queryTime.addDaysUTC(-pn);

if (pn > 0) {

var gr = new GlideRecord('change_request');

gr.addQuery('state', 0);

gr.addQuery('sys_updated_on', '<', queryTime);

gr.query();

while(gr.next()) {

gr.state = 3;

//  gr.comments = 'Change automatically closed after ' + pn + ' days in the Review state.';

gr.active = false;

gr.closed_by = gr.sys_updated_by;

gr.update();

}

}

}

TLDR of BR:

This business rule is designed to automatically close change requests that have been in the Review state for more than a certain number of days. The number of days is specified by the glide.ui.autoclose.change.time property, which is set to 14 in this case. The business rule does the following steps:

  • It gets the value of the glide.ui.autoclose.change.time property and converts it to an integer (pn).
  • It creates a GlideDateTime object (queryTime) and subtracts pn days from it. This is the cutoff date for closing change requests.
  • It checks if pn is greater than zero, meaning that the autoclose feature is enabled.
  • It queries the change_request table for records that have state = 0 (Review) and sys_updated_on < queryTime, meaning that they have been in the Review state for longer than pn days.
  • It loops through the query results and updates each record with the following values:
  • state = 3 (Closed)
  • active = false
  • closed_by = sys_updated_by (the last user who updated the record)
  • It saves the changes to the database.

Save.

Navigate to > System Properties (sys_properties) table

Create new System Property.

Give it a name and value (of type: integer):

Additionally, you can give it a description and assign the required roles to it.

  • Navigate back to the newly created Schedule Item (Autoclose Changes)

Add this script in the job context field: 

fcScriptName=change autoclose

Select Trigger Type: Interval

Repeat: Hours: 01

Save.

Test it!

Congrats & keep pushing!

PS. Don't forget to do it in an update set to be safe


Worth checking resources:


INCIDENT: Auto close configuration

https://www.servicenow.com/community/itsm-articles/incident-auto-close-configuration/ta-p/2302132


How can I verify a scheduled job?

https://www.servicenow.com/community/developer-forum/how-can-i-verify-scheduled-job/m-p/2111725


Test Scheduled Jobs(and Fix Scripts) in the Script Debugger

https://www.servicenow.com/community/now-platform-articles/test-scheduled-jobs-and-fix-scripts-in-the-script-debugger/ta-p/2321945


Invoking "Execute Now" for Scheduled Jobs using Javascript

https://www.servicenow.com/community/developer-articles/invoking-quot-execute-now-quot-for-scheduled-jobs-using/ta-p/2328169


Comments

Popular Posts