How to set up a Cancel Button with Confirm Dialog and skipping mandatory check?

Niclas
Giga Guru

We have the following requirement

  1. Cancel Button should prompt a Confirm Window (client-code)
  2. If Cancel Button is clicked, mandatory field check shall be skipped
  3. We need to trigger some server-side logic after confirmation

 

So for 1) and 3) thanks to ServiceNowGuru we already now how to combine Client and Server side code in the UI Action

For 2) I have read that you can call the UI action sysverb_cancel to ignore the mandatory checks.

So based on this, I named the UI action sysverb_cancel and added the following Script: 

 

function confirmCancel(){
	if (confirm(getMessage('Are you sure you want to cancel?')))
		gsftSubmit(null, g_form.getFormElement(), 'sysverb_cancel');
	else
		return false;
}


//Code that runs without 'onclick'
//Ensure call to server-side function with no browser errors
if (typeof window == 'undefined'){
	gs.log("Test"); //This message isn't logged
}

 

The problem is, that it seems like you can not combine sysverb_cancel with a client & server-side UI Action. This is what I observe:

  • Confirm dialog pops up correctly
  • No error about missing mandatory fields is displayed
  • But the server side code is not triggered, the example message is not logged?


As soon as I rename the Action Name to something like custom_cancel_button the server-side code will be triggered correctly.

Unfortunatley there is no official documentation of sysverb_cancel. Does anyone know how it can be combined with client & server side code?

 

3 REPLIES 3

Michael Fry1
Kilo Patron

If you want to skip mandatory fields, you need to add a UI Policy too. Below is example of Cancel Change. The State changes to Canceled, that's what your UI Policy condition is based on. When State - is - Canceled - no mandatory fields.

If they click cancel, the State is returned to it's original value.

Once they click OK, then the Server side script runs.

g_form.getFormElement(), 'cancel_change'); <-- replace cancel_change with your Action name

 

function cancelchange(){
    var st = g_form.getValue('state');
    g_form.setValue('state', 'Canceled');
    
    var answer = confirm('Are you sure you want to cancel this Change?');
    if(answer != true){
        g_form.setValue('state', st);
        return false;
    }
    gsftSubmit(null, g_form.getFormElement(), 'cancel_change');
}

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

function serverResolve(){
    current.state = '4';
    current.update();
    action.setRedirectURL(current);
}



That's sounds reasonable. But I think the setValue of the state won't work if it's read only due to ACL or Dictionary. But instead of a UI Policy I could disable all the mandatory fields via the client site code in the UI action. 

Anyway it would be much more handy if it would be possible to combine the sysverb_cancel "magic" (which disables mandatory check automatically) with a confirm box. So you wouldn't need to update your UI Policy / UI Action everytime a new (mandatory) field gets added to the form! But doesn't seem to be possible 😕 

 

 

Set the action name to sysverb_cancel

and update this line:

    gsftSubmit(null, g_form.getFormElement(), 'sysverb_cancel');

If should then skip over mandatory fields