The Zurich release has arrived! Interested in new features and functionalities? Click here for more

UI Action with UI Page and Server Side Script

rahul2222kumar
Tera Contributor

Hi,

I have a UI action through which I am calling a popup with GlideModal, the popup should take the reason as input and set the reason on form in a field u_reason_for_cancellation . Then I have server side script in UI Action to be executed.

 

Please find the below :

 

UI Action:

Name: Cancel

Client : True

Action Name : cancel

OnClick : confirmcancel()

 

Script : 

function confirmcancel() {
    alert('UI Page loading');
    var dialog = new GlideModal("sn_grc_cancel_act");
    dialog.setPreference("sys_id", g_form.getUniqueValue());
    dialog.render();
}

function cancel() {
    alert('ui action server script triggered');

    var user_now = gs.getUserID().toString();
    var user_name = gs.getUserDisplayName() + " during " + previous.state.getDisplayValue();
    gs.eventQueue('sn_grc.bnpp_asc.notify.task.cancelled', current, user_now, user_name.toString());
    alert('event queued');

    var asmt = new GlideRecord("asmt_assessment_instance");
    asmt.addQuery("trigger_id", current.sys_id);
    asmt.addQuery("state", "!=", "complete");
    asmt.query();
    while (asmt.next()) {
        asmt.state = "canceled";
        asmt.update();
    }
    alert('state updated.');


    var act = current.sys_id;

    var theApprovals = new GlideRecord("sysapproval_approver");
    theApprovals.addQuery("document_id", act);
    theApprovals.addQuery("source_table", "sn_grc_asset_classification_task");
    theApprovals.addQuery("state", "requested");
    theApprovals.query();
    while (theApprovals.next()) {
        theApprovals.setValue("state", "cancelled");
        theApprovals.update();
    }

    alert('approval cancelled');

    var planApprovalGr = new GlideRecord("sn_grc_act_approval");
    planApprovalGr.addQuery("u_act", act);
    planApprovalGr.addQuery("state", "not_yet_requested");
    planApprovalGr.query();
    while (planApprovalGr.next()) {
        planApprovalGr.setValue("state", "no_longer_required"); //set requested;
        planApprovalGr.update();
    }
    alert('approval cancelled 2');

    current.u_current_approval_group = "";
    current.u_current_approval_level = "";
    current.state = 7;
    current.u_reason_for_cancellation = reason;
    current.work_notes = reason;
    current.update();
    action.setRedirectURL(current);
}
 
 
 
UI Page:
Name: cancel_act
Endpoint : sn_grc_cancel_act.do
 
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>
        <g:evaluate>
            var sysid = RP.getWindowProperties().get('current_table') || '';
        </g:evaluate>
        <div>
            <p><label style="margin-top: var(--now-global-space--md,20px); font-size: meduim; font-weight: 500">${gs.getMessage('Cancel ACT')}</label></p>
            <label style="margin-top: var(--now-global-space--md,14px); font-size: smaller">${gs.getMessage('ACT Cancellation Reason')}</label>
            <textarea name="reason_to_cancel" rows="10" cols="80" id="reason_to_cancel" class="form-control"></textarea>
            <p align="right">
                <button style="margin-right: 30px" class="button1" onclick='onok()'>${gs.getMessage('ok')}</button>
                <button class="button2" onclick="oncancel()">${gs.getMessage('Cancel')}</button>
            </p>
        </div>
    </g:ui_form>
</j:jelly>
 
Client Script:
function onok() {
    alert("OK clicked");
    var reason = gel("reason_to_cancel").value.trim();
    alert("Reason is:" + reason);
    gsftSubmit(null, g_form.getFormElement(), 'cancel');
    GlideModal.get().destroy();
}

function oncancel() {
    alert("Cancel clicked");
    GlideModal.get().destroy();
    return false;
}

With this, I am getting the popup, but after entering the reason and clicking on OK, it does not process server side script. Please help with this.
1 REPLY 1

Ankur Bawiskar
Tera Patron
Tera Patron

@rahul2222kumar 

this line should pass the control to UI action

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

If not then move the server side logic from UI action to UI page processing script.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader