2

In Mulesoft I have an ArrayList stored as an outbound property with one element. It looks like this: enter image description here

But if I try to access the size of this array I get an error and can't figure out why: enter image description here

The error is

[Error: object is not an instance of declaring class]
[Near : {... message.outboundProperties.crm ....}]
             ^
[Line: 1, Column: 1]

And here's a flow that throws the same error:

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

<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:quartz="http://www.mulesoft.org/schema/mule/quartz" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd
http://www.mulesoft.org/schema/mule/quartz http://www.mulesoft.org/schema/mule/quartz/current/mule-quartz.xsd">
    <quartz:connector name="Quartz" validateConnections="true" doc:name="Quartz"/>
    <flow name="tempFlow">
        <quartz:inbound-endpoint jobName="job1" repeatInterval="1" repeatCount="0" connector-ref="Quartz" name="runOnce" doc:name="Quartz">
            <quartz:event-generator-job>
                <quartz:payload>foo</quartz:payload>
            </quartz:event-generator-job>
        </quartz:inbound-endpoint>
        <message-properties-transformer doc:name="Message Properties">
            <add-message-property key="crmRequests" value="#[[]]"/>
        </message-properties-transformer>
        <dw:transform-message doc:name="Copy_of_buildUpdateRequest">
            <dw:set-payload><![CDATA[
%dw 1.0
%output application/java
---
{
    statecode: 0
}
]]></dw:set-payload>
        </dw:transform-message>
        <expression-component doc:name="Copy_of_Expression"><![CDATA[#[message.outboundProperties.crmRequests.add(payload)]]]></expression-component>
        <set-payload value="#[message.outboundProperties.crmRequests.size()]" doc:name="Set Payload"/>
        <logger level="INFO" doc:name="Logger"/>
    </flow>
</mule>
CamJohnson26
  • 1,119
  • 1
  • 15
  • 40

4 Answers4

3

Wrap the expression inside bracket:

<set-payload value="#[(message.outboundProperties.crmRequests).size()]" doc:name="Set Payload"/>

For further testing you can try other java.util.ArrayList methods, e.g.: (message.outboundProperties.crmRequests).get(0)

sulthony h
  • 1,279
  • 1
  • 8
  • 9
  • Awesome, this worked! Any idea why it has to be in brackets? – CamJohnson26 Jul 01 '16 at 14:38
  • Just from my observations, mules seems to sometimes have difficulty with chaining. Using intermediate values or brackets seems to eliminate it. – dlb Jul 01 '16 at 22:46
2

If you have set the ArrayList to be stored in crmRequests. Why in the debug view it is showing the entire #[[message.outboundProperties.crmRequest] seems something wrong. It should appears in outboundProperties tab with single name crmRequests with the type of ArrayList. Check that out.

Also try setting with #[message.outboundProperties.'crmRequests'.size()] or #[message.outboundProperties.['crmRequests'].size()].

Edit: Sample Flow, have done a small change set the outboundProperties inside groovy itself instead of using MessageProperties and then with expression component.

<flow name="checkFlow">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
    <dw:transform-message doc:name="Transform Message">
        <dw:set-payload><![CDATA[%dw 1.0
 %output application/java
 ---
{
 statusCode:0
}]]></dw:set-payload>
    </dw:transform-message>
    <scripting:component doc:name="Groovy">
        <scripting:script engine="Groovy"><!  [CDATA[message.setOutboundProperty('crmRequests',payload);
return payload;]]></scripting:script>
    </scripting:component>
     <set-payload value="#[message.outboundProperties.crmRequests.size()]" doc:name="Set Payload"/>

    <logger  level="INFO" doc:name="Logger"/>
</flow>
star
  • 1,493
  • 1
  • 28
  • 61
2
#[message.outboundProperties.crmRequests.size()]

Mule was unable to resolve above MEL. Follow this two step approach for desired results. Use:

<set-variable variableName="crmRequestsList" value="#[message.outboundProperties.crmRequests]" doc:name="Variable" />
<set-payload value="#[flowVars.crmRequestsList.size()]" doc:name="Set Payload"/>

Instead of:

<set-payload value="#[message.outboundProperties.crmRequests.size()]" doc:name="Set Payload"/>
0

You can set a variable and assign value to it and call it in your payload you can check it there.