Client Script for Making the Custom Subtab Entry Mandatory in Customer Payment

Business Requirement

When entering a “Customer Payment” record, certain fields within a custom subtab must be completed before submission. This validation ensures that financial allocations are accurate and prevents discrepancies in payment records. If any required data is missing, the system should alert the user and prevent record submission until the values align.

Solution

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


    function saveRecord(context) {
        var currentRecord = context.currentRecord;
        if (currentRecord.type === "customerpayment") {
            var getLineCount = currentRecord.getLineCount('recmachcustrecord_vs_paymentsalesrep_pa');
            var totalPay = currentRecord.getValue('payment');
            var splitted = 0;
            
            if (getLineCount && getLineCount > 0) {
                for (var i = 0; i < getLineCount; i++) {
                    splitted += Number(currentRecord.getSublistValue({ 
                        sublistId: 'recmachcustrecord_vs_paymentsalesrep_pa', 
                        line: i, 
                        fieldId: 'custrecord_vs_sr_cust_amount' 
                    }));
                }
                if (totalPay != splitted) {
                    alert("The total payment of the sales reps does not match with the total payment");
                    return false;
                }
                return true;
            } else {
                alert("Please add the required subtab details to proceed");
                return false;
            }
        }
    }


    return {
        saveRecord: saveRecord
    };
});

Leave a comment

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