Validation of Work Order Line Fields During Record Creation or Edit in User Event Script

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
define(['N/error'],
    /**
     * @param{error} error
     */
    (error) => {
        "use strict";

        /**
         * 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 invalidLines = 0;
            try {
                if (scriptContext.type === scriptContext.UserEventType.CREATE || scriptContext.type === scriptContext.UserEventType.EDIT) {
                    let workOrderRec = scriptContext.newRecord;
                    let sublistId = 'recmachcustrecord_parent';
                    let lineCount = workOrderRec.getLineCount({ sublistId });
                    for (let i = 0; i < lineCount; i++) {
                        let soLine = workOrderRec.getSublistValue({
                            sublistId,
                            fieldId: 'custrecord_so_line_id',
                            line: i
                        });;
                        let dtLine = workOrderRec.getSublistValue({
                            sublistId,
                            fieldId: 'custrecord_jj_dt_order_line_mhaun_21',
                            line: i
                        });
                        if (!soLine || !dtLine) {
                            invalidLines = 1;
                            break;
                        }
                    }
                }
            } catch (error) {
                log.error("Error @ beforeSubmit", error);
            }
            if (invalidLines == 1) {
                let errorMessage = error.create({
                    name: 'MISSING_LINE_FIELDS',
                    message: `MISSING REQUIRED VALUES: Each Work Order item line must have both SO Line and DT Line fields populated. Please update the missing values and try again.`,
                    notifyOff: false
                });
                throw errorMessage.message;
            }
        }


        return { beforeSubmit }
    });

Leave a comment

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