Within a NetSuite client script, it happens that we need to navigate to a different page (e.g. with
window.location.href = '/newurl'
).
If the action of redirecting to the new URL is triggered when the user is editing a NetSuite record, or working on a suitelet with fields he can edit, and he changed any of the fields values on the page (or the script itself did it on page init), then this redirect action will cause the page to show a confirmation dialog to the user, warning that leaving the page will potentially cause the changes to be lost.
This is the behaviour we want in some case, but in some other case maybe this is bothering the UX we have in mind, just putting in the process an additional pointless click to warn about something the script has already taken care of.
I decided to write this post because I saw this issue again lately when a NetSuite consultant was showing me a suitelet script that he developed, with a client script that was adding / changing an URL parameter every time the user was changing a value of a field in a filters group. This was causing this dialog to pop up every time the user was changing some filter, but it was pointless because the client script was actually storing that value in the URL as a query string parameter (and I suppose then reading it while rendering the suitelet server side).
There is a way - when needed - to
disable this confirmation dialog, which is adding the following line of code just before the call that triggers the URL change:
window.onbeforeunload = null;
This will cause the function that asks for a confirmation (which is added by default in any NetSuite page) to be removed, thus not invoked anymore.
This method should be quite safe, because it works on the standard browser event window.onbeforeunload, no NetSuite reverse engineering is involved here.
When it comes to
just adding or changing URL query string parameters, there is also another possibility: the NetSuite unofficial API nlapiChangeCall, which does exactly that, and doesn't need to take care of suppressing the confirmation dialog.
If for example you have this URL
https://system.netsuite.com/app/site/hosting/scriptlet.nl?script=10&deploy=1&custparam_customer_id=15, and within a client script you want to redirect to the same page while changing the value of custparam_customer_id to 20 and adding a new parameter custparam_vendor_id set to 18, then it's as simple as it follows:
nlapiChangeCall({'custparam_customer_id':'20', 'custparam_vendor_id':'18'});
This will cause the URL to change without causing any confirmation to pop up. The only parameter of the function is an associative array of query string parameters and the values to be set.
I found this I guess back in 2011 or 2012 while trying to avoid that evil dialog, and I just checked, it's still there. So I suppose it's quite reliable to use it in a client script, although it's essential to consider that there is no guarantee that it will continue working, so it should be used with proper error handling, and if it is within a business process it should have a fallback solution (which can be for example a combination of a function for parsing the URL + adding the parameters + redirecting together with the solution explained above, with the window.onbeforeunload trick).
It looks like a SuiteScript 1.0 API, but since it's used client side it's available also for SuiteScript 2.0 client scripts.
alessio |
October 23, 2019