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

Karthiga S
Kilo Sage

Hi @Malo 

 

Sure, you can achieve this by creating a scheduled job in ServiceNow that will run a script to check for Request Items that have been in the 'Awaiting Approval' state for 60 days and then set their state to 'Closed' and make them inactive. Here's a guide:

1. Navigate to System Definition > Scheduled Jobs in ServiceNow.
2. Click on 'New' to create a new scheduled job.
3. Fill in the necessary details like Name, Description, and when you want the job to run.
4. In the 'Script' field, you can write a script to achieve your requirement. Here's the script:

var gr = new GlideRecord('sc_req_item'); // 'sc_req_item' is the table for Request Items
gr.addQuery('state', 'Awaiting Approval'); // Filter for Request Items in 'Awaiting Approval' state
gr.addQuery('sys_created_on', '<=', gs.daysAgo(60)); // Filter for Request Items created 60 days ago
gr.query();
while(gr.next()) {
gr.state = 'Closed'; // Set state to 'Closed'
gr.active = false; // Set active to false
gr.update();
}


5. Click on 'Submit' to create the scheduled job.

This script will run at the specified time and update the state of the Request Items that have been in 'Awaiting Approval' state for 60 days to 'Closed' and make them inactive.

 

Please mark it Correct and Hit Like if you find this helpful!

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Malo ,

 

Create the scheduled job with condition as run daily also create the system property with type integer and value as 60.

Script :

 

var grRITM = new GlideRecord("sc_req_item");
grRITM.addEncodedQuery("state=awaiting_user_info"); //replace "awaiting_user_info" with backend value of awaiting approval
grRITM.query();
while (grRITM.next()) {
    var checkAwaitingTime = new GlideRecord('sys_audit');
    checkAwaitingTime.addEncodedQuery("tablenameSTARTSWITHsc_req_item^documentkey=" + grRITM.sys_id + "^newvalue=2^sys_created_onRELATIVELT@dayofweek@ago@" + gs.getProperty('auto.close.ritm.after.days')); //replace 2 with backend value of awaiting approval and write down name of your own system property
    checkAwaitingTime.query();
    if (checkAwaitingTime.next()) {
		grRITM.state='3'; //replace 3 with closed complete backend value
            grRITM.active=false;
		grRITM.update();
    }
}

 

 Note : Replace proper backend values in script.


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

So if after I create a scheduled job, what about the system property, how do I set that up

 

Thank you

Hi @Malo ,

 

Navigate to sys_properties table.

Create new record in that as below :

Name : auto.close.ritm.after.days

Type : Integer

GunjanKiratkar_0-1693982491756.png

 


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy