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

rajesh9885
Mega Guru

it works the same way it work for business rule.

 compare the dates 

something similar

startdate();

function startdate() {
var tnow = gs.nowDateTime();
//gs.addInfoMessage('tnow value is ' + tnow);
if (gs.dateDiff(current.start_date.getDisplayValue(), tnow , true) <= 0)
return;

gs.addErrorMessage(gs.getMessage("{0} must be after current time", [ current.start_date.getLabel()]));

current.setAbortAction(true);
}

 

regards

Rajesh

That is basically what I'm dong except I'm not calling a function.  But when I click on the UI Action I get no message and nothing else happens even if I'm at or after the start time.  Here is the full code from the UI Action.  This is mostly the OOB UI Action.

function moveToImplement(){
	var tnow = gs.nowDateTime();
	if (gs.dateDiff(current.start_date.getDisplayValue(), tnow , true) <= 0){
		gs.addErrorMessage('You cannot implement this change before the approved change windows. ' + current.start_date.getDisplayValue());
		current.setAbortAction(true);
	}
	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);
}

 

put a addinfo message on line 2 and see if you getting any value for tnow and please do check if condition to run is ok

var tnow = gs.nowDateTime();

Also i can see that you are not using "return"

you are asking the system to return the message

I'm not getting the add info message and when I tried to add the return where you had it I got errors on the reset of the script.  I have never had to add a return to display a message using a business rule.