GlideModal - Client
The GlideModal API provides methods for displaying a content overlay, known as a modal. Modals are interactive windows that appear above a page and close when a user takes an action. You can use a modal to display information, ask questions, or perform actions.
Use the GlideModal methods in scripts anywhere that you can use client-side JavaScript. These methods are most often called from a UI action with the Client check box selected.
- Static text
- Dynamic text
- Forms
- Images
- Buttons
- Get an existing modal.
- Create modal content from a UI page or from passed HTML.
- Set the title in the modal.
- Set the width of the modal.
- Get and set preferences.
- Switch modal views.
var dialog = new GlideModal("UI_dialog_name");
//Set the dialog title
dialog.setTitle('Show title');
//Set the dialog width
dialog.setWidth(550);
//Display the modal
dialog.render();function cancelDialog(){
var dialog = new GlideModal('cancelTask');
//Sets the dialog title
dialog.setTitle('Cancel Task');
//Set up valid custom HTML to display
dialog.renderWithContent('<div style="padding:15px"><p>What action do you want to take?</p>
<p><select name="cancellation" id="taskCancellation" class="form-control">
<option value="cancelOnly" role="option">Cancel this task but keep the requested item open</option>
<option value="cancelAll" role="option">Cancel this and all other tasks, closing the requested item</option>
</select></p><div style="padding:5px;float:right"><button style="padding:5px;margin-right:10px"
onclick="window.changeTaskAction(this.innerHTML,jQuery(\'#taskCancellation\').val())"
class="btn btn-default">Abort</button><button style="padding:5px" class="btn btn-primary"
onclick="window.changeTaskAction(this.innerHTML,jQuery(\'#taskCancellation\').val())">Cancel Task</button></div></div>');
//Use the windows object to ensure the code is accessible from the modal dialog
window.changeTaskAction = function(thisButton, thisAction){
//Close the GlideModal dialog window
dialog.destroy();
//Submit to the back-end
if(thisButton=='Cancel Task'){
if(thisAction=="cancelAll"){
g_form.setValue('state',4);//Closed Incomplete -- will close the Requested Item and all other open tasks
}else{
g_form.setValue('state',7);//Closed Skipped -- will only close this task
}
//Regular ServiceNow form submission
gsftSubmit(null, g_form.getFormElement(), 'cancel_sc_task');
}
};
return false;//Prevents the form from submitting when the dialog first load
}GlideModal - GlideModal(String id, Boolean readOnly, Number/String width)
Creates an instance of the GlideModal class.
| Name | Type | Description |
|---|---|---|
| id | String | Name of theUI page to load into the modal. You can also specify either the Table: UI Page [sys_ui_page] |
| readOnly | Boolean | Optional. Flag that indicates whether the content in the modal is read-only. Valid values:
Default: false |
| width | Number or String | Optional. Width of the modal in pixels or the modal CSS class. If a pixel width is passed, it aligns the specified width with the corresponding CSS class. Possible modal CSS classes:
Default: modal-md Maximum width: 900 pixels 注: You can also set the modal width using the setWidth() method. |
The following code example shows how to create a GlideModal object using an existing UI page.
var dialog = new GlideModal('UI_dialog_name');
//Set the dialog title
dialog.setTitle('Show title');
//Set the desired preferences
dialog.setPreference('table', 'task');
dialog.setPreference('name', 'value');
//Opens the dialog
dialog.render();
The following code example shows how to create a GlideModal object using the glide_modal_confirm file.
var dialog = new GlideModal('glide_modal_confirm', true, 300);
dialog.setTitle(new GwtMessage().getMessage('Confirmation'));
dialog.setPreference('body', new GwtMessage().format("This will complete all update sets in the batch. Continue changing state to complete?"));
dialog.setPreference('focusTrap', true);
dialog.setPreference('onPromptComplete', doComplete);
dialog.setPreference('onPromptCancel', doCancel);
dialog.render();
function doComplete() {
callback(true);
}
function doCancel() {
callback(false);
}
GlideModal - destroy()
Closes the current modal.
| Name | Type | Description |
|---|---|---|
| None | None | None |
| Type | Description |
|---|---|
| None | None |
The following code example shows how to use the destroy() method to close a modal.
function cancelDialog(){
var gmod = new GlideModal('cancelTask');
//Sets the dialog title
gmod.setTitle('Cancel Task');
//Set up valid custom HTML to be displayed
gmod.renderWithContent('<div style="padding:15px"><p>What action do you want to take?</p>
<p><select name="cancellation" id="taskCancellation" class="form-control">
<option value="cancelOnly" role="option">Cancel this task but keep the requested item open</option>
<option value="cancelAll" role="option">Cancel this and all other tasks, closing the requested item</option>
</select></p><div style="padding:5px;float:right"><button style="padding:5px;margin-right:10px"
onclick="window.changeTaskAction(this.innerHTML,jQuery(\'#taskCancellation\').val())"
class="btn btn-default">Abort</button><button style="padding:5px"
class="btn btn-primary" onclick="window.changeTaskAction(this.innerHTML,jQuery(\'#taskCancellation\').val())">
Cancel Task</button></div></div>');
//Use the windows object to ensure our code is accessible from the modal dialog
window.changeTaskAction = function(thisButton, thisAction){
//Close the glide modal dialog window
gmod.destroy();
//Submit to the back-end
if(thisButton=='Cancel Task'){
if(thisAction=="cancelAll"){
g_form.setValue('state',4); //Closed Incomplete -- closes the Requested Item and all other open tasks
}else{
g_form.setValue('state',7); //Closed Skipped -- only closes this task
}
//Regular ServiceNow form submission
gsftSubmit(null, g_form.getFormElement(), 'cancel_sc_task');
}
};
return false; //Prevents the form from submitting when the dialog first load
}
The following code example shows how to use GlideModal.get().destroy() to close a modal in a client script. The following button should be declared in the UI page HTML: <button
onclick="closeMe()">close</button>.
function closeGlideModal() {
try {
GlideModal.get().destroy();
} catch (err) {
console.warn("closeGlideModal ERROR: " + err.message);
var x = document.getElementById('THE_NAME_OF_YOUR_UI_PAGE' + '_closemodal');
if (x) {
x.click();
} else {
console.warn("No 'X' close button found!");
}
}
}
function closeMe() {
setTimeout(function() {
closeGlideModal();
}, 100);
}
GlideModal - get(String id)
Returns the GlideModal object identified by the specified UI page name.
Use this method to obtain the GlideModal object to use in other GlideModal operations such as, GlideModal.get().destroy().
| Name | Type | Description |
|---|---|---|
| id | String | Name of theUI page associated with the modal. You can also specify either the Table: UI Page [sys_ui_page] |
| Type | Description |
|---|---|
| GlideModal | Requested GlideModal object. |
This example shows how to use the get() method to obtain the modal that you want to close using the destroy() method.
// If the modal was initially created like this:
var dialog = new GlideModal("glide_modal_confirm");
dialog.render();
// Code using the modal
...
// Now use the get() and destroy() methods to close the modal
var glideModal = new GlideModal().get("glide_modal_confirm");
glideModal.destroy();
// You can also code it like this:
// GlideModal.prototype.get('glide_modal_confirm').destroy();
GlideModal - getPreference(String name)
Returns the value of the specified preference (property).
Invoking actions that create the modal typically also create the necessary preferences for the modal using the GlideModal - setPreference(String name, String value) method. The UI page client script can then consume these preferences using this method.
| Name | Type | Description |
|---|---|---|
| name | String | Name of the preference value to retrieve. This value must have previously been set on the modal using the setPreference() method. |
| Type | Description |
|---|---|
| String | Specified preference's value. |
This example shows a simple case of setting a preference and then retrieving that preference from a specified modal.
var dialog = new GlideModal('UI_dialog_name');
//Sets the dialog title
dialog.setTitle('Modal title');
//Sets the value of the preference table
dialog.setPreference('table', 'incident');
//Gets the value of the preference table
var title = dialog.getPreference('table');
GlideModal - render()
Renders the UI page specified when the API was instantiated in the modal. You must call this method after you define the modal for it to appear in the UI.
Call this method when you use a UI page to generate the content in your modal. If you want to display HTML within a modal, call GlideModal - renderWithContent(String html) to render the modal.
| Name | Type | Description |
|---|---|---|
| None |
| Type | Description |
|---|---|
| void | None |
The following code example shows how to instantiate a GlideModal object using the UI page "UI_dialog_name", set the title and width of the modal, and then display the modal in the UI (render).
var dialog = new GlideModal("UI_dialog_name");
//Set the dialog title and width
dialog.setTitle('Show title');
dialog.setWidth(550);
//Display the dialog
dialog.render();
GlideModal - renderWithContent(String html)
Displays a modal with the specified string-based HTML content.
Use the renderWithContent() method in lieu of the render() method when deriving the modal content from HTML.
| Name | Type | Description |
|---|---|---|
| html | String | HTML content to display in the modal. |
| Type | Description |
|---|---|
| void | None |
This code example shows how to display a modal that is constructed using the passed HTML string which contains a list of choices that the user can select from.
function cancelDialog(){
var dialog = new GlideModal('cancelTask');
// Sets the dialog title
dialog.setTitle('Cancel Task');
// Set up valid custom HTML to display
dialog.renderWithContent('<div style="padding:15px">
<p>What action do you want to take?</p>
<p><select name="cancellation" id="taskCancellation" class="form-control">
<option value="cancelOnly" role="option">Cancel task but keep requested item open
</option>
<option value="cancelAll" role="option">Cancel this and all other tasks, closing the
requested item</option></select></p><div style="padding:5px;float:right"><button
style="padding:5px;margin-right:10px onclick="window.changeTaskAction(this.innerHTML,
jQuery(\'#taskCancellation\').val())" class="btn btn-default">Abort</button>
<button style="padding:5px" class="btn btn-primary"
onclick="window.changeTaskAction(this.innerHTML,jQuery(\'#taskCancellation\').val())">
Cancel Task</button></div></div>');
// Use the windows object to ensure the code is accessible from the modal dialog
window.changeTaskAction = function(thisButton, thisAction){
// Close the GlideModal dialog window
dialog.destroy();
// Submit to the back-end
if(thisButton=='Cancel Task'){
if(thisAction=="cancelAll"){
// Closed Incomplete -- will close the Requested Item and all other open tasks
g_form.setValue('state',4);
}else{
// Closed Skipped -- will only close this task
g_form.setValue('state',7);
}
//Regular ServiceNow form submission
gsftSubmit(null, g_form.getFormElement(), 'cancel_sc_task');
}
};
// Prevents the form from submitting when the dialog first loads
return false;
}
GlideModal - setPreference(String name, String value)
Sets the specified field on the current form to the specified value.
| Name | Type | Description |
|---|---|---|
| name | String | Name of the form field to update. If this field does not exist on the current form, the request is ignored. |
| value | String | Value to store in the specified form field. |
| Type | Description |
|---|---|
| void | None |
The following code example shows how to set the table preference to 'task' and the name preference to 'value'.
var dialog = new GlideModal('UI_dialog_name');
//Set the dialog title
dialog.setTitle('Show title');
//Set the desired preferences
dialog.setPreference('table', 'task');
dialog.setPreference('name', 'value');
//Opens the dialog
dialog.render();
GlideModal - setPreferenceAndReload(Object properties)
Sets the specified preferences and then reloads the modal.
| Name | Type | Description |
|---|---|---|
| properties | Object | Name-value pairs to set as preferences in the format. |
| Type | Description |
|---|---|
| void | None |
In this example, the setPreferenceAndReload() function sets preferences and reloads the modal with updated preferences. The modal renders with the initial preferences, and after 5 seconds the setPreferenceAndReload() is called and reloads the modal with the updated preferences.
// UI Page to render in the GlideModal is 'glide_modal_confirm'
var glideModal = new GlideModal("glide_modal_confirm", false, 600);
glideModal.setTitle(getMessage("Confirmation"));
glideModal.setPreference("body", getMessage("Are you sure you want to reject this deal registration? This action cannot be undone."));
glideModal.setPreference('buttonLabelComplete', getMessage("Reject"));
glideModal.setPreference('buttonLabelCancel', getMessage("Cancel"));
glideModal.render();
var preferences = {
"body": "THIS IS THE UPDATED BODY PREFERENCE",
"buttonLabelComplete": "NEW REJECT",
"buttonLabelCancel": "NEW CANCEL"
};
setTimeout(function() {
glideModal.setPreferenceAndReload(preferences);
}, 5000);
GlideModal - setTitle(String title)
Sets the title of the modal.
| Name | Type | Description |
|---|---|---|
| title | String | Text to display in the title of the modal. |
| Type | Description |
|---|---|
| void | None |
The following code example shows how to set the modal title to Table to update.
var dialog = new GlideModal('UI_dialog_name');
//Sets the dialog title
dialog.setTitle('Table to update');
dialog.setPreference('table', 'task');
dialog.setWidth(550);
//Opens the dialog
dialog.render();
GlideModal - setWidth(Number/String width)
Sets the width of the modal.
You can also set the width of a modal when you first instantiate the API using the GlideModal - GlideModal(String id, Boolean readOnly, Number/String width) method.
| Name | Type | Description |
|---|---|---|
| width | Number or String | Width of the modal in pixels or the modal CSS class. If a pixel width is passed, it aligns the specified width with the corresponding CSS class. Possible modal CSS classes:
Maximum width: 900 pixels |
| Type | Description |
|---|---|
| void | None |
The following code example shows how to set the modal width to 550 pixels.
var dialog = new GlideModal('UI_dialog_name');
//Sets the dialog title
dialog.setTitle('Show title');
dialog.setPreference('name', 'value');
dialog.setWidth(550);
//Opens the dialog
dialog.render();
GlideModal - switchView(String newView)
Change the view and reload the modal.
| Name | Type | Description |
|---|---|---|
| newView | String | View to use. |
| Type | Description |
|---|---|
| void | None |
GlideModal - updateTitle(
Updates the title specified using GlideModal setTitle() method.
You must always first call GlideModal - setTitle(String title) to specify the Modal title using updateTitle() to commit the change.
| Name | Type | Description |
|---|---|---|
| None |
| Type | Description |
|---|---|
| None |
The following example demonstrates how to use set and update a modal's title by calling setTitle() and updateTitle() respectively.
var modal = new nowapi.GlideModal();
modal.renderWithContent('TEST CONTENT');
setTimeout(function() {
modal.setTitle("TEST setTitle() AND updateTitle() METHODS");
// Only updates the 'title' prop, updateTitle() must be called to see change
modal.updateTitle();
}, 3000);