ServiceNow Client Script GlideOverlay: Open GlideOverlay Window to Submit or Update Another Record.js


Wed Nov 25 2020 21:05:50 GMT+0000 (Coordinated Universal Time)

Saved by

// 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
	// 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)) {
							console.warn(!(overlayG_form.getMissingFields().length > 0) + 'CLOSING');
						} else {
							console.warn(!(overlayG_form.getMissingFields().length > 0) + 'NOT CLOSING');
					}, 500);