Making Fields Mandatory In Return Authorization Record (UI Context)

Requirements:

Make the item sublist fields: FAILURE REASON, PRIMARY ISSUE, SECONDARY ISSUE & ACTION is mandatory in the Return Authorization record if the item types are any of Inventory, Assembly, or Kit. 

Our Solution  

This can be achieved by using Suite scripts. 

As per the requirement, the item line fields: FAILURE REASON, PRIMARY ISSUE, SECONDARY ISSUE & ACTION should be made mandatory in the Return Authorization record if the item types are any of Inventory, Assembly, or Kit. 

On the creation of Return authorization through UI, a client script will validate the item lines. Initially, it will check the first line to know if the item type is any of the mentioned types (Inventory, Assembly, or Kit), if so it will validate the mandatory fields. It will allow us to add an item line in the item sublist if the mandatory fields are not empty. When the item type is neither Inventory, Assembly, or Kit, it will be excluded from the validation.

/**
 * @NApiVersion 2.1
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
/************************************************************************************************
 *Making Mandatory fields*
 *
 *********************************************************************************************
 *
 * Author: NUHEARA LTD
 *
 * Date Created : 04-May-2022
 *
 * Created By: VAIRAMUTHU, NUHEARA LTD
 *
 * Description : Making Mandatory fields
 *
 * REVISION HISTORY
 *
 ***********************************************************************************************/
