The CreatorCon Call for Content is officially open! Get started here.

auto move state to close and set to inactive

Malo
Tera Contributor

Hello all

 

if my Request Item has been in awaiting approval for 60 days, I would like to set the state to closed and make it as inactive

 

How can I acheve this, can I get guideance please 

 

Also if possible can it be a system property so it is easy to modify without by anyone in a case they decide to change the length of days 

 

If not, any solution will do

 

 

I am new to all this, please be gentle

 

🙂 thank you all

6 REPLIES 6

Harish Bainsla
Kilo Patron
Kilo Patron
  1. Create a Scheduled Script Execution:

    • Navigate to "System Scheduler" in ServiceNow.
    • Create a new Scheduled Script Execution record.
    • Configure the schedule for when you want this script to run. For example, you can set it to run daily.
  2. Create a Script:

    • In the Scheduled Script Execution record, specify the script that will run when the scheduled job executes.

  3. (function() {

    var targetState = 'awaiting_approval';
    var daysThreshold = 60;

    var now = new GlideDateTime();

    var gr = new GlideQuery('sc_req_item');
    gr.addQuery('request.requested_for.state', targetState);
    gr.query();

    while (gr.next()) {
    var requestedDate = new GlideDateTime(gr.getValue('sys_created_on'));
    var daysSinceCreation = now.subtract(requestedDate).getDayPart();

    if (daysSinceCreation >= daysThreshold) {
    gr.setValue('state', 'closed');
    gr.setValue('active', false);
    gr.update();
    }
    }
    })();

HI 

 

This is what I have thus far, please let me know if I am on the right part as I am very confused 

Also, do I need to create a system property, If so can you help please 

 

Thank you 

Malo_0-1693967216472.png