Skip to content

Instantly share code, notes, and snippets.

  • Save nancystodd/649fdde7e9169feb662238789fca5bf3 to your computer and use it in GitHub Desktop.
Save nancystodd/649fdde7e9169feb662238789fca5bf3 to your computer and use it in GitHub Desktop.
ServiceNow Client Script GlideOverlay: Open GlideOverlay Window to Submit or Update Another Record.js
// opens a new u_error_reporting record from an sc_task record
function openOverlay(overlayID, overlayTitle, iframeURI, height, width) {
// Instantiate the GlideOverlay
// Note: GlideOverlay extends GlideBox, which contains the close() method
var overlayWindow = new GlideOverlay({
id: overlayID,
title: overlayTitle,
iframe: iframeURI,
allowOverflowX: true,
closeOnEscape: true,
height: height,
width: width
});
overlayWindow.render();
// Get the element containing the iframe document.
var overlayDocument = overlayWindow._box.getElementsByClassName('gb_iframe')[0];
// Add a listener for when the document in the iframe finishes loading, and run some code when it does.
// Without this, our code would not have a "document" in which to execute, and wouldn't do anything.
overlayDocument.onload = function() {
// Get all of the "buttons" (button UI actions) in the document in the iframe -- that is, on the form in the GlideOverlay.
var overlayG_form = overlayDocument.contentWindow.g_form;
var buttonsInOverlayWindow = overlayDocument.contentWindow.document.getElementsByClassName('form_action_button');
// Iterate over each UI action button on the form.
for (var i in buttonsInOverlayWindow) {
// Standard check to make sure that the element (still) exists.
if (buttonsInOverlayWindow.hasOwnProperty(i)) {
// Adding an event listener to each UI action button. This executes a function when the button is clicked.
buttonsInOverlayWindow[i].addEventListener('click', function() {
// When a UI action button is clicked, we first call setTimeout. We pass in only a very small timeout timer, since
// even a timeout of 0 will cause the code passed into the setTimeout function to execute after all other scripts
// have finished running as a result of that click (such as the form being submitted).
setTimeout(function() {
// Finally, after all other code has executed as a result of the user clicking the UI action button, we call
// a function to check if any mandatory fields have not been filled out. If we're good, then we close the overlay.
if (!(overlayG_form.getMissingFields().length > 0)) {
overlayWindow.close();
console.warn(!(overlayG_form.getMissingFields().length > 0) + 'CLOSING');
} else {
console.warn(!(overlayG_form.getMissingFields().length > 0) + 'NOT CLOSING');
}
}, 500);
});
}
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment