How to close RITM and REQ from an SCTASK

mkader
Kilo Guru

Hello,

We have a catalog item that generates a REQ a little differently than the OOB way. When a REQ is created, it goes through an approval process first. Once the REQ is approved, an SCTASK and RITM gets created under the REQ. The SCTASK is not created under the RITM. My question is, when the SCTASK that is created under the REQ is closed, how can I also close the RITM and REQ or to make things more simple, how can I close all tickets once the SCTASK is closed?

Thanks!

*** EDITED ***

Here is an example of what I am talking about 

find_real_file.png

As you can see when a REQ is created and approved, a RITM and SCTASK is also created. Within the RITM there is an additional SCTASK created. There is redundancy happening here. The users are working within the SCTASK being created on the REQ and not the RITM. If the SCTASK within the RITM is closed, it works as expected by closing the RITM and the REQ but not when the SCTASK in the REQ is closed.

1 ACCEPTED SOLUTION

@mkader 

to close the RITM and other SC TASK for this REQ use this

Update the existing BR code with this

// get RITM for this REQ and close it

var ritm = new GlideRecord('sc_req_item');

ritm.addQuery('request', current.request);

ritm.query();

if(ritm.next()){

ritm.state = 3;

ritm.update();

// close SC Task for this RITM

var taskRec = new GlideRecord('sc_task');

taskRec.addQuery('request_item', ritm.sys_id);

taskRec.addEncodeddQuery('stateIN-5,1,2');

taskRec.query();

while(taskRec.next()){

taskRec.state = 3;

taskRec.update();

}

// close the REQ

var req = current.request.getRefRecord();
req.state = 3;
req.update();

Regards
Ankur

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

View solution in original post

28 REPLIES 28

You are welcome

Regards
Ankur

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

@Ankur Bawiskar - I was told by members on my team that this won't work because this BR only takes into consideration 1 SCTASK being created and will therefore cancel the REQ even if multiple SCTASKs are still in the Open state under the REQ. It seems the best approach is to go through the workflow. Can you please walk me through the workflow steps? 

An example would be. User adds several items to the cart. All these items get checked out together and creates 1 REQ. After I tested the BR solution, the SCTASK gets closed, the related RITM gets closed and the REQ gets closed, but there are still several RITMS that are open. 

Yash Agrawal1
Tera Guru

Helllo mkader, 

Please use the below code on requested_item table to close the task.

(function executeRule(current, previous /*null when async*/ ) {

    var gr = new GlideRecord('sc_task');//to close the task
    gr.addActiveQuery();
    gr.addQuery('request_item', current.getUniqueValue());//considering request_item is a field on task table which stores the ritm numner ,you can change it acoding to your need
    gr.query();
    while (gr.next()) {
        gr.setValue('state', 4); //replace 4 with the correct value of state which you want to update
        gr.update();
    }

})(current, previous);

Please keep posted for more help

Regards

Yash Agrawal