0

我需要使用 JSON 文件进行发布请求。JSON 目前看起来像这样:

{
        "compositeRequest" : [{
          // Account
          "method" : "POST",
          "url" : "/services/data/v52.0/sobjects/Account",
          "referenceId" : "refAccount",
          "body" : { 
            "Name" : req.body.accName 
          }
        },{
          // Contact
          "method" : "POST",
          "url" : "/services/data/v52.0/sobjects/Contact",
          "referenceId" : "refContact",
          "body" : { 
            "LastName" : req.body.conLastName,
            "AccountId" : "@{refAccount.id}"
          }
        },{
          // Order
          "method" : "POST",
          "url" : "/services/data/v52.0/sobjects/Order",
          "referenceId" : "refOrder",
          "body" : { 
            "AccountId" : "@{refAccount.id}",
            "Pricebook2Id" : PBResult.records[0].Id,
            "EffectiveDate" : date,
            "Status" : "Draft"
          }
        },{
          // OrderItem
          "method" : "POST",
          "url" : "/services/data/v52.0/sobjects/OrderItem",
          "referenceId" : "refOrderItem",
          "body" : { 
            "Product2Id" : req.params.productId,
            "OrderId" : "@{refOrder.id}",
            "Quantity" : req.body.itemQuantity,
            "PricebookEntryId" : entryResult.records[0].Id,
            "UnitPrice" : entryResult.records[0].UnitPrice,
            "blng__BillableUnitPrice__c": entryResult.records[0].UnitPrice,
            "SBQQ__ChargeType__c": prodResult.records[0].SBQQ__ChargeType__c,
            "blng__TaxRule__c": prodResult.records[0].blng__TaxRule__c,
            "blng__BillingRule__c": prodResult.records[0].blng__BillingRule__c,
            "blng__RevenueRecognitionRule__c": prodResult.records[0].blng__RevenueRecognitionRule__c,
            "ServiceDate": date,
            "blng__LastChargeToDate__c": date
          }
        }
      ]
    }

我想根据数组的大小生成更多的 OrderItem 对象(在注释中指出),我该怎么做?我已经有了需要在正文中添加的数据。是否可以创建一个函数来创建另一个 OrderItem,并将其放在已经存在的 OrderItem 之后(如果我要从零创建第一个 OrderItem 会更好)。

提前致谢。

4

1 回答 1

0

如果你想向 中添加一些东西object,你必须使用

Object.assign(myObject, data);

如果你想在 中添加一些东西array,你必须使用:

myArray.push(data);

由于我不太了解您的数据构造的结构,因此我只是尝试参考// OrderItem您标记的 。在这种情况下,它将类似于:

let newOrderItem = {
          // OrderItem
          "method" : "POST",
          "url" : "/services/data/v52.0/sobjects/OrderItem",
          "referenceId" : "refOrderItem",
          "body" : { 
            "Product2Id" : req.params.productId,
            "OrderId" : "@{refOrder.id}",
            "Quantity" : req.body.itemQuantity,
            "PricebookEntryId" : entryResult.records[0].Id,
            "UnitPrice" : entryResult.records[0].UnitPrice,
            "blng__BillableUnitPrice__c": entryResult.records[0].UnitPrice,
            "SBQQ__ChargeType__c": prodResult.records[0].SBQQ__ChargeType__c,
            "blng__TaxRule__c": prodResult.records[0].blng__TaxRule__c,
            "blng__BillingRule__c": prodResult.records[0].blng__BillingRule__c,
            "blng__RevenueRecognitionRule__c": prodResult.records[0].blng__RevenueRecognitionRule__c,
            "ServiceDate": date,
            "blng__LastChargeToDate__c": date
          }

yourObject.compositeRequest.push(newOrderItem)
于 2021-09-07T20:53:35.897 回答