NetSuite / SuiteScript: Client Side Form Validation & More

Published on July 23, 2019

Using Client Scripts, you can easily add client-side form validation and manipulation to NetSuite forms. For example, you can immediately validate a field's value when it changes, apply form-level validation when a form is submitted, conditionally hide and show fields, and much more.

Included below is a simple SuiteScript that demonstrates a few of things that you can do with Client Scripts. To experiment with it, apply it to Employee records in a sandbox account. When an employee form loads, you'll see an alert. Try changing an employee's job - and if you change it to "Accountant" notice how the Notes field is hidden. Then change the employee's Phone number to "5555555555" and submit the form.

/**
 * @NApiVersion 2.0
 * @NScriptType ClientScript
 */
define([], function() {

	/*
	
		This is just an experiment with form validation, conditionally
		hiding fields, etc.	To test it, deploy it against the Employee 
		record type.
	
	*/
	
	
    return {
        fieldChanged: fieldChanged,
        pageInit: pageInit,
        saveRecord: saveRecord
    };		
		

    function fieldChanged( context ) {
    
    	// Shows an alert if the Job field is changed.
    	// If the job is changed to "Accountant," then the notes field is hidden.

        if ( context.fieldId == 'job' ) {
        
        	// Get the text value if the selected job.
        	var jobValue = context.currentRecord.getText( { fieldId: 'job' } );
        
        	alert( 'You changed the job to: ' + jobValue );
        	
        	// Get the comments field (object).
        	var commentsField = context.currentRecord.getField( { fieldId: 'comments' } );
        	
        	if ( jobValue == 'Accountant' ) {
        		commentsField.isDisplay = false; 
        	} else {
        		commentsField.isDisplay = true; 
        	}

        }
        
    }



    function pageInit(context) {
    
    	// Shows an alert when the page loads.
        alert( 'Welcome to the form.' );
           
    }


    function saveRecord(context) {
    
    	// Executed after the submit button is pressed, but before the form is submitted.
    	// If the phone is "5555555555," an alert is shown, and the form is rejected.

		if ( context.currentRecord.getText('phone') == '5555555555' ) {
			alert ('Sorry, but that phone number is not allowed.');
			return false;
		}

        return true;
    }

    
});

Comments? Questions? Feel free to reach out to me.