如何在SuiteScript中仅初始化记录而不触发“ validateline”功能?

时间:2018-11-30 07:48:05

标签: javascript netsuite suitescript2.0 clientscript

NetSuite的发票记录中有2个字段。

一个是 poUnitCost 字段,另一个是 poTotalCost ”字段。

poTotalCost 的值等于 poUnitCost 乘以数量

公式:poTotalCost = poUnitCost x数量

一旦创建了记录,脚本将调用pageInit来查找相应的字段并将其填充到发票记录中。

但是,脚本不仅会触发pageInit函数,还会触发validateLine函数。

如何在创建记录时仅触发pageInit而不触发validate函数?

代码如下:

/**
 *@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
        }
    });

1 个答案:

答案 0 :(得分:2)

您无法停止子列表事件的触发。提交一行后,validateLine将触发。在任何不希望其执行其正常逻辑的情况下,您都需要调整validateLine处理程序以返回true