Close all incidents more than a year old
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2022 01:13 PM
Hello,
Newbie to SN .
I'm trying to write background script to run for one time use only, to CLOSE ALL the incidents, that is more than a year old( 365 days).
Nothing happens when I run it in Test environment. What AM I doing wring?
rejectOldApprovalRecord ();
function rejectOldApprovalRecord ()
{
var approval = new GlideRecord("incident");
approval.addQuery('sys_updated_on' , '<=' , gs.daysAgo(365)); // 5 days ago
approval.addQuery('state', '!=' "closed');//
approval.addQuery('group.assignment_group', '');//Searches for REQUETERS MANAGER OR MANAGERS MANAGERS APPROVAL, no group approvals
approval.query();
while (approval.next())
{
approval.state='Closed';
gs.comments='Auto Closed for inactivity for more than 365 days';
gs.log(approval.number + ' ................updated with .......... ' + approval.state);
approval.update();
}
}
- Labels:
-
User Interface (UI)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2022 01:24 PM
Use addEncodedQuery with "active=true^sys_updated_on<javascript:gs.beginningOfOneYearAgo()" and remove all your addqueries
Can you clarify what you are searching with this line?
approval.addQuery('group.assignment_group', '')
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2022 07:04 AM
Im getting this :
Invalid query string received:null : null
Invalid query string received:null : null
Im clueless, as this is my first time debugging in SN now, as newbie into ity. Please help. I changed teh query as suggested by you.
rejectOldApprovalRecord ();
function rejectOldApprovalRecord ()
{
var approval = new GlideRecord("Task");
approval.addEncodedQuery ('sys_updated_on' , '<=' , gs.daysAgo(365)); // 5 days ago
approval.addEncodedQuery ('state', '=', 'Active');//
// approval.addQuery('group.assignment_group', '');//Searches for REQUETERS MANAGER OR MANAGERS MANAGERS APPROVAL, no group approvals
approval.query();
while (approval.next())
{
approval.state='Closed';
gs.comments='Auto Closed for inactivity for more than 365 days';
gs.log(approval.number + ' ................updated with .......... ' + approval.state);
approval.update();
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2022 07:29 AM
Hi Shree,
Please write following fix script. Don't forgot to mark my answer as correct if that helps.
var gr = new GlideRecord('incident');
gr.addEncodedQuery('sys_updated_on<=javascript:gs.beginningOfOneYearAgo()^active=true');
gr.query();
while(gr.next())
{
gr.state = 'closed';
gr.setWorkflow(false);
gr.update();
}
Regards,
Musab
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2022 01:04 AM
This one is efficient way.
var gr = new GlideRecord('incident');
gr.addEncodedQuery('sys_created_on<=javascript:gs.beginningOfOneYearAgo()^stateNOT IN6,7,8');
gr.query();
while(gr.next())
{
gr.state = 'closed'; // you can also put 7 as integer
gr.setWorkflow(false);
gr.update();
}
You have to query on created an year ago and not closed,cancelled,resolved to minimize no. of transactions
during running of script.
If its helpful,please mark answer as correct
Anshu