Fix script to close REQ and RITM

mkader
Kilo Guru

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!

1 ACCEPTED SOLUTION

asifnoor
Kilo Patron

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

 

View solution in original post

19 REPLIES 19

Upender
Giga Expert

Hi Mkader,

Try this code.

 

closeRitms();
closeRequests();

function closeRitms(){
var ritm = new GlideRecord('sc_req_item');
ritm.addEncodedQuery('active=true^state=3^ORstate=4^ORstate=7');
ritm.query();
while (ritm.next()) {
ritm.active = false;
ritm.setWorkflow(false);
ritm.update();
}
}

function closeRequests(){
var request = new GlideRecord('sc_request');
request.addEncodedQuery('active=true^request_state=in_process');
request.query();
while (request.next()) {
var taskCount = new GlideAggregate('sc_req_item');
taskCount.addAggregate('COUNT');
taskCount.addQuery('request', request.getValue('sys_id'));
taskCount.addActiveQuery();
taskCount.addQuery('state', 'IN', '3,4,7');
taskCount.query();
if (taskCount.next()) {
var number = taskCount.getAggregate('COUNT');
if (number == 0) {
request.active=false;
request.request_state = 'closed_complete';
request.setWorkflow(false);
request.update();
}
}
}
}

 

 

 

Regards,

Upender

@Upender - Thanks for your response. My encoded query had an "accounting" filter. What was the reason you left that out? Also why did you add the query for state being 3,4,7? 

Hi,

the script shared by your is on knowledge table

How does it relate for using accounting field?

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Ankur Bawiskar  Sorry it was a typo when I was typing it out here. table is sc_request