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

Background Script to Close RITM and REQs without SCTASK under it or pending approvals under it.

FNow1
Tera Contributor

Hi Fam! 

My scripting is awful, so I apologize in advance. Dealing with an Issue where workflows were broken for w.e. reason and we have stranded REQ and RITM without SCTASKS under them. I have a background script that can close them and solve the issue but I’m having a problem where its also closing items that dont have an SCTASK because they are pending approval. 

How can I modify the below to close out RITMS and REQ that dont have an underlying task or Pending approval attached to it? 

 

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();
			}
		}
	}
}
3 REPLIES 3

OlaN
Giga Sage
Giga Sage

Hi,

I would recommend that you create a filtered list on the sc_req_item table.
There you can filter on both the correct Workflow (Context.Parent Workflow) and on the ones who are pending approval.
After you made a filter that shows the correct results, copy the query and insert it into your script,
so there's no need to start with a query on the sc_request.

find_real_file.png

Jaspal Singh
Mega Patron
Mega Patron

Something as below should help.

var getreq=new GlideRecord('sc_request');
getreq.addQuery('approval','!=','requested');
getreq.addEncodedQuery('stateNOT IN3,4,7');
getreq.setLimit(2); //run for 2 records only. Comment out later if everything works
getreq.query();
gs.print('REQ is '+getreq.number); // added for reference
while(getreq.next())
{
 var getritm=new GlideRecord('sc_req_item');
 getritm.addQuery('request',getreq); 
getritm.addQuery('approval','!=','requested');
getritm.addEncodedQuery('stateNOT IN3,4,7');
 getritm.query();
 while(getritm.next())
 {
  var chktask=new GlideRecord('sc_task');
   chktask.addQuery('request',getreq);
  var chktaskaddor=chktask.addOrCondition('request_item',getritm);
   chktask.query();
   if(!chktask.next())
{
getritm.state='3';
getritm.update();

 getreq.state='3';
getreq.update();
}

}

}

Hi @Jaspal Singh 

I tried your script and I got the error:

 

*** Script: REQ is undefined