- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2020 07:09 AM
Hello There
can some one help me with script to update closed date on incident table
We have a issue with BR which updated the close date of the incident same as resolved , for the data fix we need to write a fix script where it should update closed at of the incident with +3 days of resolved date
For example if the incident closed at and resolved at times are 1-1-2020 the fix is to set the closed at date to set as 1-4-2020
one issue is There are 200k incidents to update can some one help me with the optimised code
The query i used is
var gr = new GlideRecord('incident');
gr.addEncodedQuery("active=false^sys_updated_by=system^sys_updated_on>javascript:gs.dateGenerate('2018-12-13','23:59:59')");
gr.query();
var count = 0;
while(gr.next()){
count++;
gr.closed_at=gr.resolved_at +3 days;
gr.update();
}
gr.setWorkflow(false);
gs.print(count);
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2020 08:19 AM
Example (= tested, obviously with different query for testing purpose) with the addDays.
var grIncident = new GlideRecord('incident');
grIncident.addEncodedQuery('sys_id=b0c8497edbd88cd0b9f39026db96195d');
grIncident.setWorkflow(false);
grIncident._query();
while(grIncident._next()) {
var newDate = GlideDateTime(grIncident.getValue('closed_at'));
newDate.addDays(3);
grIncident.setValue('closed_at', newDate);
grIncident.update();
}
Though, as mentioned, performing this 200.000 times is a bit worrying.
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
---
LinkedIn
Community article list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2020 07:55 AM
Hi there,
Can you share what you've tried / set up so far? You are mentioning ias last sentence "can some one help me with the optimised code". So you've got something already?
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
---
LinkedIn
Community article list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2020 08:06 AM
var gr = new GlideRecord('incident');
gr.addEncodedQuery("active=false^sys_updated_by=system^sys_updated_on>javascript:gs.dateGenerate('2018-12-13','23:59:59')");
gr.query();
var count = 0;
while(gr.next()){
count++;
gr.closed_at=gr.resolved_at +3 days; /// not sure how to set 3 + days here
gr.update();
}
gr.setWorkflow(false);
gs.print(count);
** please correct me if any part of the code is incorrect

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2020 08:13 AM
For adding days, you could use the addDays function. See below link, contains examples.
https://developer.servicenow.com/app.do#!/api_doc?v=newyork&id=r_GDT-addDays_N
The amount of records is a bit worrying. Doing a query and then while, 200.000 times... that won't perform well. Obviously setWorkflow(false) will help, though still this is massive.
If you somehow could group your records, like all records for 2018-12-14, and the resolved date + time can be set to the same, for example 2018-12-14 18:00:00 + 3 days, then you could already smarten the querying a bit with updateMultiple().
Or performing this in badges might help. Scheduled, in off-office hours.
Question:
This obviously needs to be done because of a certain issue you had. Is that also resolved now?
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
---
LinkedIn
Community article list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2020 08:20 AM
Thanks for the reply Mark , ya updating on 200 k incidents is bit worrying me as well,
as you mentioned may be i need to do it in badges , but all the incidents have different dates how to use the add days functionality there ?
meaning 20 k incidents have both closed at and resolved date as 2-3-2018 12:20:20