Fix script to update multiple records
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-11-2020 02:06 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-13-2020 01:12 PM
this is not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-11-2020 03:00 PM
The relationship between sc_request and sc_req_item is one to many and the above script does not allow for sc_request with more than 1 sc_req_item, but as a ServiceNow developer you already know that - right?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-11-2020 04:15 PM
Hi,
Find the below code :-
var gr = new GlideRecord('sc_req_item');
gr.addEncodedQuery('state=4^active=false');
gr.setLimit(5);//this will update only 5 records only for testing purpose, please remove this line so that all the records will be updated at once
gr.query();
while(gr.next()){
var gr2=new GlideRecord('sc_request');
gr2.addActiveQuery();
gr2.query();
if(gr2.next()){
gr2.setValue('state','value_of_state_you_want_to_set');//4 is the value for closed incomplete and 3 for complete
gr2.update();
}
}
If i was able to solve your query then mark my answer correct and helpful.
Thanks & Regards,
Prasant kumar sahu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-13-2020 01:29 PM
You can use this code. First run with commit = false to check the list of records that will be updated and then run the query with commit = 'true' to actually update it.
var commit = 'false'
var grRequest = new GlideRecord("sc_request"); grRequest.addActiveQuery(); grRequest.query(); while (grRequest.next()) { var grRequestedItem = new GlideRecord("sc_req_item"); grRequestedItem.addQuery("request",grRequest.sys_id); grRequestedItem.addQuery('active', false); grRequestedItem.query(); if(grRequestedItem.getRowCount() == 0) {
}
gs.print(grRequest.number + " | " + grRequest.openedby.name + " | " + grRequest.syscreated_on + " -This request will be closed by the script");
if(commit =='true'){
grRequest.active = false;
grRequest.update();
}
}