UI Action Display Error message

Brian Lancaster
Tera Sage

I want to make it so that when someone click on the UI Action Implement before the start time it gives them an error message.  I'm not sure they best way to code date/time comparisons in a UI action.  What would be the best way to do this?

1 ACCEPTED SOLUTION

Brian Lancaster
Tera Sage

Hello thanks for you help.  I got this working by using a script include I had for validating date/times on client scripts for catalog items.

function moveToImplement(){
	var validTime = new GlideAjax ("ClientDateTimeUtils");
	validTime.addParam('sysparm_name', 'getNowDateTimeDiff');
	validTime.addParam('sysparm_fdt', g_form.getValue('start_date'));
	validTime.getXMLAnswer(function(dateDiff){
		if (dateDiff > 0){
			alert('You cannot implement this change before the approved change windows. ' + g_form.getValue('start_date'));
		}
		else {
			var ga = new GlideAjax("ChangeRequestStateHandlerAjax");
			ga.addParam("sysparm_name", "getStateValue");
			ga.addParam("sysparm_state_name", "implement");
			ga.getXMLAnswer(function(stateValue) {
				g_form.setValue("state", stateValue);
				gsftSubmit(null, g_form.getFormElement(), "state_model_move_to_implement");
			});
		}
	});
}

if (typeof window == 'undefined')
	setRedirect();

function setRedirect() {
	current.update();
	action.setRedirectURL(current);
}

View solution in original post

26 REPLIES 26

You have an onClick function that only runs client side code, once that has completed the gsftsubmit line calls back to the action name which then runs the server side code. 

The code you put in the function called in the onClick must be client supported API, using glidesystem, the current object and dateDiff won't work in that function.

Brian Lancaster
Tera Sage

Hello thanks for you help.  I got this working by using a script include I had for validating date/times on client scripts for catalog items.

function moveToImplement(){
	var validTime = new GlideAjax ("ClientDateTimeUtils");
	validTime.addParam('sysparm_name', 'getNowDateTimeDiff');
	validTime.addParam('sysparm_fdt', g_form.getValue('start_date'));
	validTime.getXMLAnswer(function(dateDiff){
		if (dateDiff > 0){
			alert('You cannot implement this change before the approved change windows. ' + g_form.getValue('start_date'));
		}
		else {
			var ga = new GlideAjax("ChangeRequestStateHandlerAjax");
			ga.addParam("sysparm_name", "getStateValue");
			ga.addParam("sysparm_state_name", "implement");
			ga.getXMLAnswer(function(stateValue) {
				g_form.setValue("state", stateValue);
				gsftSubmit(null, g_form.getFormElement(), "state_model_move_to_implement");
			});
		}
	});
}

if (typeof window == 'undefined')
	setRedirect();

function setRedirect() {
	current.update();
	action.setRedirectURL(current);
}