Can you pass data from a User Event script to a Client Script in SS2.x?
Hey folks!
I have a User Event script that is calling showMessage() in a Client Script. So in the User Event script, it looks something like this:
var warning = context.form.addField({ id: 'custpage_custom_id', type: serverWidget.FieldType.INLINEHTML }); warning.defaultValue = "<script>NS.jQuery(function($){ require(['/SuiteScripts/pathToClientScript/myClientScript'], function(a){ a.showMessage();});});</script>";
And the Client Script is something like this:
function showMessage(data) { try{ var warningMessage = message.create({ title: "Warning: Lorem Ipsum", message: 'lorem ipsum . . . .', type: message.Type.WARNING }).show(); }catch(e){} }
I need some data, a variable, from the User Event to pass into the Client Script and be added to/concatenated into the message of the Client Script. I’ve tried a few different way, including by adding a new property to the object, but it seems SuiteScript doesn’t allow for that. I’ve also tried to get the current record in the Client Script and log it, but even when the Client Script runs (displays my warning message) the log doesn’t appear in the Execution Log in NetSuite. So perhaps a secondary question is: Is there a trick to logging in a Client Script? But really ,any insight in passing data from User Event to Client Script would be greatly appreciated!
Have you tried?
var myData = {foo:”bar”};
warning.defaultValue = “<script>NS.jQuery(function($){ require([‘/SuiteScripts/pathToClientScript/myClientScript’], function(a){ a.showMessage(‘” + JSON.stringify(myData) + “‘);});});</script>”;
Stringify! Thanks, that did it. I initially tried just passing my into the showMessage function, but didn’t stringify it and then went down other paths. Thanks for the help.
Hi! You can log a client script by using console.log, the messages will appear in the dev tools console from the browser. You may load the current record and get data from there.
Aah, yes, that makes sense, instead of using ‘N/log’ for a client script it’s just console log. So now I still need to pass custom data to the Client Script.
As of the 2018.2 release, you can set a message to display directly in the User Event script without having to add any client side script at all:
context.form.addPageInitMessage({ title: "Warning: Lorem Ipsum", message: 'lorem ipsum . . . .', type: message.Type.WARNING });
https://system.netsuite.com/app/help/helpcenter.nl?fid=section_1530198114.html
HI! you should be able to utilise the N/cache module for passing data between UE and Client script.
Hi Karl, thanks. How does N/cache work for a Client Script? I thought it was on server side only and when I try to inject/load N/cache into my client script I just get: Fail to evaluate script: {“type”:”error.SuiteScriptModuleLoaderError”,”name”:”MODULE_DOES_NOT_EXIST”,”message”:”Module does not exist: N/cache.js”,”stack”:[]} I can get and put cache values in the User Event, but not sure how to grab it in the Client. Thanks
The cache module is not available client side. Also the intended use of the module is to improve performance, not as a messaging mechanism between scripts. Notably, whilst you are able to specify a TTL, NetSuite makes no guarantee that the data will not be removed earlier, so even if it works 99% of the time it’s not best practice and can lead to hard to track bugs in the future.