Search code examples
mulemule-studiodataweavemulesoftmule4

update function is not working as expected in dataweave 2?


I would like to update one line in my incoming payload. The field is 'price'. My dataweave scripet is

%dw 2.0

output application/json

---

payload.IntegrationEntities.integrationEntity.integrationEntityDetails.contractUtilization.items.item update 
{

    case  price at .price -> "100"
}

My Payload is;

    {
        "IntegrationEntities": {
            "integrationEntity": [
                {
                    "integrationEntityHeader": {
                        "integrationTrackingNumber": "XXXX",
                        "referenceCodeForEntity": "132804",
                        "additionalInfo": "ADDITIONALINFO"
                    },
                    "integrationEntityDetails": {
                        "contractUtilization": {
                            "externalId": "417145",
                            "utilizationType": "INVOICE",
                            "isDelete": "No",
                            "documentNumber": "132804",
                            "documentDescription": "",
                            "documentDate": "2021-03-26",
                            "totalSpendAmount": ".92",
                            "documentCurrency": "AUD",
                            "createdBy": "Oracle Integration",
                            "status": "FULLY PAID",
                            "items": {
                                "item": [
                                    {
                                        "lineItemId": "132804_1",
                                        "contractNumber": "YYYYYYY",
                                        "contractLineId": "",
                                        "lineNumber": "1",
                                        "name": "132804",
                                        "description": "132804",
                                        "quantity": "1",
                                        "price": ".92",
                                        "lineAmount": ".92",
                                        "purchaseOrderNumber": "YYYYYY",
                                        "purchaseOrderDescription": ""
                                    },
 {
                                        "lineItemId": "132804_2",
                                        "contractNumber": "YYYYYYY",
                                        "contractLineId": "",
                                        "lineNumber": "1",
                                        "name": "132804",
                                        "description": "132804_2",
                                        "quantity": "1",
                                        "price": ".95",
                                        "lineAmount": ".95",
                                        "purchaseOrderNumber": "YYYYYY",
                                        "purchaseOrderDescription": ""
                                    }
                                ]
                            }
                        }
                    }
                }
            ]
        }
    }

But above script always returning with the income payload price as '.92'

The output I get is;

    [
      [
        {
          "lineItemId": "132804_1",
          "contractNumber": "XXXXXX",
          "contractLineId": "",
          "lineNumber": "1",
          "name": "132804",
          "description": "132804",
          "quantity": "1",
          "price": ".92",
          "lineAmount": ".92",
          "purchaseOrderNumber": "YYYYY",
          "purchaseOrderDescription": ""
        },
,
    {
      "lineItemId": "132804_2",
      "contractNumber": "YYYYYYY",
      "contractLineId": "",
      "lineNumber": "1",
      "name": "132804",
      "description": "132804_2",
      "quantity": "1",
      "price": ".95",
      "lineAmount": ".95",
      "purchaseOrderNumber": "YYYYYY",
      "purchaseOrderDescription": ""
    }
      ]
    ]

What is wrong with my script? Why it is not updating my price value? and why i get the partial payload as output? My requirement is, update the price amount and give the full payload as output.


Solution

  • Try with this

    %dw 2.0
    output application/json
    ---
    payload update {
       case item at .IntegrationEntities.integrationEntity[0].integrationEntityDetails.contractUtilization.items.item[0] -> item   update {
           case price at .price -> "100"
       }
    }
    

    Updating the answer per your requirement:

    %dw 2.0
    output application/json
    ---
    payload update {
    
    case .IntegrationEntities.integrationEntity[0].integrationEntityDetails.contractUtilization.items.item -> $ map {
        ($ update {
              case .price -> if ($ as Number < 1) "0" ++ $ else $
        }
        )}
    }