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

hope the condition to run the ui action is right

I just restored it back to the OOB version then add in just var tnow = gs.nowDateTime(); and it stops working.  it does not seem to like this code.

Lets debug this .

trying running this on script backgroud on any change and try to see if you can print the message

var sysid = 'sysid of change'

var tnow = gs.nowDateTime();

do glide on change

if next()

{


if (gs.dateDiff(gr.start_date.getDisplayValue(), tnow , true) <= 0)
return;

print('error');

else

{

print ('no error');

}

It work after removing the return. The return gives an error when you try to run in the script background.  You cannot do a return and have the reset of the script run.  And fixed some other stuff with the script like missing semicolons and squiggly brackets.

My bad return will always return error when you running it on scripts background.

So first part of the code is work

hope moveToImplement() is added to Onclick field on UI action