Running client script in view mode
In order to run clientscript in view mode you need to modify the form on BeforeLoad using a UserEvent script and attach a Clientscript file to the form using clientScriptFieldId function. Here’s some examples on how to do it:
Suitescript 1.0:
1 2 3 4 5 |
function beforeLoad(type, form) { if (nlapiGetContext().getExecutionContext() == 'userinterface') { form.setScript('customscript_yourscriptid');//<< SET THIS TO YOUR SCRIPT ID } } |
Here’s what you would have in your 1.0 Clientscript customscript_yourscriptid file:
Note: The Clientscript record doesn’t need to be deployed, you don’t need to fill the function names either. As long as it exists the logic will run.
1 |
alert('Test succesful'); |
Suitescript 2.0:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
var SEARCHMODULE; /** *@NApiVersion 2.x *@NModuleScope Public *@NScriptType UserEventScript */ define(['N/search'], runUserEvent); function runUserEvent(search) { SEARCHMODULE = search; var returnObj = {}; returnObj.beforeLoad = beforeLoad; return returnObj; } function beforeLoad(scriptContext) { if (scriptContext.type == scriptContext.UserEventType.VIEW) { scriptContext.form.clientScriptFileId = 163452;//<< SET THIS TO YOUR SCRIPT ID //scriptContext.form.clientScriptModulePath = 'SuiteScripts/myClientscript.js';//<< ALTERNATIVELY YOU CAN USE THE PATH scriptContext.form.addButton({ id: 'custpage_some_button', label: 'Some Button', functionName: 'someFunction' }); } } |
Here’s what you would have in your 2.0 Clientscript file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
var SEARCHMODULE; define(['N/search'], runClientscript); function runClientscript(search){ SEARCHMODULE = search; //*********** HELPER FUNCTIONS *********** function someFunction() { alert('Clientscript someFunction triggered'); return; } var returnObj = {}; returnObj.someFunction = someFunction; return returnObj; } |
Suitescript 2.0 InlineHTML Hack:
If you need to run your code without having to click a button you can try the the following code. This code injects a script into the form to hack the DOM. In other words it’s a hack.
The following code is a hack. Use carefully.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
/** *@NApiVersion 2.x *@NModuleScope Public *@NScriptType UserEventScript */ define([], runUserEvent); function runUserEvent() { var returnObj = {}; returnObj.beforeLoad = beforeLoad; return returnObj; } function beforeLoad(scriptContext) { if (scriptContext.type == scriptContext.UserEventType.VIEW) { var field = scriptContext.form.addField({ id: 'custpageinjectcode', type: 'INLINEHTML', label: 'Inject Code' }); field.defaultValue = "<script>alert('Hello')</script>"; //This is where you would type your script } } |