UI Action to force complete a service request

Terje Nilima Mo
Tera Contributor

I am afraid that this will expose my lack of understanding of how work flows and process engines work, but I would like to have an UI action that will force complete all SC_TASK, SC_REQ_ITEM related to an SC_REQUEST.

 

To do this I created this UI Action as a context menu item, but it does not set the children to closed. 'current' in this context should be the SC_REQUEST record, since that is the table the UI Action is created against.

 

function completeRequest(current){
	var requestItems = new GlideRecord('sc_req_item');
	//Get all Request items
	requestItems = getChildRecords('sc_req_item', current.sys_id);
	while (requestItems.next()){
		var tasks = new GlideRecord('sc_task');
		//Get all tasks
		tasks = getChildRecords('sc_task', requestItems.sys_id);
		while(tasks.next()){
			//Close task
			tasks.state=3;
			tasks.update();
		}
		//Close request item
		requestItems.state=3;
		requestItems.update();
	}
	//Close request
	current.request_state = 'closed_complete';
	current.update();
}

//This function returns all records of type 'type' where parent is 'sys_id' 
function getChildRecords(type, sys_id){
	var gr = new GlideRecord(type);
	gr.addActiveQuery;
	gr.addQuery('parent', sys_id);
	gr.query();
	return gr;
}

completeRequest(current);
new UIActionUtils().approvalsNoLongerRequired(current.sys_id);

The desired outcome is that all sc_req_item and all related sc_task records are "Closed complete" as well as the current request is closed complete.

 

Any pointers appreciated.

 

1 REPLY 1

Terje Nilima Mo
Tera Contributor

And the error was that sc_req_item users "request" and not "parent" to indicate which request it belongs to. Also utilized call by sharing to reduce the overhead.

 

 

function completeRequest(current){
	var requestItems = new GlideRecord('sc_req_item');
	//Get all Request items
	getChildRecords(requestItems, 'request', current.sys_id);
	while (requestItems.next()){
		var tasks = new GlideRecord('sc_task');
		//Get all tasks
		getChildRecords(tasks, 'parent', requestItems.sys_id);
		while(tasks.next()){
			//Close task
			tasks.state=3;
			tasks.update();
		}
		//Close request item
		requestItems.state=3;
		requestItems.update();
	}
	//Close request
	current.request_state = 'closed_complete';
	current.update();
}

function getChildRecords(gr, field, sys_id){
	gr.addActiveQuery;
	gr.addQuery(field, sys_id);
	gr.query();
}

completeRequest(current);


new UIActionUtils().approvalsNoLongerRequired(current.sys_id);