Reputation: 3020
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>
Upvotes: 1
Views: 7903
Reputation: 33413
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.
Upvotes: 7