Need to close Bulk RITM's in ServiceNow state and stage as completed. Can you help me the query.

Swathi504893573
Giga Contributor

HI Team,
i have a requirement to close Bulk RITM's in ServiceNow, state and stage as completed. Can you help me the query.
Thank you.

2 REPLIES 2

Tanushree Maiti
Giga Patron

Hi @Swathi504893573 

 

You can use fix script /background script to run it:

 

var ritmGR = new GlideRecord("sc_req_item");
ritmGR.addEncodedQuery("stateNOT IN3,4,7^<your_query_here>"); // Change <your_query_here> to filter the RITMs, like numberIN<ritmlist>
ritmGR.setLimit(100); // Batch processing
ritmGR.query();

while (ritmGR.next()) {
// Close associated tasks first
var taskGR = new GlideRecord("sc_task");
taskGR.addQuery("request_item", ritmGR.sys_id);
taskGR.addQuery("state", "NOT IN", "3,4,7");
taskGR.query();
while (taskGR.next()) {
taskGR.setValue("state", "3"); // Closed Complete
taskGR.setValue("active", false);
taskGR.update();
}

// Close the RITM
ritmGR.setValue("state", "3"); // Closed Complete
ritmGR.setValue("stage", "complete");
ritmGR.setValue("active", false);
ritmGR.update();
}

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin:

Rafael Batistot
Kilo Patron

Hi @Swathi504893573 This might help you 

 

https://www.servicenow.com/community/developer-forum/need-help-to-close-bulk-ritms-and-sc-tasks-effi...

See an example of code to get all them: 

 

var gr = new GlideRecord('sc_req_item');
gr.addQuery('cat_item','YOUR_CATALOG_ITEM_SYS_ID');
gr.addQuery('state','!=',3);
gr.query();
while (gr.next()) {
gr.state = 3;
gr.stage = 'completed';
gr.update();
}

 

If this response was helpful, please mark it as Helpful and, if applicable, as Correct, this helps other users find accurate and useful information more easily.