
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-12-2018 09:23 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-12-2018 09:36 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-12-2018 09:36 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-12-2018 10:01 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-12-2018 10:04 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-12-2018 09:48 PM
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
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