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:

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

3 Responses to “Creating a pop-up using a GlideDialogWindow”

  1. jwawa Says:

    This seems like it would be a very good blog post, except that most of your code isn’t visible in this blog post, making it difficult to following along.

  2. Edwin van den Berg Says:

    Thank you jwawa, some SN code was playing games with the blogpost code. We used images now, instead of text.

  3. brian Says:

    In need of something like this, tried it out but why when trying to redirect, after selecting the new approvers name and hit submit, it comes up with record not found. Would you know why that would happen ?

Leave a Reply