Expression editor - switch instead if soup

Expression editor - switch instead if soup

  

I found my self using expression editors like this "if soups":

If(X = A, "class-a",
If(X = B, "class-b"
If(X = C, "class-c"
If(X = ..., "class-...", "")
)
)
)

There is no switch to use in expression editor? I would prefer in expression because switch (in an action flow) with several options are sometimes more confusing. An alternative is javascript, but need to create more input to pass to javascript. It would be simpler when you just want to do the same as a "if", but a switch would be simpler.

Hi João,


Why not using the Switch Object /component provided by the platform instead that many "IF's" statments ? Seems a bit more clean.



Best Regards,

Diogo Romero

"Switch Object /component" is like this right? As I said, it looks more simple to me to use an expression with ifs. Just one screen and all options, if I need to build/edit is simpler, I think.

PS: Its a question more about readability and be more practical. ;)

Solution

Hi João,


So if you really prefer not using the Switch tool, you need to create a work around. There is no SWITCH CASE in expression editor.

What if you create a function where you pass in your values and then use the built in switch statement to determine the return value ? Your page would be "clean" as you wish and all the logic would be in the function.


Best Regards,

Diogo Romero

Solution

As function, you are saying about "javascript", right? As I said in the open post, is a possibility, but I need to do one or two steps mores (inputs variables, etc...). I was just looking for a lazy solution. :D

But thanks! I just wanted to confirm if there is no Switch option in expression editor. ;)

João Saro wrote:

As function, you are saying about "javascript", right? As I said in the open post, is a possibility, but I need to do one or two steps mores (inputs variables, etc...). I was just looking for a lazy solution. :D

But thanks! I just wanted to confirm if there is no Switch option in expression editor. ;)

Hi again,


No mate, i never talked about javascript. Just create one "Outsystems" function, like the example bellow.


Best Regards,

Diogo Romero

Yeah, but again... it would be the same issue. I was just trying to simplify, but no problem. :p

Hi João,

I think you're thinking too much like a high-code coder :). Remember that the goal in a visual environment like Service Studio is to be able to easily see the program flow. If you put switches in expressions, it's more difficult to see what's going on! Also, if you often have such logic (i.e. assigning multiple values based on multiple inputs), you might consider a different strategy (e.g. using Static Entities).