How to frame dynamic salesforce composite query by iterating array

659 Views Asked by At

Below is my input and through which I need to frame composite query to Salesforce

    {
  "headerId": "123",
  "Status": "Created",
  "OrderId": "232323",
  "Code": "RE",
  "Line": [{
      "lineItemId": "HP-0000074009",
      "status": null
  },
  {
      "lineItemId": "HP-0000074010",
      "status": "y"
  }
  ]
}

but the challenge here is I have array(Line) in the input which I need to iterate and first use GET to get the Id from the object and then use that Id to update the status in Salesforce using PATCH, means my dwl for composite query should look like below, where I need to get the Id and then use it in PATCH but I am unable to frame it dynamically, so please help with the correct dwl in order to achieve this scenario.

    %dw 2.0
output application/json
---
{
    "allOrNone" : true,
    "compositeRequest" : [
  {
    "method": "GET",
    "url": "/services/data/v53.0/query/?q=SELECT Id FROM Line_vod__c WHERE Name='HP-0000074009' AND Code_TPI__c='RE'",
    "referenceId": "LineId"
  },
  {
    "method": "GET",
    "url": "/services/data/v53.0/query/?q=SELECT Id FROM Line_vod__c WHERE Name='HP-0000074010' AND Code_TPI__c='RE'",
    "referenceId": "LineId"
  },
  {
    "method": "PATCH",
    "url": "/services/data/v53.0/sobjects/Line_vod__c/@{LineId.records[0].Id}",
    "referenceId": "updateLine",
    "body": {
      "Status_TPI__c": "Open"
    }
  },
  {
    "method": "PATCH",
    "url": "/services/data/v53.0/sobjects/Line_vod__c/@{LineId.records[0].Id}",
    "referenceId": "updateLine",
    "body": {
      "Status_TPI__c": "Closed"
    }
  }
]
}
1

There are 1 best solutions below

2
Shyam Raj Prasad On

Below script might help you.

 %dw 2.0
output application/json

---
{
    allOrNone : true,
    compositeRequest : flatten(payload.Line map ((item, index) -> 
       [{
    "method": "GET",
    "url": "/services/data/v53.0/query/?q=SELECT Id FROM Line_vod__c WHERE Name='" ++ item.lineItemId ++ "' AND Code_TPI__c='" ++  payload.Code ++ "'",
    "referenceId": "LineId"
  } , 
       {
    "method": "PATCH",
    "url": "/services/data/v53.0/sobjects/Line_vod__c/@{LineId.records[0].Id}",
    "referenceId": "updateLine",
    "body": {
      "Status_TPI__c": if(item.status == "y") "Closed" else "Open"
    }
       }]
    ))
}