Close all RITM's & its associated Tasks using Background Script.

Hussain Kachwal
Mega Guru

Hello,

I have a requirement to close all the active RITM's which were created before 2018 year. Also, to close its respective tasks, if open. Also, to avoid its workflow, BR's, notification to run on force closure.

Below is the Background Script I am trying to execute but with no luck:

var gr = new GlideRecord('sc_req_item');
gr.addQuery('number' , "RITM1234567"); // checking for 1 RITM
gr.query();

if (gr.next()) {

var st = new GlideRecord('sc_task');
st.addQuery('request_item',"RITM1234567"); 
st.query();

while(st.next()){

st.setWorkflow(false);
st.autoSysFields(false);
st.state = 3; // closed complete
st.update();
}

gr.setWorkflow(false);
gr.autoSysFields(false);
gr.state = 3; // closed complete
gr.stage ='Completed';
gr.update();
}

 

With the above query, only the RITM is getting Closed, but its associated tasks are still in the open state.

Kindly let me know if I am missing something here.

Thanks.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Hussain,

The ritm field on sc_task is reference hence your query is not working; update as below

var st = new GlideRecord('sc_task');
st.addQuery('request_item.number',"RITM1234567"); 

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Hussain,

The ritm field on sc_task is reference hence your query is not working; update as below

var st = new GlideRecord('sc_task');
st.addQuery('request_item.number',"RITM1234567"); 

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

You saved my day Ankur. I missed that part... Thanks for highlighting it.

It worked for 1 RITM but while running for multiple, it again closes the RITM's but not the associated tasks:

st.addQuery('request_item.number', gr.sys_id); // var gr = new GlideRecord('sc_req_item');

What am I missing here?

- Thanks

Hi Hussain,

Either query with sys_id or with number; use either of below lines

st.addQuery('request_item.number', gr.number);

OR

st.addQuery('request_item', gr.sys_id); 

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

AshishKM
Kilo Patron
Kilo Patron

Hi Hussain, 

you can get the query based on selected RITM number from list view and use the same filter condition with GlideRecord('sc_task')

You can user either way :

st.addQuery('request_item.numer','RITM1234567');

Or 

st.addQuery('request_item',gr.sys_id); // sys_id of RITM record  

 

find_real_file.png

 

Thanks,

Ashish

Please mark correct/helpful for other if it helps you.


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution