The CreatorCon Call for Content is officially open! Get started here.

Need to change RITM state based on sctask

pk31
Tera Contributor

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'

1 ACCEPTED SOLUTION

@pk31 

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.

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

View solution in original post

5 REPLIES 5

@Sandeep Rajput 

I need 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".