gsftSubmit wait for dialog to close

Mike Edmonds1
Kilo Expert

All, 

I have a dialog window that renders successfully. I need to, wait till the user enters some data and submits the dialog window, then update the form. When I try the gsftSubmit(), the dialog render, the script keeps running, and the record is not updated after the dialog box is submitted. I have also entertained using an onSubmit() client script. Again the dialog is rendered but promptly closes with out allowing any data to be entered when the script gets to the g_form.submit() function. Any help is greatly appreciated.

5 REPLIES 5

Rahul Jain11
Kilo Guru

Hi,

You can wait for the dialog window to close before it execute the gsftSubmit().

The better way to achieve this is to write your logic in the UI Page which is used to open the dialog window based on the user action.

You can write the client script or server side script in the "processing script" of UI page.

 

You can look at the example for the same here: UI Page

 

Thanks,

Rahul, 

 

The end goal would be something like this.

  1. Interact with form / enter or change information on the form
  2. Click UI action such as Save and Close
  3. Render dialog
  4. Input data to daialog window
  5. When the dialog is closed the form is submitted from the client and commits the changes to the server

I need the record to update on the client to grab any new data and comit to the server. The dialog is used to capture information for a custom time tracking application, I am gathering information form the current record to populate fields on it. It seems the Processing Script can populate data back to the record on the server and update it there, which is why I was hoping to use gsftSubmit. Can you think of anything else that may work?

Tristan Elmore
Tera Expert

I realize this is an older post, but in case someone comes across this and is trying to find a way to use a GlideModal in an onSubmit client script and you're running into issues where it's either looping or submitting the record before the user has a chance to confirm/validate etc on the modal window, try this:

 

	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() {
	}


Full credit to icerge on GitHub who came up with this one. I spun around with different ways to try to use gsftSubmit to get this to work. 
Using modal windows in SN: GlideModal, confirm onSubmit · GitHub

Hi @Tristan Elmore, I'm following you on the script code, but where to put it?

 

I tried in the UI Action of the triggering button and not honored.

For my use case, I have a UI Action Form button on the Incident table. 

 

User clicks button and the modal pops the default form view for a custom table.

 

When the user clicks submit for the custom table, is when I want to trap the event and do what I need - in my case window.open and pass a custom built url to another system - yes I can go API call and all that jazz, but my preference is to go old skool with just a simple UI/UX open in a blank tab and allow the user to get an idea of what is going on in the other system. But I digress.

 

Where's the special sauce? Or am I as usual overthinking something?

I'm a bit new to ServiceNow, but 30 yrs in programming so, I have not figured out the page lifecycle yet of ServiceNow's architecture.

Here's my code if so desired.

function showGenerateDocForm() {
    if (g_scratchpad._action_confirmed) {
        return true;
    }

    console.log("button clicked to generate an agreement");
    //alert("Button Clicked to Generate an Agreement 🤯 \n" + g_form.getUniqueValue());

    var modal = new GlideModalForm('Document Generation Request', 'u_generate_document');
    modal.setSysID('-1');
    //modal.setPreference('sysparm_view', 'default');
    modal.setPreference('sysparm_query', 'u_incident=' + g_form.getUniqueValue());
    //modal.render();
    //return false;

    // swing and a miss
    //var submitButton = document.querySelector("#sysverb_insert_bottom");
    //submitButton.addEventListener("click", function() {
    //    window.open("https://google.com", "_blank");
    //});

    //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?"));
    modal.setPreference('focusTrap', true);
    modal.setPreference('onPromptComplete', doComplete);
    modal.setPreference('onPromptCancel', doCancel);
    modal.render();
    return false;
}

function doComplete() {
    alert("nailed it");
    g_scratchpad._action_confirmed = true;
    gsftSubmit(null, g_form.getFormElement(), g_form.getActionName());
}

function doCancel() {}