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

larstange
Mega Sage

Hi



If you just need to get a parameter added to the page URL, you can do this in your client script



var myparm = getParmVal('sysparm_close_window');



if(myparm == 'true'){  


      g_form.submit();  


      window.top.close();  


    }



function getParmVal(name){


  var url = document.URL.parseQuery();


  if(url[name]){


  return decodeURI(url[name]);


  }


  else{


  return;


  }


}


bernyalvarado
Mega Sage

Hi Brian (briantreichel,



Here goes an idea. Within the business rule you can include a gs.redirect which points to a ui page which script closes the window.



I hope this helps



Thanks,


Berny


There's an idea! I think I'll try that out.



Edit: So the Business Rule would need to be an OnSubmit() 'after update', however the gs.action.getGlideURI() only seems to get the page URL in 'display' Business Rules. I don't think g_scratchpad would work 'server-to-server' (from the 'display' to the 'after update' BRs), so how would can I get the parameter server-side otherwise (preferably without writing data to the record to trigger the business rule)?



Thanks, Berny!


Hi Brian,



You will need to do an after Business Rule that should do the following:



a) lookup if the business rule is triggered from a page where the window should be closed. You can know from which page the business rule was triggered by using gs.action.getGlideURI()


b) if it's coming from a page where the window is closed then you use a gs.redirect to navigate to a ui page which will have your client javascript to do a simple window.close()



Please let me know if you have any follow up questions. I hope this helps!



Thanks,


Berny