- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2020 09:38 AM
Hello,
We are in the process of fixing a workflow that is not closing out RITMs and REQs when SCTASK is closed complete. We have over 100 REQs for this particular item. The user wants us to manually close out all REQs while we are fixing the workflow. How can I create a fix script that closes the RITM and REQ? I have a fix script to close the REQ, but not sure how to close its RITM.
var gr = new GlideRecord('kb_knowledge');
gr.addEncodedQuery('descriptionSTARTSWITHaccounting^state=1');
gr.query();
while(gr.next()) {
gr.setValue('state', 3);
gr.work_notes = "This REQ is being manually closed";
gr.update();
}
***Edited****
I mistyped the table in my GlideRecord:
var gr = new GlideRecord('sc_request');
gr.addEncodedQuery('descriptionSTARTSWITHaccounting^state=1');
gr.query();
while(gr.next()) {
gr.setValue('state', 3);
gr.work_notes = "This REQ is being manually closed";
gr.update();
}
Thanks!
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2020 10:39 AM
Hi,
Try this code. but table shoudl be sc_request and your encoded query shoudl be correct.
var gr = new GlideRecord('kb_knowledge');
gr.addEncodedQuery('descriptionSTARTSWITHaccounting^state=1');
gr.query();
while(gr.next()) {
//fetch the associated ritm
var gr1 = new GlideRecord("sc_req_item");
gr1.addQuery("request",gr.getValue("sys_id"));
gr1.query();
while(gr1.next()) {
gr1.setValue('state', 3);
gr1.work_notes = "This item is being manually closed";
gr1.update();
}
gr.setValue('state', 3); //check the close state value in sc_req_item table.
gr.work_notes = "This REQ is being manually closed";
gr.update();
}
Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.
Regards,
Asif
2020 ServiceNow Community MVP

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2020 10:40 AM
HI,
You are trying to query kb_knowledge in your code.
It should be sc_request and sc_req_item table.
Thanks,
Ashutosh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2020 11:21 AM
thank you for your response

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2020 11:34 AM
var req= new GlideRecord('kb_knowledge');
req.addEncodedQuery('descriptionSTARTSWITHaccounting^state=1');
req.query();
while(req.next()) {
var ritm= new GlideRecord("sc_req_item");
ritm.addQuery("request",req.getValue("sys_id"));
ritm.query();
while(ritm.next()) {
ritm.setValue('state', 3);
ritm.work_notes = "Manually closed by Fix Script";
ritm.update();
}
req.setValue('state', 3);
req.work_notes = "Manually closed by Fix Script";
req.update();
}
Thanks,
Ashutosh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2020 01:01 PM
***EDITED***
I have also added a script for handling the sctask to close that as well. This is only closing one of the tasks and not both of the tasks associated with the REQ. There is only 1 RITM, so I would have to make a use case to see if this also happens with multiple RITMs
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2020 11:20 PM
Hi,
some script internally is reverting it back
try this and disable any BR which might be running
var gr = new GlideRecord('kb_knowledge');
gr.addEncodedQuery('descriptionSTARTSWITHaccounting^state=1');
gr.query();
while(gr.next()) {
//fetch the associated ritm
var gr1 = new GlideRecord("sc_req_item");
gr1.addQuery("request",gr.getValue("sys_id"));
gr1.query();
while(gr1.next()) {
gr1.setValue('state', 3);
gr1.work_notes = "This item is being manually closed";
gr1.setWorkflow(false);
gr1.update();
}
//fetch the associated subtask
var gr2 = new GlideRecord("sc_task");
gr2.addQuery("request",gr.getValue("sys_id"));
gr2.query();
while(gr2.next()) {
gr2.setValue('state', 3);
gr2.work_notes = "This subtask is being manually closed";
gr2.update();
}
gr.setValue('state', 3); //check the close state value in sc_req_item table.
gr.work_notes = "This REQ is being manually closed";
gr.setWorkflow(false);
gr.update();
}
If my answer solved your issue, please mark my answer as ✅ Correct & 👍Helpful based on the Impact.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader