Mule 4 : For Each : org.mule.runtime.api.metadata.TypedValue cannot be cast to java.util.Map

548 Views Asked by At

enter image description hereScenario: enter image description here

  1. Received a list of records of from database
  2. Transformed the list of records using Transform Message for Salesforce Mapping and data type : application/java
  3. After the transformation is complete, using a for each block to upsert these transformed records into batches of size 1000.

Problem Statement : As soon as the payload is passed to for each block, the data type of the payload converts to TypedValues and we get the below error when we pass the payload to Salesforce Upsert component:


org.mule.runtime.core.internal.message.ErrorBuilder$ErrorImplementation
{
  description=org.mule.runtime.api.metadata.TypedValue cannot be cast to java.util.Map
  detailedDescription=org.mule.runtime.api.metadata.TypedValue cannot be cast to java.util.Map
  errorType=MULE:UNKNOWN
  cause=java.lang.ClassCastException
  errorMessage=-
  childErrors=[]
}

Below attached is a mule 4 sample project that will take the a input csv file and filter it to find adults and then print the adults name using a for each scope in a batch of 5 people each. Using a debugger the payload media type can be checked before and inside for each block.

Now inside for each I have used a Logger so the typedValue is not creating an issue, but if we used Salesforce component we would get an error as mentioned above. Since I do not have a SF account, I am unable to create the exact scenario. Sorry for the inconvenience but this will prove the conversion of media type inside for each.

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
    xmlns:file="http://www.mulesoft.org/schema/mule/file"
    xmlns="http://www.mulesoft.org/schema/mule/core"
    xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
    <file:config name="File_Config" doc:name="File Config" doc:id="780eb98e-37d0-48ae-8f6b-47b8782284c9" >
        <file:connection workingDir="${app.home}" />
    </file:config>
    <flow name="bbazaz-json2csv-generatorFlow" doc:id="eebb7bcb-04ba-4c2a-bc7a-924d844c6aa2" >
        <scheduler doc:name="Scheduler" doc:id="f05eafdb-3cf0-43e2-9797-bedb9c4bb04a" >
            <scheduling-strategy >
                <fixed-frequency frequency="1" timeUnit="DAYS"/>
            </scheduling-strategy>
        </scheduler>
        <file:read doc:name="Read" doc:id="b1e9b51e-ab43-4965-ac0a-10c86c72b425" config-ref="File_Config" path="TestFile/InputData.csv" outputMimeType="application/csv; header=true" outputEncoding="UTF-8"/>
        <logger level="INFO" doc:name="Log Data" doc:id="74f972dc-9025-4eb7-916f-9c8dcaef4c7e" message='#["JSON Data Received"]'/>
        <ee:transform doc:name="Filter Adults and Minors" doc:id="6dfbe838-f508-4904-a651-8285b478faa7" >
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
output application/java
---
payload reduce((item, acc = {
    'adultsList': [],
    'minorsList': []
}) -> (
    if ( (item.PersonsAge as Number) >= 18 ) {
    adultsList: (acc.adultsList default []) << {
        'Name': item.PersonName
    },
    minorsList: acc.minorsList default []
}
    else{
    minorsList: (acc.minorsList default []) << {
        'Name': item.PersonName
    },
    adultsList: acc.adultsList default []
}))]]></ee:set-payload>
            </ee:message>
        </ee:transform>
        <foreach doc:name="For Each" doc:id="ce464082-620c-41ee-8150-45264c5cb57a" collection="#[payload.adultsList]" batchSize="5">
            <logger level="INFO" doc:name="Logger" doc:id="d0953955-6c4c-4374-aed4-ee0155d8e5d5" message='#[(payload.Name joinBy(",")) ++ "is an adult"]'/>
        </foreach>
    </flow>
</mule>

Sample CSV File :

PersonName,PersonsAge
Jonas,36
Ulysses,36
Lane,31
Tate,30
Rogan,36
Bruno,37
Colt,25
Colby,34
Grady,24
Hyatt,39
Victor,20
Cade,16
Brady,34
Ulric,33
Xavier,40
Cameron,36
0

There are 0 best solutions below