VKP
VKP

Reputation: 658

How to Return String Array in a Mule 4 Function?

Hi i am working on a Migration project from Spring to Mule .

In Java we have small function like

 private String[] getPurpose(loanPurpose){
    String purpose[] = new String[2];
    if ("REFINANCE".equalsIgnoreCase(loanPurpose)) {
        purpose[0] = "Refinance";
        purpose[1] = "Cash-Out";
        return purpose;
    } 
    return null;
}

In Mule i am writing it like below .

fun getPurpose(data) = 
if(upper("PURCHASE") == data) 
// how i can assign the values in string array and return 

enter image description here

Upvotes: 0

Views: 952

Answers (2)

Salim Khan
Salim Khan

Reputation: 4303

It should work fine in studio as well. Probably the way the if value is represented could be throwing it off.. Try with the following:

%dw 2.0
output application/json

fun getPurpose(data) = 
if(upper("PURCHASE") == data) (["Refinance", "Cash-Out"]) else if (1 == 2) "abc" else "nothing"
---
getPurpose("PURCHASE")

Output:

[
  "Refinance",
  "Cash-Out"
]

From my local DW playground

Upvotes: 1

aled
aled

Reputation: 25837

[ "Refinance"," Cash-out"]

Just create the array literal with the strings as the last value of the function. I'n this case the is the value of the true branch of if.

Upvotes: 2

Related Questions