if (function)

Certified Senior Developer

Can some one explain how the below function evaluates, appreciate your help in advance!!

if(true, false, false, true, false, 30, 40) 

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    the if() function can handle if/else if/else functionality inherently.  If given more than 3 parameters (if/then/else), the subsequent odd-numbered parameters [except the final one] will each be evaluated as a new condition.

    if(
      ri!colorIndex = 1,
      "Red",
      
      ri!colorIndex = 2,
      "Green",
      
      ri!colorIndex = 3,
      "Blue",
      
      "Something else..."
    )

    For example, the above code will evaluate to "Red" for an input of 1, "Green" for an input of 2, "Blue" for an input of 3, and for any other input value, it will return the default return value of "Something else..."

    NOTE: as mentioned down-thread, this functionality is not officially supported or documented, so use it with caution if at all. (/edit)

  • 0
    Certified Senior Developer
    in reply to Mike Schmitt

    Is this a supported "feature" as it does not appear to be mentioned on the help page?

  • I'm with you, Gavin - if it's not in the documentation then it is not officially supported. I've encountered a similar 'bending' of the index() function where you can provide a list of values as way of navigating a tree-structure, and having asked Appian Engineering about this I had the same response - the documentation says it only takes 3 parameters and that's how far the official support will take you.

  • 0
    Certified Lead Developer
    in reply to Gavin Hume

    Appian can go one of three ways about this:

    1. Fully document it

    2. Alter if() so it no longer behaves this way

    3. Do nothing and leave this question in Limbo

    Given the fact that at least some portion of Appian code out there, who knows how much, runs because someone figured out if() works this way, I would think option 2 might break an untold number of applications.  Appian wouldn't want to do this, so I like option 1 and really like the odds of option 3 happening.

  • 0
    Certified Lead Developer
    in reply to Gavin Hume

    Well to be fair, I was describing how it *does work*, especially given the context of the original question.  That's not to say whether it's officially documented / supported / not supported / etc.  I agree that anyone who uses this functionality should do so with caution.

    As an aside, I'd consider the error message you get when entering an invalid number of parameters fairly official:

  • 0
    Certified Lead Developer
    in reply to Dave Lewis

    Agreed - it's used this way in at least a few places in a legacy application I've inherited, in a way that would be a pretty big pain to need to retroactively clean up.  I personally hope they'll just make this functionality official, because all-in-all it's a far less messy way of doing if/then/else functionality, as opposed to the traditional style of using infinite levels of nesting, which just looks nasty.

  • 0
    Certified Lead Developer
    in reply to Mike Schmitt

    I remember code I made one time that wound up with dozens of close parenthesis at the end.

    Unfortunately, you can get yourself into highly unreadable space either way when you need an if() to decide which of two if functions you need to run.

    if(
    firstCondition = true,
    if( secondCondition = true, "output", "other output"),
    if( thirdCondition = true, "something different", "something completely diffferent")
    )
    
    /*or equally valid*/
    
    if(
    firstCondition = true,
    if ( secondCondition = true, "output, "other output"),
    thirdCondition = true,
    "something different",
    "something completely different"
    ) /*Wait....so the first one needs an if() all to itself but not the second one? Yep*/

    It just feels like there's always going to be a way to make this painful to anyone who has to maintain your code.

  • I strongly recommend against using the function in this way since Appian's Engineering team does not document it as supported syntax. Keep in mind that there is likely a reason it is not supported officially - maybe it works in 99% of scenarios but doesn't work in one or two edge cases? It's not worth depending on a feature that isn't documented.

    Instead, there are plenty of alternates that could work if you are trying to perform an if/then/else. Plus, the beauty of SAIL is that you can create reusable rules that include the logic you need. Here's an example:

    choose(
      where(
        {
          ri!colorIndex = 1,
          ri!colorIndex = 2,
          ri!colorIndex = 3,
          true
        }
      )[1],
      "Red",
      "Green",
      "Blue",
      "Something Else..."
    )

    Or, here's a reusable rule called rule!IfThenElse that you could call whenever you want an if/then/else statement:

    index(
      ri!results,
      where(
        append(ri!conditions, true)
      )[1],
      ri!elseResult
    )

    Or there's even a lot of scenarios where a decision rule would solve it for you.

    Either way, use the power that Appian does provide instead of trying to rely on functionality that isn't documented.

  • 0
    Certified Senior Developer
    in reply to Peter Lewis

    Thank you Peter, much appreciated!

Reply Children
No Data