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

Medi C
Giga Sage
Giga Sage

Hello @Raji15,

 

Please add "Run as" to your form layout and make sure its value is "System administrator" or a user with enough permission to perform the update 
MediC_0-1743100474860.png

MediC_1-1743100559026.png


If that doesn't resolve the issue, please share the logs if there are any errors thrown.

 

 


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

Raji15
Tera Contributor

Hi @Medi C , Yes I did that 

 

I am not even able to see the logs in system logs. I used the below code still no luck .

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

Raji15
Tera Contributor

Hi @Robert H,

 

I used the above code still it is not executing. Wht might be the issue? please help