简体   繁体   English

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

[英]add button and update sales order using netsuite

I am new to netsuite,i need to update sales order by adding an item to it using a button click i had write the code for button using user event script and code for updating sales order using client script.but my client script is not working我是 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
    };

});

Your current example is almost correct.您当前的示例几乎是正确的。

You need a couple of things:你需要几件事:

  1. first add fireSlavingSync:true as a property for all your setCurrentLineItemValue methods.首先添加fireSlavingSync:true作为所有 setCurrentLineItemValue 方法的属性。 Otherwise Netsuite might not have finished updating the item's field by the time you get to your commitLine call.否则,当您调用commitLine时,Netsuite 可能还没有完成更新项目的字段。
  2. I'd also tend to set ignoreFieldChange:false .我也倾向于设置ignoreFieldChange:false
  3. you probably need to set a price level on the line item.您可能需要为订单项设置价格水平。 Depending on your version of Netsuite you should be able to just set a custom price level:根据您的 Netsuite 版本,您应该能够设置自定义价格水平:

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

AFAIK, if you add buttons from client script, they are only available in EDIT mode and you are trying to load same record and update it, this would result in record has been changed error message on UI(Also, NetSuite doesn't recommend this). AFAIK,如果您从客户端脚本添加按钮,它们仅在编辑模式下可用,并且您正在尝试加载相同的记录并更新它,这将导致 UI 上的记录已更改错误消息(此外,NetSuite 不建议这样做)。 The better way would be to use workflows to update records.更好的方法是使用工作流来更新记录。 In workflow you could add button using the workflow itself and on button click fire your workflow action script to update line item on the Order.在工作流中,您可以使用工作流本身添加按钮,然后单击按钮触发您的工作流操作脚本以更新订单上的行项目。 Here is sample you can refer to. 是您可以参考的示例。

/**
 * @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({
                        type: record.Type.SALES_ORDER,
                        id:recIdSO ,
                        isDynamic: true
                      });*/
        /*              console.log('recordid'+ recIdSO);
        */               /*log.debug({
                           title: 'recordid',
                           details: '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
        });*/
      }
    }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM