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.
- Create a Client Script
- Name the file
clientScript.jsand 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
};
});
- Create a Suitelet
- 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
};
});
- 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.