Using form.clientScriptFilePath in Suitelet Script

Using form.clientScriptFilePath in SuiteScript

form.clientScriptFilePath allows you to attach a client script to a form in NetSuite, making it easy to run client-side logic when users interact with that form.

Example Overview

Here’s a simple example to show how to use form.clientScriptFilePath.

  1. Create a Client Script
  2. Name the file clientScript.js and save it in the folder called SuiteScripts. This script will perform client-side validations.
javascript

Copy code
/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 */
define([], function() {
    function saveRecord(context) {
        var fieldValue = context.currentRecord.getValue({ fieldId: 'custfield_example' });

        if (!fieldValue) {
            alert('Field cannot be empty!');
            return false; // Prevent form submission
        }
        return true; // Allow form submission
    }

    return {
        saveRecord: saveRecord
    };
});
  1. Create a Suitelet
  2. In your Suitelet, add the following line to link the client script:
javascript

Copy code
/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 */
define(['N/ui/serverWidget'], function(serverWidget) {
    function onRequest(context) {
        if (context.request.method === 'GET') {
            var form = serverWidget.createForm({ title: 'Example Form' });

            // Add a custom field
            form.addField({
                id: 'custfield_example',
                type: serverWidget.FieldType.TEXT,
                label: 'Example Field'
            });

            // Link the client script
            form.clientScriptFilePath = 'SuiteScripts/clientScript.js'; // Adjust the path as needed

            form.addSubmitButton({ label: 'Submit' });
            context.response.writePage(form);
        } else {
            context.response.write('Form submitted successfully!');
        }
    }

    return {
        onRequest: onRequest
    };
});
  1. Deploy the Scripts
  • Upload both scripts to the File Cabinet.
  • Deploy the Suitelet to access it via a URL.

Conclusion

With form.clientScriptFilePath, you can easily attach client scripts to forms in NetSuite, enhancing user interaction with simple validations.

Leave a comment

Your email address will not be published. Required fields are marked *