Client Script Debugged Part 3: Conditional Field Validation before saving the record

Rahul ThakurRahul Thakur
3 min read

When working with NetSuite forms, one of the most common requirements is conditional field validation - making sure a field is required only if another field has a specific value. In this post, part of my Client Script Debugged series, we’ll look at a real-world scenario where users struggled until a client script solved it.

The Real Problem

On the Customer Payment record, users must select a Payment Method. If they choose Check, then the Check # field becomes mandatory. However:

  • Users often forgot to enter the check number.

  • NetSuite by default didn’t enforce this dependency.

  • This led to missing payment references and reconciliation issues.

The requirement:
If Payment Method = Check, then Check # must be entered before saving the record.

Why saveRecord is the Right Entry Point

At first glance, some developers try to use fieldChanged or postSourcing. But these only warn the user as they interact with fields - they don’t prevent data from being saved.

The saveRecord entry point is designed for this exact purpose:

  • Runs when the user clicks Save.

  • Allows us to validate multiple fields together.

  • Can block saving until requirements are met.

The Solution: Client Script Example

Here’s a SuiteScript 2.0 Client Script that enforces this validation:

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 */
define([], function() {

    function saveRecord(context) {
        var currentRecord = context.currentRecord;

        var paymentMethod = currentRecord.getText({fieldId: 'paymentmethod'});
        var checkNumber = currentRecord.getValue({fieldId: 'checknum'});

        // If Payment Method is Check but Check # is missing
        if (paymentMethod === 'Check' && !checkNumber) {
            alert('Please enter a Check Number when Payment Method is Check.');
            return false; // Prevent save
        }
        return true; // Allow save
    }

    return {saveRecord};
});

Walkthrough of the Code

  1. We used the saveRecord entry point.

  2. Retrieved values of Payment Method (paymentmethod) and Check # (checknum).

  3. Checked if Payment Method = Check.

  4. If true but Check # is empty - show alert and block the save.

This ensures the record cannot be saved unless the rule is satisfied.

Debugging & Tips

  • Use alert() or console.log() for debugging while testing.

  • Field IDs (paymentmethod, checknum) may vary if your account uses customized forms → always confirm via form customization or SuiteScript records browser.

  • Avoid overusing alert(). For production, you can replace it with dialog.confirm() for better UX.

Wrapping Up

This scenario is a perfect example of how a simple client script can enforce critical business logic that NetSuite doesn’t handle out of the box.

With saveRecord, you ensure clean data entry and avoid reconciliation headaches later.


🔍 Client Script Debugged — Real Fixes from the Field

💡 This blog series dives into actual NetSuite client script issues I’ve encountered, how I solved them, and what you can learn from them.

📌 Tip: Bookmark this series to follow along as I share more real-world fixes with problem breakdowns, working code, and developer insights.

0
Subscribe to my newsletter

Read articles from Rahul Thakur directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rahul Thakur
Rahul Thakur