使用netsuite添加按钮并更新销售订单

时间:2018-06-27 07:06:17

标签: button netsuite

我是netsuite的新手,我需要通过单击按钮添加商品来更新销售订单,我已经使用用户事件脚本编写了按钮代码,并使用客户端脚本编写了用于更新销售订单的代码。但是我的客户端脚本不起作用

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
define(['N/currentRecord','N/record'],

function(record) {

     function buttonclick() {

            try{
                var CurrRecord = currentRecord.get();
                var recIdSO= CurrRecord.id;
                var salesOrder = record.load({
                        type: record.Type.SALES_ORDER,
                        id:recIdSO ,
                        isDynamic: true
                      });
                 log.debug({
                     title: 'recordid',
                     details: 'Id: ' + recIdSO
                 }); 

                    var line=salesOrder.selectNewLine({
                     sublistId: 'item'

                 });

                    salesOrder.setCurrentSublistValue({
                            sublistId : 'item',
                            fieldId : 'item',
                            value :510 ,
                            ignoreFieldChange: true
                        });

                    salesOrder.setCurrentSublistValue({
                        sublistId : 'item',
                        fieldId : 'amount',
                        value :100 ,
                        ignoreFieldChange: true
                    });

                    salesOrder.commitLine({
                         sublistId: 'item'
                     });
                     var recId = salesOrder.save();
                     log.debug({
                         title: 'Record updated successfully',
                         details: 'Id: ' + recId
                     });

            }catch (e) {

                         log.error({
                             title: e.name,
                             details: e.message
                         });
                     }


        }





    /**
     * Function to be executed after page is initialized.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.mode - The mode in which the record is being accessed (create, copy, or edit)
     *
     * @since 2015.2
     *
    function pageInit(scriptContext) {

    }

    /**
     * Function to be executed when field is changed.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     * @param {string} scriptContext.fieldId - Field name
     * @param {number} scriptContext.lineNum - Line number. Will be undefined if not a sublist or matrix field
     * @param {number} scriptContext.columnNum - Line number. Will be undefined if not a matrix field
     *
     * @since 2015.2
     *
    function fieldChanged(scriptContext) {

    }

    /**
     * Function to be executed when field is slaved.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     * @param {string} scriptContext.fieldId - Field name
     *
     * @since 2015.2
     *
    function postSourcing(scriptContext) {

    }

    /**
     * Function to be executed after sublist is inserted, removed, or edited.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     *
     * @since 2015.2
     *
    function sublistChanged(scriptContext) {

    }

    /**
     * Function to be executed after line is selected.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     *
     * @since 2015.2
     *
    function lineInit(scriptContext) {

    }

    /**
     * Validation function to be executed when field is changed.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     * @param {string} scriptContext.fieldId - Field name
     * @param {number} scriptContext.lineNum - Line number. Will be undefined if not a sublist or matrix field
     * @param {number} scriptContext.columnNum - Line number. Will be undefined if not a matrix field
     *
     * @returns {boolean} Return true if field is valid
     *
     * @since 2015.2
     *
    function validateField(scriptContext) {

    }

    /**
     * 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) {

    }

    /**
     * Validation function to be executed when sublist line is inserted.
     *
     * @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 validateInsert(scriptContext) {

    }

    /**
     * Validation function to be executed when record is deleted.
     *
     * @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 validateDelete(scriptContext) {

    }

    /**
     * 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) {




    }

 return {

        saveRecord: saveRecord,
        buttonclick:buttonclick
    };

});

3 个答案:

答案 0 :(得分:1)

您当前的示例几乎是正确的。

您需要满足以下条件:

  1. 首先将fireSlavingSync:true添加为所有setCurrentLineItemValue方法的属性。否则,在您致电commitLine时,Netsuite可能尚未完成项目字段的更新。
  2. 我也倾向于设置ignoreFieldChange:false
  3. 您可能需要在订单项上设置价格水平。根据您的Netsuite版本,您应该可以只设置自定义价格水平:

    salesOrder.setCurrentSublistValue({ sublistId : 'item', fieldId : 'price', value :-1 , ignoreFieldChange: false, fireSlavingSync:true });

答案 1 :(得分:0)

AFAIK,如果您从客户端脚本添加按钮,则它们仅在EDIT模式下可用,并且您尝试加载相同的记录并对其进行更新,这将导致UI上的记录已更改错误消息(此外,NetSuite不会不推荐这样做)。更好的方法是使用工作流来更新记录。 在工作流程中,您可以使用工作流程本身添加按钮,然后单击按钮触发您的工作流程操作脚本以更新订单上的订单项。 Here是您可以参考的示例。

答案 2 :(得分:0)

/ **  * @NApiVersion 2.x  * @NScriptType ClientScript  * @NModuleScope SameAccount  * / define([['N / currentRecord','N / record'],

function(currentRecord,record){

function pageInit(scriptContext) {

}

 function buttonclick() {
        try{
            var salesOrder = currentRecord.get();
            console.log(salesOrder)
            var recIdSO= salesOrder.id;

/ * var salesOrder = record.load({                         类型:record.Type.SALES_ORDER,                         id:recIdSO,                         isDynamic:true                       }); / / console.log('recordid'+ recIdSO); * / /*log.debug({                      标题:“ recordid”,                      详细信息:'Id:'+ recIdSO                  }); * /

                var line=salesOrder.selectNewLine({
                 sublistId: 'item'

             });
                console.log('line'+ line);

                salesOrder.setCurrentSublistValue({
                        sublistId : 'item',
                        fieldId : 'item',
                        value :510 ,
                        ignoreFieldChange: true
                    });

                salesOrder.setCurrentSublistValue({
                    sublistId : 'item',
                    fieldId : 'amount',
                    value :100 ,
                    ignoreFieldChange: true
                });
                salesOrder.setCurrentSublistValue({
                    sublistId : 'item',
                    fieldId : 'taxcode',
                    value :-160 ,
                    ignoreFieldChange: true
                });

                salesOrder.commitLine({
                     sublistId: 'item'
                 });
                 var recId = salesOrder.save();
                 log.debug({
                     title: 'Record updated successfully',
                     details: 'Id: ' + recId
                 });

        }catch (e) {

            console.log('recordid'+ recIdSO);
                     /*log.error({
                         title: e.name,
                         details: e.message
                     });*/
                 }


    }

返回{

     pageInit: pageInit,
    buttonclick:buttonclick
};

});