Close Window from onSubmit Client Script?

Brian Treichel
Tera Guru

Hello,

We have a requirement that once an approval record has been updated (approved/rejected/comments/etc), the window should close if it comes from 'somewhere'. The way that I have been trying to do it is by adding 'sysparm_close_window=true' to the link that sends to the approval (from an email/portal/etc). In a display Business Rule, I look for that parameter and set a g_scratchpad variable 'close_window' to true. Now I am attempting to write an onSubmit Client Script that looks to see if 'close_window' is true, and if so, close the window. The problem that I am running into is that, because the onSubmit client script runs before the UI actions for Approve/Reject, the UI Action scripts never get triggered. I'm also having an issue that basic 'Save' loops the onSubmit script, and asks me if I want to leave the page without saving.

Business Rule:

g_scratchpad.close_window = gs.action.getGlideURI().toString().indexOf('sysparm_close_window=true') != -1;

OnSubmit Client Script:

if(g_scratchpad.close_window){

      g_form.submit();

      window.top.close();

    }

Anyone able to help out with this one?

Thanks,

Brian

1 ACCEPTED SOLUTION

Hi Berny, the getGlideURI() will not get the parameter because the page has already been navigated away from. I solved the issue by using AJAX in an onSubmit client script to have the form submit, then close. Didn't even need the UI Page . Thanks for the help though!


View solution in original post

8 REPLIES 8

Hi Berny, the getGlideURI() will not get the parameter because the page has already been navigated away from. I solved the issue by using AJAX in an onSubmit client script to have the form submit, then close. Didn't even need the UI Page . Thanks for the help though!


Could you share your script? I am curious on how you achieved that.


Hey MG - Just saw your comment. Here is the client script



function onSubmit() {


  var location = window.location.href;


  // we are coming from the portal...lets handle the action here


  if(location.indexOf('sysparm_portal=true') > -1){


  var ga = new GlideAjax('portalApprovalUtil');


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


  ga.addParam('sysparm_sys_id',g_form.getUniqueValue());


  ga.addParam('sysparm_comments',g_form.getValue('comments'));



  ga.addParam('sysparm_action', g_form.getActionName());


  ga.getXML(parseResponse);


  return false; // This will stop the processiong


  }


  function parseResponse(response) {


  var answer = response.responseXML.documentElement.getAttribute("answer");


  if(answer == 'success'){


  g_form.modified = false;


  window.close();


  }else{


  g_form.addErrorMessage('An error occured, please contact your administrator');


  }


  }


}


vjy4u4ever
Giga Contributor

on onSubmit client script just use this : setTimeout(function() { window.open(window.location, '_self').close(); }, 3000);


Hope this will help sombody else if your problem is solved