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

Prevent users from moving the Change to the next State before the CSTASK is completed

Alicja1
Kilo Contributor

I have a Workflow for a Standard Change in which I would like to prevent the users from moving the Standard Change to the next State (this is done manually by clicking on the button or setting the 'State' filed to the desired state) before the CTASK created on this state (creation via Workflow) is set to one of the 'Complete' statuses. 

In situation when the user clicks on the button of the next State and the CSTASK is still open, a message should be displayed indicating that the State cannot be changed because the task(s) have not been completed.

How can I best achieve this? I have read many posts and some people advise UI Actions, others Business Rules but due to my limited knowledge of scripting I cannot figure it out myself..

I appreciate your help!

Alicja

31 REPLIES 31

OK so i tested on my PDI and the script include is actually returning a string value so the if statement is not working properly. You can either change the if statement to 'if(response == 'true') or package up the response in a JSON eg:

script include:

var ChangeTaskUtils = Class.create();
ChangeTaskUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	checkTasks: function(){
		var changeID = this.getParameter('sysparm_change');
		
		var gr = new GlideRecord('change_task');
		gr.addQuery('change_request', changeID);
		gr.addActiveQuery();
		gr.setLimit(1);
		gr.query();
		return JSON.stringify(gr.hasNext());
	},
	
	type: 'ChangeTaskUtils'
});

Client script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '' || g_form.getValue('state') == oldValue) {
		return;
	}
	
	var ga = new GlideAjax('ChangeTaskUtils');
	ga.addParam('sysparm_name', 'checkTasks');
	ga.addParam('sysparm_change', g_form.getUniqueValue());
	ga.getXMLAnswer(returnAjaxData);
	
	function returnAjaxData(response){
		var answer = JSON.parse(response);
		if(answer){
			g_form.addErrorMessage('You cannot proceed a change with active tasks against it');
			g_form.setValue('state', oldValue);
		}
	}
}

Alicja1
Kilo Contributor

I am not sure what to do now - modify the Script Include 'ChangeTaskUtils' and the Client script?

What about the other Script Include from the beginning of this thread - checkTasks? Can I disable it? And in the UI action remove the && !checkTasks(current.getUniqueValue()) condition? Or does it have to stay?

I also have an active Business rule that you have suggested at the beginning of this thread - should be stay active or can I disable it?

 

 

 

Lol, yeah this thread has gone on a bit too long.

So the checkTasks script include you are calling as a condition in your UI actions so they're only visible when there are no open tasks.

The ChangeTaskUtils is the script include you call from the glide ajax in the onChange client script which prevents the state from being changed when there are open tasks.

The business rule isn't really necessary as the previous 2 methods should prevent state changes whilst tasks are open but there's no harm in keeping in active to catch anything that might slip through some other way like some enterprising user changing the state from the list view.

Alicja1
Kilo Contributor

Hi David,

Thank you for all your help, unfortunately I wasn't able to achieve what I was hoping for. 

It seems that the change of the State to 'Scheduled' in the progress bar is hard-coded somewhere in Service Now and i am not able to stop it or find where it is coming from.

Once again, thank you for your effort, much appreciated. 

Alicja

 

OK well there must be something else going on, the solutions i've provided either prevent the UI actions to progress the change from being visible whilst there are active tasks or prevent the changing of the state in the change field if there are active tasks. There should be nothing submitted to the server which is the only thing that will effect a change of the process flow bar at the top of the change form. 

I've tested all of this on my PDI and it works as required.