Wednesday, May 29, 2019

Trigger a Function in Client-side Script in View Mode by Button Added Using User Event Script

By design client side scripts may be used in form in "edit" mode only. In case an action needs to be performed when viewing the record (e.g. by clicking a button) then the following setup and code will work.
In the following example simple alert() will represent client-side script.

First there is a client-side script "clientFunction.js" with the following function:
function clientFunction() {	alert('Client script triggered');	// other client-side code} 

This client-side script has to be uploaded into NetSuite account using following steps:

  1. Customization > Scripting > Scripts > New: Client script
  2. Name = Client Function
    ID = _client_function
    Script File = select "- New -" and upload the "clientFunction.js" file
    Save (no deployment is needed)
Then User Event script can contain code in beforeLoad function which adds a button and assign newly created script to run upon clicking:
function beforeLoadScript(type, form, request) {	form.setScript('customscript_client_function');	form.addButton('custpage_recalc', 'Button (Client)', 'clientFunction()' );}

This script should be uploaded into NetSuite and deployed on record where the button should be added and client-side script executed.

Expected result is a new button on a form in view mode, alert with "Client script triggered" message is shown by clicking it.

Also, for client scripts that needs to have parameter passed to it, use the example code below as reference.

Client Script:

function clientFunction(message) {	alert(message);	// other client-side code} 

User Event Script:

function beforeLoadScript(type, form, request) {	var message = 'Client script triggered';	form.setScript('customscript_client_function');	form.addButton('custpage_recalc', 'Button (Client)', 'clientFunction("'+ message +'")' );}


DISCLAIMER: The sample code described herein is provided on an "as is" basis, without warranty of any kind, to the fullest extent permitted by law. Netsuite Inc. does not warrant or guarantee the individual success developers may have in implementing the sample code on their development platforms or in using their own Web server configurations.

Netsuite Inc. does not warrant, guarantee or make any representations regarding the use, results of use, accuracy, timeliness or completeness of any data or information relating to the sample code. Netsuite Inc. disclaims all warranties, express or implied, and in particular, disclaims all warranties of merchantability, fitness for a particular purpose, and warranties related to the code, or any service or software related thereto.

Netsuite Inc. shall not be liable for any direct, indirect or consequential damages or costs of any type arising out of any action taken by you or others related to the sample code

 

No comments:

Post a Comment