Custom Error throwing using N/error in SuiteScript

define(['N/error'],
    /**
 * @param{error} error
 */
    (error) => {


        /**
         * Defines the function definition that is executed before record is submitted.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {Record} scriptContext.oldRecord - Old record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @since 2015.2
         */
        const beforeSubmit = (scriptContext) => {
            let record = scriptContext.newRecord;
            //customer field
            let customerField = record.getValue({
                fieldId: 'custrecord_jj_discount_customer'
            });
            let valueCus = customerField ? true : false;


            //mpn field
            let mpnField = record.getValue({
                fieldId: 'custrecord_jj_discount_item_mpn'
            });
            let mpnvalue = mpnField ? true : false;


            //hp mpn field
            let hpmpnField = record.getValue({
                fieldId: 'custrecord_jj_discount_item_hp_mpn'
            });
            let hpmpnvalue = hpmpnField ? true : false;


            //is for all customer field
            let isForAllCustomerField = record.getValue({
                fieldId: 'custrecord_jj_discount_all_customer'
            });


            // public sector field
            let publicSectorField = record.getValue({
                fieldId: 'custrecord_jj_discount_public_sector'
            });


            //validating
            if ((!valueCus && !isForAllCustomerField && !publicSectorField) && (!mpnvalue && !hpmpnvalue)) {
                //if none of the mandatory values exist
                const message = 'Error: The following fields are missing: (i)'Customer' or 'Is For All Customer' or 'Public Sector' (any one of the three fields are to be filled) and (ii) 'MPN' or 'HP MPN' (any one of the two fields are to be filled).';


                throw errorFun(message);
            }
            else if (!mpnvalue && !hpmpnvalue) {
                //if MPN or HP MPN values dont exist
                const message = 'Error: The following fields are missing: 'MPN' or 'HP MPN'.';
                throw errorFun(message);
            }
            else if (!valueCus && !isForAllCustomerField && !publicSectorField) {
                //if customer or is for all customer or public sector values dont exist
                const message = 'Error: The following fields are missing: 'Customer' or 'Is For All Customer' or 'Public Sector'.';
                throw errorFun(message);
            }
        }
        function errorFun(errMessage) {
            try {
                let errorObj = error.create({
                    message: errMessage,
                    name: 'MANDATORY_FIELD_MISSING',
                    notifyOff: true
                });
                return errorObj.message;
            }
            catch (Err) {
                log.error("Error in the function errorFun", Err);
            }




        }
        return { beforeSubmit }


    });

Leave a comment

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