Creating a pop-up using a GlideDialogWindow

General Add comments
by:
I recently had to create a pop-up window with a reference field where one could select a user to redirect an approval to. A Glide Dialog Window seemed to be the best option to me.
An approver had to be able to open the window by clicking on the ‘Redirect’ button. This button didn’t exist yet, so I created it.

The UI Action

Below you see the settings for the UI Action: It’s a Form Button that is client-callable.

The Redirect button isn’t supposed to be visible for everyone, only users with the role approval_admin should be able to redirect or the current approver. I also do not want the button to be visible if the approval has a different state than ‘requested’ and neither do I want to be able to redirect other than Requested Items approvals, so the source table must be ‘sc_req_item’ which is the table with the requested catalog items.

Once the button is clicked I would like see a window popping up, so I set ‘onClick’ to run the function ‘approvalDialog()’, which is located in the script section of the UI Page:

The function ‘approvalDialog()’ will first get the sys_id of the approval record, we need that later on, in order to find the approval record back on the server side. I do this by creating the variable ‘si’ and populate it with the result of ‘g_form.getUniqueValue()’, that retrieves the sys_id of the current record.
Next, I initialize the Glide Dialog Window. For this I populate the variable ‘gdw’ with a new GlideDialogWindow object. I give the object a fitting title, give it the sys_id which I stored in the variable ‘si’ and then it’s time to have a look at the UI Page that gives the pop-up it’s appearance.
I provide the new UI Page ‘sh_redirect_approver’, because that is what my new Glide Dialog Window object is looking for.

The UI Page

A UI page consists of three sections. The HTML section where the pop-up window is shaped, the client script section, where the code is that is called in the HTML section and finally, the processing script, where the server-side code is.
Below is the code for the HTML section:

<g:ui_form>
    <!-- get the sys_id value that has been stored in 'si' and is sent by the UI Action -->
    <g:evaluate var="jvar_si" expression="RP.getWindowProperties().get('si')" />
    <!-- store the approval sys_id in the (hidden) sid object -->
    <input type="hidden" name="sid" value="${jvar_si}"/>
    <input type="hidden" id="cancelled" name="cancelled" value="false"/>
    <TABLE BORDER="0">
        <TR>
            <TD>
                Please select the user you wish to set as approver.
            </TD>
        </TR>
        <TR>
            <TD>
                <g:ui_reference id="approver_picker" name="approver"table="sys_user" columns="name" columns_search="false" order_by="name" show_popup="true" query="active=true^locked_out=false" />
            </TD>
        </TR>
        <TR>
            <TD>
                <P></P>
            </TD>
        </TR>
        <TR id="dialogbuttons">
            <TD align="center">
                <g:dialog_buttons_ok_cancel ok="return validateApprover();" cancel="onCancel();" />
            </TD>
        </TR>
    </TABLE>
</g:ui_form>

The UI Page above is a Jelly page. With Jelly, logic can be embedded within static content and computed values may be inserted into the static content. ‘g’ tags are extensions to the Jelly syntax, specific to ServiceNow.

Lets have a look at the code above. First it retrieves the sys_id which we stored in the variable ‘si’. This is done with the function ‘RP.getWindowProperties().get(‘si’)’, this obtained value is now stored in the variable ‘jvar_si’. Immediately I create a new, hidden object to which I assign the value of jvar_si and I give it the name ‘sid’. This way, I am sure that I can use the variable on the server-side.
There’s another hidden object that is created: ‘cancelled’ and I use it to tell the server-side processing script whether it should run or not.
Next I call the UI Macro ‘ui_reference’ that I use to make the dropdown list. You can see that it queries the sys_user table for users that are active and are not locked out. Note that we give it the name ‘approver’, this will be used in the processing script.
Finally, I create two buttons. One called ‘OK’, that calls ‘validateApprover()’ and another, ‘Cancel’ that calls ‘onCancel’.
Both functions are in the second section, the client script:

function onCancel() {
    // Set cancelled to 'true' so that the server-side doesn't update the record.
    var c = gel('cancelled');
    c.value = "true";
    GlideDialogWindow.get().destroy();
    return false;
}

function validateApprover() {
    // I don't really want to do stuff on the client side...
    GlideDialogWindow.get().destroy();
    return true;
   
}

In the client script section, you see the two functions for the two buttons in the Jelly page.
‘onCancel()’ gets the ‘cancelled’ object that was created in the Jelly page and sets the value to ‘true’ and this stops the server-side code from running. This is followed by ‘GlideDialogWindow.get().destroy();’ which closes the pop-up.
validateApprover() doesn’t really do anything besides closing the pop-up, the ‘cancelled’ object keeps the value ‘false’, which means that the processing script is executed:

if (cancelled == "false" && trim(approver) != '') {
    // I do want to do things on the server side though...
    var gr = new GlideRecord('sysapproval_approver');
    gr.addQuery('sys_id', sid);
    gr.query();
    while (gr.next()) {  
         //set approver field to the new approver
        gr.approver = approver;
        gr.update();
        }
        response.sendRedirect("sysapproval_approver.do?sys_id=" + sid);
    }

else {
    // Just redirect to the un-updated approval record
    response.sendRedirect("sysapproval_approver.do?sys_id=" + sid);
}

So, If ‘cancelled’ is false and the value in the dropdown list is not empty, then the approval record can be updated.
For this the sysapproval_approver table is queried for the record where the sys_id is the one that we stored in ‘sid’.
After that the approver field is set to the value of the ‘approver’ we selected with the ui_reference and the record is updated.

Last but not least, the UI is redirected to the updated sysapproval_approver record.

This is the result: a basic pop-up Glide Dialog Window with a reference field where one can select the approver of choice:

I hope this article was useful for you. Please leave a comment if you have any questions!
.img[at].img

Leave a Reply