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 could write a function like below in a script include and call it in your conditions just like:

&& startDateReached(current.start_date)

function startDateReached(date){
	
	var startDate = date;
	var currentDate = gs.nowDateTime();
	var timeDiff = gs.dateDiff(currentDate, startDate, true);
	
	if(timeDiff > 0){
		return false;
	}
	return true;
}

You could refine it further to check the end date hasn't been passed as well if the change has to be done within the change window.

That is doing the same thing when I click nothing happens.  Its almost like the UI Action does not want to run Server Side scripts.  Other then the one that is part of function setRedirect().

I tested the function on my PDI and it worked...

have you reverted the UI action code to it's original form? I think the reason it wasn't working is that you're running server side code in the client side onClick function. The whole bit below is using methods not available in the client.

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);
	}

 

Harsh Vardhan
Giga Patron

can you try to update the below condition .. 

 

	if (gs.dateDiff(current.start_date.getDisplayValue(), tnow.getDisplayValue() , true) <= 0){

 

can you confirm the date diff condition would be less than 0 then you want to show the message ? 

I have already tried most of that.  I don't think you can put If in the condition filed on a UI action.