Reload the form after closing UI Page ran from UI Action

MaciejD
Tera Expert

I have a UI action that spawns a UI Page, which in turn runs a Script Include in order to create an approval. The functionality works, but there's a problem - I can't figure out how to reload the form after sending the data. It must be reloaded in order to hide the button, thus preventing the user from creating multiple approvals (only one is permitted).

UI Action code (onclick: loadConfirmDialog(), client checkbox set to true):

var approvalDialog;

function loadConfirmDialog() {
    var dialogClass = window.GlideModal ? GlideModal : GlideDialogWindow;
    approvalDialog = new dialogClass("ask_for_approval_dialog", false, 648, 250);
    approvalDialog.setPreference('sys_id', g_form.getUniqueValue());
    approvalDialog.setTitle(new GwtMessage().getMessage("Ask for approval"));
    approvalDialog.render();
}

if (typeof window == 'undefined')
    setRedirect();

function setRedirect() {
    //current.u_additional_approval = true;
    //current.state = 3;
    //current.u_hold_reason = 10;
    current.update();
    action.setRedirectURL(current);
}

UI Page client script:

function submitData() {
	var reference = gel('approval_approver_ref').value;
	var textArea = trim(gel('approval_reason').value);
	var sysId = gel('sys_id').value;

	if (!reference || !textArea) 
		return;

	//console.log("MD: " + JSON.stringify(reference) + " " + JSON.stringify(textArea) + " " + sysId);
	var ga = new GlideAjax('createApprovalFromUiAction');
		ga.addParam('sysparm_name', 'createApprovalFromUiAction');
		ga.addParam('sysparm_ritm', sysId);
		ga.addParam('sysparm_approver', reference);
		ga.addParam('sysparm_reason', textArea);
		ga.getXML();
		GlideDialogWindow.get().destroy();
		//gsftSubmit(null, g_form.getFormElement(), "ask_for_approval_ui_page");
		//action.setRedirect(current);
}

function cancelDialog() {
	GlideDialogWindow.get().destroy();
}

I tried multiple things, and my findings are as follows:
1. The setRedirect() function doesn't run, so anything inside it does nothing. Is it possible to make the UI Action do something after closing the UI Page?
2. The commented out "gsftSubmit" function kind of does the trick, but it inconveniences the user. It kicks the user back to the previous page and displays the "Action not authorized" error.
3. I tried the "location.reload()" function in the UI Page, but it either did nothing, or reloaded the page BEFORE it sent any data.

How can I make all of that work together?

2 ACCEPTED SOLUTIONS

@MaciejD 

it worked for me with this simple UI page, It reloaded the form

give this for dialog buttons and  don't give extra attributes

<g:dialog_buttons_ok_cancel cancel="return cancelDialog();" ok="return submitData();"/>

My UI Page and Output:

dialog window ui page.gif

In your client script set return true and see if it works

function submitData() {

    var reference = gel('approval_approver_ref').value;

    var textArea = trim(gel('approval_reason').value);

    var sysId = gel('sys_id').value;



    if (!reference || !textArea) 

        return;



    //console.log("MD: " + JSON.stringify(reference) + " " + JSON.stringify(textArea) + " " + sysId);

    var ga = new GlideAjax('createApprovalFromUiAction');

        ga.addParam('sysparm_name', 'createApprovalFromUiAction');

        ga.addParam('sysparm_ritm', sysId);

        ga.addParam('sysparm_approver', reference);

        ga.addParam('sysparm_reason', textArea);

        ga.getXML();

        GlideDialogWindow.get().destroy();

        return true;

}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

MaciejD
Tera Expert

Alright, I added this line

action.setRedirect(current);

just before these lines:

GlideDialogWindow.get().destroy();
//return true;

The commented out line doesn't seem to do anything. However, seems like it finally works.

View solution in original post

22 REPLIES 22

Ankur Bawiskar
Tera Patron
Tera Patron

@MaciejD 

why not have condition in that UI action so that it shows only when approval is not there?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

There is a condition that does exactly that. The thing is, the button will not disappear after the form is reloaded.

until after*

Ankur Bawiskar
Tera Patron
Tera Patron

@MaciejD 

action won't work in client script of UI page

try this

function submitData() {
    var reference = gel('approval_approver_ref').value;
    var textArea = trim(gel('approval_reason').value);
    var sysId = gel('sys_id').value;

    if (!reference || !textArea)
        return;

    //console.log("MD: " + JSON.stringify(reference) + " " + JSON.stringify(textArea) + " " + sysId);
    var ga = new GlideAjax('createApprovalFromUiAction');
        ga.addParam('sysparm_name', 'createApprovalFromUiAction');
        ga.addParam('sysparm_ritm', sysId);
        ga.addParam('sysparm_approver', reference);
        ga.addParam('sysparm_reason', textArea);
        ga.getXML();
        GlideDialogWindow.get().destroy();
        window.opener.location.reload();
}

function cancelDialog() {
    GlideDialogWindow.get().destroy();
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader