- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-21-2025 04:14 AM
var grd = new GlideDateTime("2025-07-21");
grd.addDays(-30);
grd.query();
var gr = new GlideRecord("incident");
gr.addQuery('sys_updated_on','<',grd);
gr.query();
while(gr.next()){
if(gr.state!='7'){
gr.setValue('state','7');
gs.info(gr.number);
}
}
i write this code can anyone please help me to correct this code ?? as it worked but doesnot change the state to closed of some incidents IDK why?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-21-2025 05:41 AM
Hi @harshchaudh ,
try this
var grd = new GlideDateTime("2025-07-21 00:00:00");
grd.addDays(-30);
grd.query();
var gr = new GlideRecord("incident");
gr.addQuery('sys_updated_on', '<', grd);
gr.query();
if (gr.next()) {
if (gr.state != '7') {
gr.setValue('state',7);
gr.setUseEngines(false);
gr.setWorkflow(false);
gr.update();
gs.info(gr.number);
}
}
why not use the Update JOB (no code approach)?
https://www.youtube.com/watch?v=5jZmZS6tb1I
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Chaitanya

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-21-2025 04:23 AM - edited ‎07-21-2025 04:39 AM
Use this below script in background scripts.
var incGr = new GlideRecord("incident");
incGr.addActiveQuery();
incGr.addEncodedQuery("sys_updated_onRELATIVELT@dayofweek@ago@30");
incGr.query();
while (incGr.next()){
incGr.state = 7;
incGr.close_code = 'Solution provided';
incGr.close_notes = 'Closed via background script';
incGr.update();
}
Accept the solution and mark as helpful if it does, to benefit future readers.
Regards,
Sumanth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-21-2025 04:36 AM
Sorry but i need to run it without encoded query.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-21-2025 05:10 AM
If you want to do it without encoded query, you can use below script
var incGr = new GlideRecord("incident");
incGr.addActiveQuery();
incGr.query();
// Calculate date 30 days ago
var thresholdDate = new GlideDateTime();
thresholdDate.subtractDays(30);
while (incGr.next()) {
// Compare sys_updated_on with thresholdDate
if (incGr.sys_updated_on.getGlideObject().compareTo(thresholdDate) < 0) {
incGr.state = 7;
incGr.close_code = 'Solution provided';
incGr.close_notes = 'Closed via background script';
incGr.update();
}
}
Though this works, curious to know why you don't want to use encoded query.
Accept the solution and mark as helpful if it does, to benefit future readers.
Regards,
Sumanth

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-21-2025 04:28 AM
The line grd.query(); is incorrect because GlideDateTime doesn't have a query() method. You should remove this line.
Ensure that '7' corresponds to the closed state in your instance's state field for incidents. Different instances might have different state values for 'Closed'.
After setting a field value, you need to call gr.update(); to save the changes.
Here's the revised code:
var grd = new GlideDateTime("2025-07-21");
grd.addDays(-30);
var gr = new GlideRecord("incident");
gr.addQuery('sys_updated_on', '<', grd);
gr.query();
while (gr.next()) {
if (gr.state != '7') {
gr.setValue('state', '7');
gr.update(); // Save the changes
gs.info(gr.number + " state updated to closed.");
}
}