[resolved] tMap Expression Builder - multiple else if

One Star

[resolved] tMap Expression Builder - multiple else if

Hi all,
I have a simple project with a tFileInputDelimited, tMap, and tMysqlOutput components. I'm trying to build a calculated field in the tMap component Expression builder, but all my attempts have failed to compile at this point. What I'm doing is fairly straight forward, in php it would look something like this:
if ($custPrice>0) {
// sale
if ($promoCode=='GP') {
$revenueType='G';
} else {
$revenueType='S';
}
} else if ($custPrice<0) {
// return/refund
$revenueType='R';
} else {
// no charge
if ($promoCode=='GR') {
$revenueType='D';
} else if ($productTypeIdentifier=='7' || $productTypeIdentifier=='7T' || $productTypeIdentifier=='7F') {
// upgrade
$revenueType='U';
} else if ($productTypeIdentifier=='1' || $productTypeIdentifier=='1T' || $productTypeIdentifier=='1F') {
// comp
$revenueType='C';
} else {
// other! not expected
$revenueType='O';
}
}

Basically the revenueType G, S, R, U or C is what I want to return to the output column. I seem to be able to do individual row1.column.equals lines properly - Is it possible to do in one expression in Talend? Or a better way to handle this?
Thanks!

Accepted Solutions
Seven Stars

Re: [resolved] tMap Expression Builder - multiple else if

Simply
row1.custPrice>0
?row1.promoCode.equals("GP")
?"G"
:"S"
:row1.custPrice<0
?"R"
:row1.promoCode.equals("GR")
?"D"
:row1.productTypeIdentifier.matches("7|7T|7F")
?"U"
:row1.productTypeIdentifier.matches("1|1T|1F")
?"C"
:"O"

The whitespace is irrelevant but included for ease of reading. I've used a regex .matches comparison rather than multiple .equals comparison for simplicity.

All Replies
One Star

Re: [resolved] tMap Expression Builder - multiple else if

Hi
The convenientest way is to create a custom routine(a java method) in Talend Studio.
Regards,
Pedro
Seven Stars

Re: [resolved] tMap Expression Builder - multiple else if

Simply
row1.custPrice>0
?row1.promoCode.equals("GP")
?"G"
:"S"
:row1.custPrice<0
?"R"
:row1.promoCode.equals("GR")
?"D"
:row1.productTypeIdentifier.matches("7|7T|7F")
?"U"
:row1.productTypeIdentifier.matches("1|1T|1F")
?"C"
:"O"

The whitespace is irrelevant but included for ease of reading. I've used a regex .matches comparison rather than multiple .equals comparison for simplicity.
One Star

Re: [resolved] tMap Expression Builder - multiple else if

alevy - thanks so much - that did the trick!