Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Cancel RITM using Servicenow Scheduled Job

Raji15
Tera Contributor

Hi Everyone,

I have Jira where I have to do a scheduled job to cancel requested items stuck in approval for more than 30 days.

I have executed the following script in background script it is running (the given ritm is getting rejected). When I executed in Scheduled job its not even getting executed. 

The script I used in BG script 

var ritmGR = new GlideRecord('sc_req_item');

ritmGR.addEncodedQuery('cat_item=fcb5900a1bdc82104db699f2b24bcb75^sys_created_on<=javascript&colon;gs.beginningOfLast30Days()^stage!=complete^approval!=approved^ORapproval=NULL');

ritmGR.query();

 

while(ritmGR.next()){

    ritmGR.setValue('state','4');

    ritmGR.setValue('close_notes','Closing as this request is not approved for more than 30 days');

    gs.info(ritmGR.getRowCount());

    ritmGR.update();

}

 

Please help..!

Thanks ahead

1 ACCEPTED SOLUTION

Robert H
Mega Sage

Hello @Raji15 ,

There is a syntax error in the encoded query. Try this:

var ritmGR = new GlideRecord('sc_req_item');

ritmGR.addQuery('cat_item', 'fcb5900a1bdc82104db699f2b24bcb75');
ritmGR.addQuery('sys_created_on', '<=', gs.beginningOfLast30Days());
ritmGR.addQuery('stage', '!=', 'complete');
ritmGR.addQuery('approval', '!=', 'approved').addOrCondition('approval', 'NULL');

ritmGR.query();

while (ritmGR.next()) {
    ritmGR.setValue('state', '4');
    ritmGR.setValue('close_notes', 'Closing as this request is not approved for more than 30 days');
    ritmGR.update();
}

And, as @Medi C mentioned, make sure the "Run as" user has the permission to update these records.

Regards,
Robert

View solution in original post

11 REPLIES 11

Raji15
Tera Contributor

Hi,

I have marked it as answered😊

Thank you for clarification.

sanketpatil09
Tera Guru

Your scheduled job might not be working due to permissions, logging issues, or a faulty query. Since scheduled jobs run under a system user, try adding gs.log("Scheduled Job Started", "ScheduledJob"); to check if it's running. If the query isn’t fetching records, test it in Scripts - Background. To improve performance, use ritmGR.autoSysFields(false); before ritmGR.update();. If it still doesn’t work, consider using a Business Rule to auto-close requests older than 30 days or a Flow Designer for a no-code solution.