Hi. I have added a custom <input type="button"&...
# suitescript
w
Hi. I have added a custom <input type="button"> on a form inside a inline HTML-field. How would I enable that button to reference a client script function that resides in my ss2.0 script? the button has an onClick="myButton". I have tried added myButton: myButton in the return for the clientscript.
s
Is there a reason you are adding a button with an html field instead of adding the button natively with the form in the serverWidget module?
s
This function is written using NetSuite's pattern for creating button click events on Buttons. You can call this function to set the OnClick Function, as long as you update the CLIENT SCRIPT MODULE PATH, and, pass in the function name as a string (the same method used when adding the function name to a button.)
// Sample Usage:
var singleFolderFn = 'launchFolderWindow(\'' + folderId + '\')';
var singleFolderLinkText = '<a href="#" onclick="' + getClientOnclickJS(singleFolderFn) + '"> one file</a>';
/**
* This function calls the provided function name in the Client Script Module Path.
*
* @param {string} functionName - Same format as current addButton functionName
*/
function getClientOnclickJS(functionName) {
var CLIENT_SCRIPT_MODULE_PATH = '/SuiteScripts/folder/client-script-file';
var onclickBlob = "var rConfig = JSON.parse(&#39;{}&#39;);\
rConfig[&#39;context&#39;] = &#39;"+ CLIENT_SCRIPT_MODULE_PATH + "&#39;;\
var entryPointRequire = require.config(rConfig);\
entryPointRequire([&#39;"+ CLIENT_SCRIPT_MODULE_PATH + "&#39;],\
function (mod) {\
try { \
mod."+ functionName + "\
} catch(e){\
console.log(e);\
}\
}); return false;";
return onclickBlob.replace(/\s\s\s\s/gmi, '');
}
Of Course, it is possible that NetSuite will change the way they do this , and your script will fail... :)
m
This is not a good practice on native netsuite forms, but it’s something I’ve done on custom suitelet pages. You can tie these elements to functions in your ss2.0 client script (via native javascript’s addEventListener() or jQuery’s “.click()” callback within your client script.
👍🏼 1
w
I eventually did the right thing and used the native button-function in client scripts. The reason I wanted to do it in an inline-html-field was that I wanted to do a validation on a field before presenting the button. Now I'm doing the validation after the user press the button, so the button is visible all the time.