cancel
Showing results for 
Search instead for 
Did you mean: 

Nested ternary expression using Match operator

aaronb
New Contributor

Expression:
parseFloat($InvoiceTotal1.trim()) < 0 && $CategoryDescription.trim() == “INVOICE ADJUSTMENTS” ? parseFloat($InvoiceAdjustments.trim()) : (parseFloat($InvoiceTotal1.trim()) > 0 && $CategoryDescription.trim() == “INVOICE ADJUSTMENTS” ? parseFloat($InvoiceAdjustments.trim()) : (parseFloat($InvoiceTotal1.trim()) < 0 ? parseFloat($CategorySalesValue.trim())+parseFloat($CategoryDiscountAllowance.trim()) : parseFloat($CategorySalesValue.trim())+parseFloat($CategorySalesTax.trim())))

Description:
Hi,
I am using the above ternary expression to handle essntially four different conditions. I used a ternary expression for timing purposes but long term I think it would make sense to use the match operator or something similar. I explored this option but never could get the syntax correct. If anyone has some experience with something similar that they could help point me in the right direction I would greatly appreciate it!

Thank you

Sample:
See above

Release used:
4.16

2 REPLIES 2

cjhoward18
Employee
Employee

Hi Aaron,

Try this expression below:

"match(parseFloat($InvoiceTotal)) {

val if val < 0 && $CategoryDescription.trim() == “INVOICE ADJUSTMENTS” => parseFloat($InvoiceAdjustments.trim()),

val if val > 0 && $CategoryDescription.trim() == “INVOICE ADJUSTMENTS” => parseFloat($InvoiceAdjustments.trim()),

val if val < 0 =>
parseFloat($CategorySalesValue.trim()) + parseFloat($CategoryDiscountAllowance.trim())

_ =>
parseFloat($CategorySalesValue.trim()) + parseFloat($CategorySalesTax.trim())
}"

the last case (_) is the default case.

note that val is used to capture your match value.

Also note you could combine branch 1 and 2 since they return the same value and conjoin them with an OR.

Thanks Cole. I will give that a try and see what I get.

f464dd1c7c382c015b3fc53eebe1cd1aadffee61.jpeg