Is there a way to `DECLARE` variables inside of a ...
# suiteanalytics
d
Is there a way to
DECLARE
variables inside of a saved search formula? Using something like
DECLARE myInt integer = 1
just gives
ERROR: Invalid Expression
k
why would you need to declare it? Just use it?
d
long winded calculation that was repeated. simplified example:
CASE WHEN {field1}+{field2}-({field3}+{field4}) > 0 THEN NVL({field5},{field1})+{field2}-({field3}+{field4}) ELSE 0 END
I've just done it in full, but was trying to do something like:
DECLARE myInt integer = {field2}-({field3}+{field4})
CASE WHEN {field1}+myInt > 0 THEN NVL({field5},{field1})+myInt ELSE 0 END
k
Yeah, that'd be nice - if you ever figure it out be sure to share it. You'd probably have to comment it out using the trick from the running total search in order to do variable declaration
d
haha, tried that briefly (didn't work):
DECLARE/*magic*/ myInt integer = {field2}-({field3}+{field4})
in it's entirety the formula's ugly, but now works ¯\_(ツ)_/¯ FWIW, the formula is to replicate the "Order Items" page's "To Order" value in a saved search… 🤦
k
might wrap NVLs around several of your inputs.
but yeah - I've actually had to do the same thing before for a client
z
Hard-coding constants and complicated business logic into Saved Search formulas may not be the best thing for "future you" to maintain. On the other hand, hey, you're just trying to get something done, I appreciate that. In that vein, you could define custom fields with non-stored default values for the constants you require in context. I know that sounds weird, but it's cheap, and from the perspective of solution architecture, encapsulating these constants in purpose built custom fields makes them easier to find, potentially self-documenting (via the field names and description), etc.
👍 1