如何在提交活动后获得承诺的项目数量?

问题描述 投票:0回答:1

我正试图从销售订单行项目中获取一些价值,但由于某种原因,我无法从销售订单项目中获得数量提交字段。

这是我正在使用的现有代码:

/**
* @NApiVersion 2.x
* @NScriptType UserEventScript 
*/
define([
  'N/log'
  ], function(
    log
  ) {
    const exports = {};
      function afterSubmit(context) {
      var newRecord = context.newRecord
      switch (context.type) {
        case context.UserEventType.CREATE:
          return ;
        case context.UserEventType.EDIT:
          var payload = getSalesOrderItems(newRecord);
          log.debug(payload);
          break;
        default:
          throw 'Invalid event type';
      }
  }

  exports.afterSubmit = afterSubmit;
  return exports;
});

function getSalesOrderItems(salesRecord) 
      {
        var items = [];
        var itemLength = salesRecord.getLineCount({
            sublistId : 'item'
        }); 

        if (itemLength === 0) throw 'Order does not have any valid item';

        for (var index = 0; index < itemLength; index++) {
          var item = {};

          var itemId = salesRecord.getSublistValue({
            sublistId: 'item', 
            fieldId: 'item', 
            line: index
          });

          try {
            var itemRecord = record.load({
              type: record.Type.SERIALIZED_INVENTORY_ITEM,
              id: itemId,
            });
          } catch (ex) {
            if (JSON.parse(ex).name == "SSS_RECORD_TYPE_MISMATCH") {
              itemRecord = record.load({
                type: record.Type.KIT_ITEM,
                id: itemId,
              });
            }
          }

          if (!itemRecord) throw ('Item with id ' + itemId + ' does not exist');

          item.sku = itemRecord.getValue('itemidorig');
          item.quantity_committed = salesRecord.getSublistValue({
            sublistId: 'item', fieldId: 'quantitycommitted', line: index
          });
          item.quantity = salesRecord.getSublistValue({
            sublistId: 'item', fieldId: 'quantity', line: index
          });

          items.push(item)

          return items;
        }
      }

这是目前的结果。

{
    [{
        "sku":"EMOST00405",
        "quantity":2
    }]
}

这是我期待的结果。

{
    [{
         "sku":"EMOST00405",
         "quantity_committed": 1,
         "quantity":2
    }]
}

通过更新顺序触发事件时,它可以正常工作。任何回复表示赞赏。

netsuite suitescript2.0
1个回答
0
投票

您的代码似乎是正确的,但如果您当前表单上没有字段(可能隐藏/未在当前表单上添加),NetSuite将返回undefined。确保在当前正在处理的SO表单上添加并显示该字段,然后再次尝试运行该代码。

© www.soinside.com 2019 - 2024. All rights reserved.