How to stop a stop an catalog task form closing the catalog task until the date is met on that task variables

ramya8
Kilo Contributor

we have a requirement where when a task is assigned to a  person    and on the task variables there is a date field when the date field is set for 3/17/22 we should validate with the current date  and should not allow closing the task until the date is met 

 

 

function onSubmit() {
//Type appropriate comment here, and begin script below
var type = g_form.getValue('u_task_category');
var action = g_form.getActionName();
var state = g_form.getValue('state');
if ((action == 'close_task' || state == 3) && type == 'Finance-Input') {
var date = g_form.getValue('finance_target_date');
var ga = new GlideAjax('CatalogMain');
ga.addParam('sysparm_name', 'dateValidate');
ga.addParam('sysparm_newdate', date);
ga.getXML(getData);
}

function getData(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
return false;
}
}

 

Tried this code no luck  can some one assist I'm new to servicenow

1 ACCEPTED SOLUTION

Dan H
Tera Guru

Hi,

You can create a business rule for this.

See the following:

find_real_file.png

 

 

find_real_file.png

 

(function executeRule(current, previous /*null when async*/) {

	var taskDate = current.u_finance_target_date;
	var time = new GlideDateTime();
	
	
	if(taskDate > time){
		gs.addInfoMessage("The date on the task is later than the current date.");
		current.setAbortAction(true);
	}

})(current, previous);

 

Please mark my answer as Correct/Helpful based on impact

Regards,

Dan H

View solution in original post

5 REPLIES 5

sethivarun
Kilo Guru

Hi, 

 

you don;t need to use GlideAjax here. 

have a look at the following threads for date validation via client script in servicenow: 

 

https://community.servicenow.com/community?id=community_question&sys_id=4a298be1db5cdbc01dcaf3231f96...

https://community.servicenow.com/community?id=community_question&sys_id=9a5587addbd8dbc01dcaf3231f96...0

 

Dan H
Tera Guru

Hi,

You can create a business rule for this.

See the following:

find_real_file.png

 

 

find_real_file.png

 

(function executeRule(current, previous /*null when async*/) {

	var taskDate = current.u_finance_target_date;
	var time = new GlideDateTime();
	
	
	if(taskDate > time){
		gs.addInfoMessage("The date on the task is later than the current date.");
		current.setAbortAction(true);
	}

})(current, previous);

 

Please mark my answer as Correct/Helpful based on impact

Regards,

Dan H

ramya8
Kilo Contributor

we have a UI action button on the task in this case how can we handle users who might use any of the actions

 find_real_file.png

It will not matter how to task is closed, from UI action, from changing the state or any other way. The business rule will trigger.

Please mark my answer as Correct/Helpful based on impact

Regards,

Dan H