Conditional saving on buttons

Is it OK to use this approach for conditional multiple rule input saving on button component?

a!buttonWidget(
            label: "Go Back",
            value: local!activeStep - 1,
            saveInto: {
              local!activeStep,
              if(
                local!activeStep = 1,
                a!save(
                  ri!employee,
                  null
                ),
                null
              ),
              if(
                local!activeStep = 1,
                a!save(
                  ri!address,
                  null
                ),
                null
              ),
              if(
                local!activeStep <> 1,
                a!save(
                  ri!address,
                  rule!EEDM_GetAddressDetailsById(
                    local!startAddressId
                  ).data
                ),
                null
              ),
              if(
                local!activeStep <> 1,
                a!save(
                  ri!employee.addressId,
                  local!startAddressId
                ),
                null
              ),
              if(
                local!activeStep = 2,
                a!save(
                  ri!addressUpdChoice,
                  null
                ),
                null
              )
            }

This is from milestone template which uses the same button on most steps and I need to do specific actions depending on which step I'm on.

To me, it seems fine to use IF conditions in this way and it works fine so far.

However, I haven't run into some examples of this here.

Am I using something undocumented or my "works fine" is just coincidence?

  Discussion posts and replies are publicly visible

  • +1
    Certified Lead Developer

    Nope, you're following the paradigm perfectly.

    The saveInto parameter takes an array of targets or a!save() function calls.  All your if()'s do is return either an a!save() or a null.  If saveInto ignores the nulls, you can just act like your if's just return a!saves.  So your list of if()'s just becomes a list of a!save(), exactly what saveInto wants. 

    Nothing in the whole language cares how you arrive at the data as long as it's the type that it's looking for.  It just abstracts all function calls to be nothing more than whatever their output is.  You have a function whose output is an a!save(); that's good enough for saveInto().  It does not matter what it is. 

    But also, there is essentially no place in the whole language where you could not use if(), and essentially no place where you should not.  It's for everywhere, but so is almost everything else.  You could use a!forEach() to generate your list of a!save()'s there too.  I think I have before.  Or the apply() function, though that might as well be deprecated, or a helper rule, or the reduce() function, or a host of other things.  The only condition is the output of whatever it is happens to be a!save()s.

    It's no coincidence.  You have reached a higher level of understanding of how to use the expression language.  

  • +1
    Certified Lead Developer

    This looks correct, though I think there's a chance you might have issues unless you replace "null" with an empty set {} for the non a!save portion of the if statement.  "saveInto" requires a list of saves, and items in the list that return an empty set are safely ignored, but items in the list that return "null" will show up as an entry in the list, which Appian may or may not handle properly right now.

  • +1
    Certified Lead Developer
    in reply to Mike Schmitt

    One other thing I spotted is what looks like 2 if()'s that have the same condition.  You could just make 1 if() that returns an array of those two outputs on the same "true" condition.

    The standard is the empty set {} as the 'false' condition for most if()s I've seen.  When the output is strings, it's often "" the empty string.

  • 0
    A Score Level 1
    in reply to Dave Lewis

    Thank you, guys! It is a true joy to learn Appian with such a great community! :)