- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2023 07:49 PM
I want to change RITM state based on the SCTASK state, and don't want to run any business rules or workflow while doing this ,so I write following script:
change_state();
function change_state() {
var gr = new GlideRecord("sc_task");
gr.addEncodedQuery("request_item.cat_item=e1e0207edbe9e3008d52dc62ca9619cb^stateIN3,4,7^parent.state=1");
gr.query();
while (gr.next()) {
var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('sys_id', gr.request_item);
ritm.query();
if (ritm.next()) {
//var st = gr.getDisplayValue('state');
ritm.state = gr.state;
//ritm.state = gr.state;
ritm.autoSysFields(false);
ritm.setWorkflow(false);
ritm.update();
}
}
}
after running this script the state of RITM is changed to 'closed skipped' or 'Pending'
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2023 10:38 PM
this query is not required -> ^parent.state=1
try this
change_state();
function change_state() {
var gr = new GlideRecord("sc_task");
gr.addEncodedQuery("request_item.cat_item=e1e0207edbe9e3008d52dc62ca9619cb^stateIN3,4,7");
gr.query();
while (gr.next()) {
var ritm = gr.request_item.getRefRecord();
ritm.state = '3'; // set to closed
ritm.autoSysFields(false);
ritm.setWorkflow(false);
ritm.update();
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2023 08:42 PM
ideally we change the state of RITM to closed once all SC Tasks are closed
Your above script is written in some BR?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2023 10:27 PM
@Ankur Bawiskar
Yes...I have created one business rule to update future records, But to update all older records that comes under this condition I have created the fix script and ran it but it didn't worked properly its making state value on RITM as "Closed Skipped" whereas it should be "Closed Completed".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2023 10:38 PM
this query is not required -> ^parent.state=1
try this
change_state();
function change_state() {
var gr = new GlideRecord("sc_task");
gr.addEncodedQuery("request_item.cat_item=e1e0207edbe9e3008d52dc62ca9619cb^stateIN3,4,7");
gr.query();
while (gr.next()) {
var ritm = gr.request_item.getRefRecord();
ritm.state = '3'; // set to closed
ritm.autoSysFields(false);
ritm.setWorkflow(false);
ritm.update();
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2023 08:50 PM
@pk31 I ran your script in the background script and it ran fine for me. In order to automate the state sync between SC Task and RITM, you will have to write an onAfter Update BR on SC Task table and use this script.