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

@Yash Agrawal - Thank you for your response. How can I do this within the same workflow the sc_task is being created? The way our SCTASK creations is a little weird. We have a RITM that is also created on the REQ and there is an SCTASK related to that RITM. My goal is to prevent them from interfering with each other so that's why I am trying to directly in the workflow.

The users are working on the SCTASK that is being created within the REQ and not the RITM

For Ex:

find_real_file.png

As you can see in this REQ, a RITM and SCTASK is created. If I open the RITM, there will also be an SCTASK. Our users are not working within the RITM, they are working within the SCTASK that gets created on the REQ

Hello,

Would you please tell me that when the REQ is approved

are you creating task and ritm both ,or first ritm is created then task.

Please keep posted ,so that i can help you better.

Regards

Yash Agrawal

Hello User,

Will you please tell me that do you have any field on task table to know that task belongs to which ritm or which request.

One more question,as you said you have a task under request also and after opening ritm there also you have task,Hows it is possible.

Also note on REQ form,if you see the catalog tasl realted list then it does not mean that you have task directly under REQ.

Regards

Yash Agrawal

@Yash Agrawal - We have 2 workflows that kick off when a this REQ is created. The first workflow that kicks off creates the standard REQ > RITM > SCTASK. The second workflow that kicks off creates REQ > SCTASK after the REQ is created.

To answer the first part of your question, yes there is a field that has the REQ # on the SCTASK. 

find_real_file.png

I also forgot to mention that closing the RITM from the first workflow will close the REQ, but not the SCTASK from the Second workflow. If we can close the RITM then this will solve the issue

Hello @mkader

Lets first close the task which hierarchy is 

Req>>task

so close the task,use the below code.

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

    var grrequest = new GlideRecord('sc_task');//to close the task
    gr.addActiveQuery();
    gr.addQuery('request', current.getUniqueValue());//considering request is a field on task table which stores the request 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);

Above code will close the task which comes under request.please create a after business rule with condition

state is closed_complete,on REQUEST TABLE.

Please keep posted,for more help.

Regards

Yash Agrawal