Client Script For Validating the Date

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
/*************************************************************************************
 ***********
 * Mohd Ali Mohideen Supermarket LLC-UAE-NS Software project
 *
 * MAMS-156
 *
 *
 *************************************************************************************
 ***********
 *
 *
 * Author: Jobin and Jismi IT Services LLP
 *
 * Date Created : 18-April-2023
 *
 * Description: This script is used for validating(date) the custom record vendor credit limit info
 *
 * REVISION HISTORY
 *
 * @version 1.0 MAMS-156 : 18-April-2023 : Created the initial build by JJ0177
 *
 *
 *************************************************************************************
 **********/
define(['N/currentRecord',],
    /**
     * @param{currentRecord} currentRecord
     */
    function (currentRecord)
    {
        function pageInit(scriptContext)
        {
            try
            {
                var form = scriptContext.currentRecord;
                var fromDate = form.getValue({fieldId: 'custrecord_jj_fromdate_mams156'});
                var toDate = form.getValue({fieldId: 'custrecord_jj_todate_mams156'});
                if(fromDate && toDate && fromDate > toDate)
                {
                    form.setValue({
                        fieldId: 'custrecord_jj_todate_mams156',
                        value: null
                    });
                    alert("Record cannot be saved; 'To Date' should be greater than or equal to 'From Date'.");
                    return false; // prevent record save
                }
                return true;
            }
            catch(error)
            {
                log.debug("error @pageInit",error)
            }
        }

        /**
         * Function to be executed when field is changed.
         *
         * @param {Object} scriptContext
         * @param {Record} scriptContext.currentRecord - Current form record
         * @param {string} scriptContext.sublistId - Sublist name
         * @param {string} scriptContext.fieldId - Field name
         * @param {number} scriptContext.lineNum - Line number. Will be undefined if not a sublist or matrix field
         * @param {number} scriptContext.columnNum - Line number. Will be undefined if not a matrix field
         *
         * @since 2015.2
         */
        function fieldChanged(scriptContext)
        {
            try
            {
                if(scriptContext.fieldId === 'custrecord_jj_fromdate_mams156' || scriptContext.fieldId === 'custrecord_jj_todate_mams156')
                {
                    var form = scriptContext.currentRecord;
                    var fromDate = form.getValue({fieldId: 'custrecord_jj_fromdate_mams156'});
                    var toDate = form.getValue({fieldId: 'custrecord_jj_todate_mams156'});
                    if(fromDate && toDate && fromDate > toDate)
                    {
                        form.setValue({
                            fieldId: 'custrecord_jj_todate_mams156',
                            value: null
                        });

                        alert("Record cannot be saved; 'To Date' should be greater than or equal to 'From Date'.");
                        return false;
                    }
                    return true;
                }
            }
            catch(error)
            {
                log.debug("error @fieldChanged", error)
            }
        }
        return {
            pageInit:pageInit,
            fieldChanged: fieldChanged
        };
    });

Leave a comment

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