Generate a json file dynamically

59 views Asked by At

I need to do a post request using a JSON file. The JSON is currently looking like this:

{
        "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
          }
        }
      ]
    }

I want to generate more OrderItem objects(indicated in the comment) based on the size of an array, how can I do this? I already have the data that I need to add on the body. Is it possible to create a function to create another OrderItem, and place it after the already existing one (it would even be better if I was going to create the first OrderItem from zero).

Thanks in advance.

1

There are 1 answers

2
StevenSiebert On

If you want to add something to an object, you have to use

Object.assign(myObject, data);

If you want to add something to an array, you have to use:

myArray.push(data);

As I don“t really understand the structure of your data construct, I just try to refer to // OrderItem, which you marked. In this case it would be something like:

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)