Search code examples
salesforcemulemule-studiomulesoftmule4

Map the Array in to the each individual JSON so the records can be created


I am trying to convert the below Array in to the JSON, trying by iterating through the Array and transforming. The array looks like below

Quote_Sent_To__r = [
{
Quote__c=0Q02D05XGQSA2,
 Id=a1H2D0m94QUAQ, 
type=QuoteSentTo__c
},
{
Quote__c=0Q02D00XGQSA2,
 Id=a1H2D00000AQ, 
type=QuoteSentTo__c
}
]

I have stored the array in to the variable quoteSentToList and iterating through the for loop

enter image description here

Within each iteration I need to get the JSON like

   {
    "Quote__c": "0Q02D05XGQSA2"
    }

So this can be passed to the Salesforce Update operation. I tried like

%dw 2.0
output application/json
var item = vars.quoteSentToList[counter]
---
{
"Quote__c" :payload.Id
}

It errors saying

Reason: Unable to resolve reference of: counter.. Scripting language error on expression 'payload'. Reason: Unable to resolve reference of: payload.. This is my first project and any help is greatly appreciated

Error

""Unexpected character 'v' at quoteSentToList@[1:1] (line:column), expected false or true or null or {...} or [...] or number but was , while reading quoteSentToList as Json.

1| vars.existingQuote[0].Quote_Sent_To__r ^" evaluating expression: "%dw 2.0 output application/json --- vars.quoteSentToList map { Quote__c: payload.Id, Id: $.Id }"."


Solution

  • counter is a Mule variable, not a DataWeave variable. You need to use the prefix vars. to reference it inside DataWeave scripts: vars.counter.

    Alternatively, instead of using a <foreach> scope, you can transform the entire array at once and then use each element as needed:

    %dw 2.0
    output application/json
    ---
    vars.quoteSentToList map { Quote__c: $.Id }
    

    Output:

    [
      {
        "Quote__c": "a1H2D0m94QUAQ"
      },
      {
        "Quote__c": "a1H2D00000AQ"
      }
    ]