Popup for cancel confirmation not working

Roshini
Tera Guru

I am trying to create a UI Action with name 'Cancel RITM', which will be asking for reason for cancellation, and cancel /Ok button. Once Ok is clicked, it will verify if there is some value or not, if no value it will give an info message and won't allow to proceed further, If value is there it will allow to make the RITM closed incomplete( value =4) and update the work notes with "Cancelled: " + cancel comments.

Below is the UI action and Ui page am trying, but it is not working as expected, am not getting any error.
Just the OK button is not validating anything and updating anything. It is redirecting to "ui_page_process" page with UI action sys id.


Ui action



function cancelRITM()
{
    var dialog = new GlideDialogWindow ('cancel_reason_dialog');
    dialog.setTitle('Cancellation Reason');
    dialog.setSize(750, 300);
    dialog.setPreference('sys_id', g_form.getUniqueValue()); // Pass current record ID
    dialog.render();
   
}


Ui page
HTML
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
    <g:ui_form>
        <!-- Capture current record ID passed from UI Action -->
        <input type="hidden" name="sys_id" value="${RP.getWindowProperties().get('sys_id')}" />
       
        <table width="100%">
            <tr>
                <td>
                <p>Please provide a reason for cancellation:</p>
                    <textarea id="cancel_comments" name="cancel_comments" rows="4" style="width:100%"></textarea>
                </td>
            </tr>
            <tr id="dialog_buttons">
                <td align="right">
                    <!-- UI Macro for standard OK/Cancel buttons -->
 <g:dialog_buttons_ok_cancel ok="validateReason()" ok_type="submit"
                                            cancel="cancelDialog()" cancel_type="button"/>

                </td>
            </tr>
        </table>
    </g:ui_form>
</j:jelly>



Client script
function validateReason() {
    var reason = gel('cancel_comments').value.trim();
    if (reason == "") {
        alert("Cancellation reason is mandatory.");
        return false; // Blocks submission
    }
    return true; // ALLOWS submission to Processing Script
}

function cancelDialog() {
    GlideDialogWindow.get().destroy();
    return false;
}


Processing script


    var gr = new GlideRecord('sc_req_item'); // Or your target table
    if (gr.get(sys_id)) {

        gs.info("@ro inside 1")
        gr.state = 4; // 'Cancelled' state
       // gr.close_notes = "Cancelled: " + cancel_comments;
        gr.update();
       
        // Redirect back to the form or list
        response.sendRedirect("sc_req_item.do?sys_id=" + sys_id);
    }
10 REPLIES 10

Naveen20
ServiceNow Employee

There are a few issues here — both in the code and likely in the UI Action configuration. Let me walk through them.

UI Action Configuration Issues

For a client-side UI Action to save and work properly, you need to ensure:

  1. The Client checkbox is checked on the UI Action form.
  2. The Onclick field contains the function call: promptForReason()
  3. The Script field contains only the function definition.

If the "Client" checkbox isn't checked and you're writing client-side code (using g_form, GlideAjax, etc.), ServiceNow will reject it or throw errors on save because it tries to validate it as server-side script.

Script Include Issue

Your Script Include is missing the type property, which can cause problems:

var RecordUpdateHandler = Class.create();
RecordUpdateHandler.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    updateReason: function() {
        var gr = new GlideRecord('incident');
        if (gr.get(this.getParameter('sysparm_id'))) {
            gr.work_notes = "Reason: " + this.getParameter('sysparm_reason');
            gr.update();
            return 'success';
        }
        return 'error';
    },
    type: 'RecordUpdateHandler'
});

Also make sure the Client callable checkbox is checked on the Script Include, otherwise GlideAjax won't be able to invoke it.

UI Action Script Refinement

The prompt() browser dialog can be unreliable across ServiceNow UI versions (especially in UI16/Next Experience). A safer approach using GlideModal or a simple dialog:

function promptForReason() {
    var dialog = new GlideDialogWindow('glide_ask_standard');
    dialog.setTitle('Enter Reason');
    dialog.setPreference('sysparm_cancel_text', 'Cancel');
    dialog.setPreference('sysparm_ok_text', 'Submit');
    dialog.setPreference('sysparm_message', 'Please enter the reason for this update:');
    dialog.setPreference('sysparm_type', 'textarea');
    dialog.setPreference('sysparm_ok_callback', function(win) {
        var reason = win.document.getElementById('glide_ask_standard_value').value;
        if (reason) {
            var ga = new GlideAjax('RecordUpdateHandler');
            ga.addParam('sysparm_name', 'updateReason');
            ga.addParam('sysparm_id', g_form.getUniqueValue());
            ga.addParam('sysparm_reason', reason);
            ga.getXMLAnswer(function(response) {
                if (response === 'success') {
                    g_form.addInfoMessage('Updated successfully.');
                    g_form.refresh();
                }
            });
        }
    });
    dialog.render();
}

Quick checklist to get it saving:

  1. Check the Client checkbox on the UI Action.
  2. Put promptForReason() in the Onclick field.
  3. Put the function definition in the Script field.
  4. Add type: 'RecordUpdateHandler' to the Script Include.
  5. Check Client callable on the Script Include.

Try saving again after making these changes — the most common culprit is the missing "Client" checkbox, which causes ServiceNow to parse client-side code as server-side and fail validation.