BookatlasTwo-Day MuleSoft → Salesforce Bootcamp
1 / 18 · Module 3 — DataWeave 2.0 Crash Course · filtered: cheatsheet⊞ allnext →

3.39Harder exercise

Input:

{
  "customers": [
    {
      "id": "1",
      "name": "Acme",
      "phone": null,
      "revenue": "2000000"
    },
    {
      "id": "2",
      "name": "Widget",
      "phone": "4045551234",
      "revenue": "500000"
    }
  ]
}

Wanted Salesforce records:

[
  {
    "External_Id__c": "1",
    "Name": "Acme",
    "AnnualRevenue": 2000000,
    "Type": "Enterprise"
  },
  {
    "External_Id__c": "2",
    "Name": "Widget",
    "Phone": "4045551234",
    "AnnualRevenue": 500000,
    "Type": "Standard"
  }
]

DataWeave:

%dw 2.0
output application/java
---
payload.customers map (customer) -> {
    External_Id__c: customer.id,
    Name: customer.name,

    (Phone: customer.phone)
        if (customer.phone != null),

    AnnualRevenue:
        customer.revenue as Number,

    Type:
        if ((customer.revenue as Number) >= 1000000)
            "Enterprise"
        else
            "Standard"
}

That's probably about the complexity ceiling I would expect you to comfortably handle after two days.