如何从对象数组中设置发票行项目数据?

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

我正在处理多张发票,它是使用计划脚本的行级数据。

下面是对象数组。

Var JEIDArray = [ { item: "663", amount: "3000.00", JE: 13026407, invId: "13020561" }, { item: "663", amount: "100.00", JE: 13026408, invId: "13020561 “ }, { 物品: “663”,金额:“111.00”,JE:13026409,invId:“13026406”},{项目:“663”,金额:“222.00”,JE:13026410,invId:“13026406”},{项目:“373” ”,金额:“444.00”, JE:13026411,invId:“13026406”}];

在这里我得到了多余的发票 ID。因此,我必须每次加载每张发票,然后设置行级别值。如何避免多次加载同一张发票,但在正确的发票上设置行级数据? 请帮忙!

以下是更新发票行的函数: 函数 updateInvoice(JEIDArray) { 尝试{

        //var currentInvoiceId = null;
        for (var r=0;r<JEIDArray.length;r++)
        {
        var getRecId=JEIDArray[r].invId;
        log.debug("getRecId",getRecId);
        
        //currentInvoiceId=getRecId;
        
        var invoice = record.load({
            type: record.Type.INVOICE,
            id: getRecId,
            isDynamic: false
        })
    
        // Get the number of invoice lines
        var lineCount = invoice.getLineCount({
            sublistId: 'item'
        });
        log.debug("lineCount", lineCount);

        // Loop through each invoice line
        for (var i = 0; i < lineCount; i++) {
            var currentItemId = invoice.getSublistValue({
                sublistId: 'item',
                fieldId: 'item',
                line: i
            });
            var currentItemAmount = invoice.getSublistValue({
                sublistId: 'item',
                fieldId: 'custcol_ay_cts_allocation_amount',
                line: i
            });

            // Loop through each item in JEIDArray
            var flag = false;
            for (var j = 0; j < JEIDArray.length; j++) {
                var jeItem = JEIDArray[j];
                if (currentItemId == jeItem.item && currentItemAmount == jeItem.amount) {
                    //log.debug("Item and amount flag in JEIDArray: ", currentItemId, currentItemAmount);
                    flag = true;
                    invoice.setSublistValue({
                        sublistId: 'item',
                        fieldId: 'custcol_ay_cts_assoc_ic', // Replace with your desired custom field
                        line: i,
                        value: jeItem.JE
                    });
                    break;
                }
            }
            if (!flag) {
                //log.debug("Item or amount not flag in JEIDArray: ", currentItemId, currentItemAmount);
            }
        }
        // Update the invoice if necessary
        var invoiceId=invoice.save();
        log.debug('Invoice Updated', `Invoice ID: ${invoiceId}`);
    }
netsuite suitescript2.0 invoice
1个回答
0
投票

在迭代之前按

invId
对数据进行分组。有很多方法可以做到这一点;就我个人而言,我会在数组上使用
reduce()
,如下所示:

JEIDArray.reduce((data, line) => {
  data[line.invId] ??= []
  data[line.invId].push(line)
  return data
}, {})

这将产生一个类似的对象

{
  "13020561": [
    {
      "item": "663",
      "amount": "3000.00",
      "JE": 13026407,
      "invId": "13020561"
    },
    {
      "item": "663",
      "amount": "100.00",
      "JE": 13026408,
      "invId": "13020561"
    }
  ],
  "13026406": [
    {
      "item": "663",
      "amount": "111.00",
      "JE": 13026409,
      "invId": "13026406"
    },
    {
      "item": "663",
      "amount": "222.00",
      "JE": 13026410,
      "invId": "13026406"
    },
    {
      "item": "373",
      "amount": "444.00",
      "JE": 13026411,
      "invId": "13026406"
    }
  ]
}

一旦有了这个对象,您就可以通过

Object.keys()
迭代每个发票。所有这些都假设您使用的是 SuiteScript 2.1。

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