Skip to content

Instantly share code, notes, and snippets.

@icerge
Last active April 17, 2024 12:35
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save icerge/4c29728c4a8cf0c04ef8bc8cf14519c1 to your computer and use it in GitHub Desktop.
Save icerge/4c29728c4a8cf0c04ef8bc8cf14519c1 to your computer and use it in GitHub Desktop.
Using modal windows in SN: GlideModal, confirm onSubmit

get confirmation in onSubmit client script

Have you ever got into this trap?

I'd like to make a confirmation with user that he/she are certain about submit/save/update action using a client script. This is the case. I can use a dumb confirm dialog which isn't cute at all. I can also try to use a GlideModal overlay with rich UI Page content. Why not?!

Well, because of

  • the fact that submit action has been already initiated from onSubmit function of Client Script perspective
  • and
  • because of javascript nature

it is not so straight-forward :)

Simple answer is that system doesn't wait for the GlideModel to render(). And I didn't find any proper way to force it.

Moreover, it is a bad design step. I think of a validation phase before a submit event is triggered. It's not facilitated by ServiceNow though. It's another story.

Still, to achieve the result I do the following - gain control by interuption and trigger event again if it is confirmed:

  1. check if confirmation has been already gained
  2. Render confirmation dialog.
  3. Stop submit by returning false
  4. trigger initial action in onPromptComplete handler script using gsftSubmit()
  5. set an attribute in g_scratchpad e.g. confirmed_actionXZY to indicate the confirmation has already been gained

Example follows.

	if(g_scratchpad._action_confirmed) {
		return true;
	}
	
	var dialog = new GlideModal('glide_modal_confirm', false, 300);
	dialog.setTitle(new GwtMessage().getMessage('Confirmation'));
	dialog.setPreference('body', new GwtMessage().format("Are you sure to save?"));
	dialog.setPreference('focusTrap', true);
	dialog.setPreference('onPromptComplete', doComplete);
	dialog.setPreference('onPromptCancel', doCancel);
	
	dialog.render();
	
	return false;
	
	function doComplete() {
		g_scratchpad._action_confirmed =  true;
		gsftSubmit(null, g_form.getFormElement(), g_form.getActionName());
	}
	
	function doCancel() {
	}

Additionally, consider a low order of client script like this one to avoid double load.

You can get it in a Client Script of OOTB instance.

Complete Batch Confirm List: /sys_script_client.do?sys_id=608f236747232200a03a19fbac9a7150

	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);
	}

A shortcut to open up a glide_confirm UI page in modal dialog.

The function code may be found in console context. Here is it.

function gsftConfirm(title, question, onPromptSave, onPromptCancel, onPromptDiscard) {
  var width, dialogClass = GlideDialogWindow;
  if (window.GlideModal) {
    dialogClass = GlideModal;
    width = 400;
  }
  var dialog = new dialogClass('glide_confirm', false, width);
  dialog.setTitle(title);
  dialog.setPreference('title', question);
  dialog.setPreference('onPromptSave', onPromptSave);
  dialog.setPreference('onPromptCancel', onPromptCancel);
  dialog.setPreference('onPromptDiscard', onPromptDiscard);
  dialog.render();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment