Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I got a Mule flow with 2 queries. The first simply validates if a username is duplicated.

Does something like this: SELECT count(*) AS TOTAL FROM user_tests WHERE username = #[json:username] AND test_message_title = #[json:test_message_title]

The second one does an INSERT if TOTAL = 0.

Now, i want to use the variable "TOTAL" in a choice inside the same flow. How do i do it?

Tried #[TOTAL=0], tried #[variable:TOTAL=0], and many others but couldn't make it work.

EDITED: I made a java component that retrieve the "0" or whatever number from the payload. How do i make the choice expression to compare it?

Tried using Integer.valueOf(message.payload) = 0 in the choice expression but i get an InvalidExpressionException.

This is my entire flow:

<flow name="ADMIN_INSERT_TEST_FILE" doc:name="ADMIN_INSERT_TEST_FILE">
    <ajax:servlet-inbound-endpoint channel="/admin/save_test_message" responseTimeout="10000" doc:name="Ajax"/>
    <set-variable variableName="#['saved_payload']" value="#[payload]" doc:name="Save original payload"/>
    <jdbc:outbound-endpoint exchange-pattern="request-response" queryKey="validate_test_name" queryTimeout="-1" connector-ref="ExtendedRoutingConnector" doc:name="validate duplicated test name">
        <jdbc:query key="validate_test_name" value="SELECT count(*) AS TOTAL FROM user_tests WHERE username = #[json:username] AND test_message_title = #[json:test_message_title]"/>
    </jdbc:outbound-endpoint>
    <logger message="#[message.payload[0].TOTAL]" level="INFO" category="Total" doc:name="Logger"/>
    <choice doc:name="Choice">
        <when expression="#[message.payload[0].TOTAL == 0]">
            <processor-chain>
                <set-payload value="#[variable:saved_payload]" doc:name="Save Payload"/>
                <jdbc:outbound-endpoint exchange-pattern="request-response" queryKey="insert_user_test_message" queryTimeout="-1" connector-ref="ExtendedRoutingConnector" doc:name="insert user test message">
                    <jdbc:query key="insert_user_test_message" value="INSERT INTO user_tests (username, test_message_title, user_test_message) VALUES (#[json:username], #[json:test_message_title], #[json:message])"/>
                </jdbc:outbound-endpoint>
            </processor-chain>
        </when>
        <otherwise>
            <processor-chain>
                <logger message="Name #[json:test_message_title] already exists" level="INFO" doc:name="Logger"/>
            </processor-chain>
        </otherwise>
    </choice>
</flow>

Thanks in advance.

share|improve this question
Replace variableName="#['saved_payload']" with variableName="saved_payload". – David Dossot Mar 12 at 18:33
add comment (requires an account with 50 reputation)

1 Answer

up vote 2 down vote accepted

Use MEL:

#[message.payload[0].TOTAL == 0]

This will retrieve the value of the TOTAL column from the first row returned by your SELECT query.

share|improve this answer
Got an error Caused by: [Error: could not access property: message.payload[0].TOTAL]; parent is null: message.payload[0].TOTAL]] [Near : {... Unknown ....}] Can i do something like: #[message.payload[0].TOTAL] = 0 ?? – msqar Mar 12 at 18:16
Show your flow / SQL endpoint config, otherwise impossible to help you. – David Dossot Mar 12 at 18:18
Edited with the entire Mule flow ;) thanks for the help David and quick response. – msqar Mar 12 at 18:20
Use == for comparison, = is for assignment. – David Dossot Mar 12 at 18:24
Thrown: Caused by: org.mule.api.MessagingException: Failed to process Expression Evaluation "json:test_message_title" (org.mule.api.MuleRuntimeException). Message payload is of type: ArrayList – msqar Mar 12 at 18:29
show 6 more commentsadd comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.