javascriptnetsuitesuitescript2.0clientscript

How to just initialize the record without trigger "validateline" function in SuiteScript?


There are 2 fields in the Invoice record of NetSuite.

One is the poUnitCost field, the other is the poTotalCost" field.

The value of poTotalCost equals poUnitCost multiply quantity.

(Formula: poTotalCost = poUnitCost x quantity)

Once the record is been creating, the script will call the pageInit which find the corresponding fields and fill them into the Invoice record.

However, script not only triggers the pageInit function but also the validateLine function.

How to just trigger the pageInit when creating the record without triggering the validate function?

The code is as the following:

/**
 *@NApiVersion 2.x
 *@NScriptType ClientScript
 */
define(['N/currentRecord', 'N/record', 'N/search'],
    function (currentRecord, record, search) {

        function pageInit(context) {
            console.log('inside pageinit function');

            var rec = context.currentRecord;
            var createdFromID = rec.getValue({
                fieldId: 'createdfrom'
            });
            var so_record = record.load({
                type: record.Type.SALES_ORDER,
                id: createdFromID
                // isDynamic: true
            });

            var inv_lines = rec.getLineCount({
                sublistId: 'item'
            });

            for (var i = 0; i < inv_lines; i++) {
                var soLineID = rec.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'so_line_id_in_Inv',
                    line: i
                });

                // Find the index of line whose 'custcol_so_line_id' field is soLineIDINV
                var soLineNumber = so_record.findSublistLineWithValue({
                    sublistId: 'item',
                    fieldId: 'so_line_id_in_SO',
                    value: soLineID
                });

                var poUnitCost = so_record.getSublistValue({
                    sublistId: 'item',
                    fieldId: 'unit_cost_in_SO',
                    line: soLineNumber
                });

                rec.setCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'unit_cost_in_Inv',
                    value: poUnitCost,
                    line: i
                    // fireSlavingSync: true
                });

                var lineQty = rec.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'quantity',
                    line: i
                });

                var poTotalCost = lineQty * poUnitCost;

                rec.setCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'total_cost_in_Inv',
                    value: poTotalCost,
                    line: i
                    // fireSlavingSync: true
                });
            }
            return true;
        }

        function validateLine(context) {
            console.log('inside validateLine function');

            var rec = context.currentRecord;
            var sublistName = context.sublistId;

            if (sublistName == 'item') {
                var poUnitCost = rec.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'unit_cost_in_Inv',
                });

                var lineQty = rec.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'quantity',
                });

                var poTotalCost = lineQty * poUnitCost;

                rec.setCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'total_cost_in_Inv',
                    value: poTotalCost
                    // fireSlavingSync: true
                });
            }
            return true;
        }

        return {
            pageInit: pageInit,
            validateLine: validateLine
        }
    });

Solution

  • You can't stop the sublist events from triggering. When a line is committed, validateLine will fire. You will need to adjust your validateLine handler to return true in any situations where you don't want it to execute its normal logic.