Making Item Rate field mandatory via scripting

The requirement was to make the rate field on item table to be mandatory when submitting an item. Used a client script “ValidateLine” function to show an error message while user submit the item line without rate. This works during create and edit of transaction record.

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

    /**
     * Validation function to be executed when sublist line is committed.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     *
     * @returns {boolean} Return true if sublist line is valid
     *
     * @since 2015.2
     */
    function validateLine(scriptContext) {
        try{
            var rec = scriptContext.currentRecord;
            if(scriptContext.sublistId == "item"){
                var rate= rec.getCurrentSublistValue({
                    sublistId:"item",
                    fieldId:"rate"
                })

                //check the condition whether the rate field selected or not
                if(!rate||rate<0){

                    alert("Please enter the rate value above zero");
                    return false;
                }else {
                    return true;
                }
            }

        }catch (e) {
            console.log("error@validateLine",e)
            return true;
        }

    }

    return {
        validateLine: validateLine
    };
    
});

Leave a comment

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