Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Fix script to close req and Ritm if sc tasks are closed complete / incomplete

Vasavi O
Tera Contributor

Hi All,

 

I have tried to implement a fix script to  check if the tasks are closed complete/incomplete and move forward to close the respective RITM and Request. This is the script I have and  it is not working properly.

 
var taskGr = new GlideRecord('sc_task');
taskGr.addEncodedQuery('stateIN3,4');
taskGr.query();
while (taskGr.next()) {
    var gr1 = new GlideRecord('sc_req_item');
    gr1.addQuery('request.state=1^stateIN1,2^cat_itemISEMPTY');
    //gr1.addEncodedQuery('numberSTARTSWITHRITM1186392');
 gr1.query();
    while (gr1.next()) {
        gr1.state = taskGr.state;
        if (taskgr.state == 4) {
            gr1.approval = "rejected";
            gr1.state = "closed_rejected";
            gr1.stage = "incomplete";
        } else if (taskgr.state == 3) {
            gr1.approval = "approved";
            gr1.state = "closed_complete";
            gr1.stage = "completed";
        }
gr1.update();
    }
}
 
Can anyone help me on this please.
Thank you.
5 REPLIES 5

Community Alums
Not applicable

Hi @Vasavi O ,

Please find the below script which you can use:

var req = new GlideRecord('sc_request');
req.addEncodedQuery('request_stateNOT INclosed_complete,closed_incomplete,closed_cancelled,closed_rejected,closed_skipped');
req.query();
while(req.next()){
	var ritm = new GlideRecord('sc_req_item');
	ritm.addEncodedQuery('stateNOT IN3,4,7');
	ritm.query();
	if(!ritm.next()){
		req.request_state='closed_complete';
		req.update();
		
	}
	else{
		while(ritm.next()){
			var task=new GlideRecord('sc_task');
			task.addEncodedQuery('stateNOT IN3,4,7');
			task.query();
			if(!task.next()){
				ritm.state='3';
				ritm.update();
			}
		}
	}
}

Hi @Community Alums , I have used these below script but not working ritm is still not updated.

var gr = new GlideRecord('sc_req_item');
gr.addEncodedQuery("request.state=1^stateIN1,2^cat_itemISEMPTY");
gr.addEncodedQuery('numberSTARTSWITHRITM1186392');
gr.query();
        while (gr.next()) {
            var task = new GlideRecord('sc_task');
            task.addEncodedQuery('stateNOT IN3,4,7');
            task.query();
            if (!task.next()) {
                gr.state = '3';
                gr.update();
            }
        }
 

Nisha15
Mega Sage
Mega Sage

Hi @Vasavi O ,

Please try this Code:

var taskGr = new GlideRecord('sc_task');
taskGr.addEncodedQuery('stateIN3,4');
taskGr.query();
while (taskGr.next()) {
var ritmGr = new GlideRecord('sc_req_item');
ritmGr.addQuery('sys_id', taskGr.request_item); // Query the request item related to the task
ritmGr.query();
while (ritmGr.next()) {
if (taskGr.state == 4) { // Closed Incomplete
ritmGr.approval = "rejected";
ritmGr.state = "closed_rejected";
ritmGr.stage = "incomplete";
} else if (taskGr.state == 3) { // Closed Complete
ritmGr.approval = "approved";
ritmGr.state = "closed_complete";
ritmGr.stage = "completed";
}
ritmGr.update();
}
}

Vasavi O
Tera Contributor

Hi @Nisha15 , I have tried this code and it is not working for me.