define(['N/currentRecord','N/record', 'N/ui/dialog'],
    /**
     * @param{currentRecord} currentRecord
     * @param{record} record
     * @param{dialog} dialog
     */
    function(currentRecord,record, dialog)
    {
        /**
         * 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
            {
                let currentRecord = scriptContext.currentRecord;

                if (scriptContext.sublistId === 'item') {
                    let itemType = currentRecord.getCurrentSublistText({
                        sublistId: 'item',
                        fieldId: 'itemtype'
                    });                                                           //Get the current item's Item Type
                    log.debug({title: 'itemType', details: itemType});

                    if (itemType === 'InvtPart' || itemType === 'Assembly' || itemType === 'Kit')   //Check the current item is Inventory, Assembly or kit
                    {
                        let failureReason = currentRecord.getCurrentSublistValue({sublistId: 'item', fieldId: 'custcol_line_failure_reason'});                                                            //Get the field values
                        log.debug({title: 'failureReason', details: failureReason});
                        let primaryIssue = currentRecord.getCurrentSublistValue({sublistId: 'item', fieldId: 'custcol2'});
                        log.debug({title: 'primaryIssue', details: primaryIssue});
                        let secondaryIssue = currentRecord.getCurrentSublistValue({sublistId: 'item', fieldId: 'custcol3'});
                        log.debug({title: 'secondaryIssue', details: secondaryIssue});
                        let action = currentRecord.getCurrentSublistValue({sublistId: 'item', fieldId: 'custcol6'});
                        log.debug({title: 'action', details: action});

                        if (checkForParameter(failureReason))    //Check the fields are empty or not
                        {
                            if (checkForParameter(primaryIssue))
                            {
                                if (checkForParameter(secondaryIssue))
                                {
                                    if (checkForParameter(action))
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        dialog.alert({title: 'Empty Field:', message: 'Please fill the Action Field!'});
                                        return false;
                                    }
                                }
                                else
                                {
                                    dialog.alert({title: 'Empty Field:', message: 'Please fill the Secondary Issue Field!'});
                                    return false;
                                }
                            }
                            else
                            {
                                dialog.alert({title: 'Empty Field:', message: 'Please fill the Primary Issue Field!'});
                                return false;
                            }
                        }
                        else
                        {
                            dialog.alert({title: 'Empty Field:', message: 'Please fill the Failure Reason Field!'});
                            return false;
                        }
                    }
                    else
                    {
                        return true;
                    }
                }
            }
            catch (e)
            {
                log.debug({title: 'error in validateLine', details: e});
            }
        }

        /**
         * Validation function to be executed when record is saved.
         *
         * @param {Object} scriptContext
         * @param {Record} scriptContext.currentRecord - Current form record
         * @returns {boolean} Return true if record is valid
         *
         * @since 2015.2
         */

        function saveRecord(scriptContext)
        {
            try
            {
                    let flag = false;
                    let currentRecord = scriptContext.currentRecord;

                    let lineCount = currentRecord.getLineCount({sublistId: 'item'});
                    log.debug({title: 'lineCount', details: lineCount});

                    for (let lineNo = 0; lineNo < lineCount; lineNo++)
                    {
                        log.debug({title: 'lineNo', details: lineNo});
                        let itemType = currentRecord.getSublistValue({sublistId: 'item', fieldId: 'itemtype', line: lineNo});//Get the current item's Item Type
                        log.debug({title: 'itemType', details: itemType});

                        if (itemType === 'InvtPart' || itemType === 'Assembly' || itemType === 'Kit')   //Check the current item is Inventory, Assembly or kit
                        {
                            let line = lineNo + 1;
                            let failureReason = currentRecord.getSublistValue({sublistId: 'item', fieldId: 'custcol_line_failure_reason', line: lineNo});                                                            //Get the field values
                            log.debug({title: 'failureReason', details: failureReason});
                            let primaryIssue = currentRecord.getSublistValue({sublistId: 'item', fieldId: 'custcol2', line: lineNo});
                            log.debug({title: 'primaryIssue', details: primaryIssue});
                            let secondaryIssue = currentRecord.getSublistValue({sublistId: 'item', fieldId: 'custcol3', line: lineNo});
                            log.debug({title: 'secondaryIssue', details: secondaryIssue});
                            let action = currentRecord.getSublistValue({sublistId: 'item', fieldId: 'custcol6', line: lineNo});
                            log.debug({title: 'action', details: action});

                            if (checkForParameter(failureReason))    //Check the fields are empty or not
                            {
                                if (checkForParameter(primaryIssue))
                                {
                                    if (checkForParameter(secondaryIssue))
                                    {
                                        if (checkForParameter(action))
                                        {
                                            flag = true;
                                        }
                                        else
                                        {
                                            dialog.alert({title: 'Empty Field:', message: 'Please fill the Action Field in line '+ line});
                                            return false;
                                        }
                                    }
                                    else
                                    {
                                        dialog.alert({title: 'Empty Field:', message: 'Please fill the Secondary Issue Field in Line ' + line});
                                        return false;
                                    }
                                }
                                else
                                {
                                    dialog.alert({title: 'Empty Field:', message: 'Please fill the Primary Issue Field in Line ' + line});
                                    return false;
                                }
                            }
                            else
                            {
                                dialog.alert({title: 'Empty Field:', message: 'Please fill the Failure Reason Field in Line ' + line});
                                return false;
                            }
                        }
                        else
                        {
                            flag = true;
                        }
                    }
                    if (flag) {
                        return true;
                    } else {
                        return false
                    }
            }
            catch (e)
            {
                log.debug({title: 'error in saveRecord', details: e});
            }
        }

        /**
         * @description Check whether the given parameter argument has value on it or is it empty.
         * ie, To check whether a value exists in parameter
         * @param {*} parameter parameter which contains/references some values
         * @param {*} parameterName name of the parameter, not mandatory
         * @returns {Boolean} true if there exist a value, else false
         */

        function checkForParameter(parameter, parameterName)
        {
            if (parameter !== "" && parameter !== null && parameter !== undefined && parameter !== false && parameter !== "null" && parameter !== "undefined" && parameter !== " " && parameter !== 'false')
            {
                return true;
            }
            else
            {
                if (parameterName)
                    log.debug({title:'Empty Value found',details: 'Empty Value for parameter ' + parameterName});
                return false;
            }
        }
        return {saveRecord: saveRecord,validateLine:validateLine};
    });

Leave a comment

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