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

Also I have that condition - it works AS SOON AS the form reloads. That's why I want it to reload after sending data!

@MaciejD 

to use processing script you need to use <g:ui_form> in your UI page html

if that form is submitted then only processing script is executed

please share your complete UI page HTML here

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

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">

<g:evaluate var="jvar_sys_id" expression="RP.getWindowProperties().get('sys_id')" />

<g:ui_form>
<input type="hidden" name="sys_id" id="sys_id" value="${jvar_sys_id}" />
  <div class="row form-group">
    <div class="col-md-2 text-right">
      <label for="approval_approver">Approver<span style="color:red;font-size:x-large;">*</span></label>
    </div>
    <div class="col-md-10">
      <g:ui_reference 
				name="approval_approver_ref" 
				id="approval_approver"
				label="Approver" 
				query="u_technical_user=false^active=true^ORDERBYname" 
				completer="AJAXTableCompleter" 
				table="sys_user"  
				mandatory="true"
			/>
    </div>
  </div>

<div class="row form-group">
    <div class="col-md-2 text-right">
      <label for="approval_reason">Reason for Approval <span style="color:red;font-size:x-large;">*</span></label>
    </div>
    <div class="col-md-10">
	<textarea class="form-control" name="approval_reason" id="approval_reason" rows="10"></textarea>
    </div>
  </div>

  <div class="fixed-bottom">
    <g:dialog_buttons_ok_cancel 
			ok_id="ok_btn" 
			ok="return submitData()" 
			ok_type="button" 
			ok_style_class="btn btn-primary" 
			cancel_type="button" 
			cancel_id="cancel_btn" 
			cancel_style_class="btn btn-default" 
			cancel="return cancelDialog()"
		/>
		</div>

</g:ui_form>
</j:jelly>

@MaciejD 

did you add logs and see if processing script ran?

I could see you already using <g:ui_form> so processing script should work

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

I added gs.info("MD: Processing script ran"); and seems like it doesn't run. It's not in the logs.