Skip to main content
July 3, 2023
Solved

Assignment to local variables in Expression rule

  • July 3, 2023
  • 9 replies
  • 0 views

Hi,

I'm trying to save an array value to a local variable, it looks like getting saved, but when I check for the length of the local variable, I'm getting as 0. Where as when I save the same array during declaration of the local variable, I'm getting the length as expected. 

What I'm missing here ? what is the proper way of saving into local variables after declaring them. I've tried using a!save() but no luck.. :( 

Below are the snaps for your reference.

Assigning array after initialization 

Assigning array during initialization

 

TIA :)

Best answer by mikes0011

There are mainly only 2 places where you can "assign a value" to a local variable:

1) at declaration

2) as the result of a user interaction in an interface component

Seeing as this is an expression rule, and therefore #2 is irrelevant, you will need to assign the value at declaration (#1). 

This is the most important concept here: There is no such thing, in Appian's expression language, as "define a blank local variable then in a later line of code give it a new value".  If you know its initial value, then you will assign it at creation time.

9 replies

mikes0011
mikes0011Answer
Brainy
July 3, 2023

There are mainly only 2 places where you can "assign a value" to a local variable:

1) at declaration

2) as the result of a user interaction in an interface component

Seeing as this is an expression rule, and therefore #2 is irrelevant, you will need to assign the value at declaration (#1). 

This is the most important concept here: There is no such thing, in Appian's expression language, as "define a blank local variable then in a later line of code give it a new value".  If you know its initial value, then you will assign it at creation time.

July 3, 2023

Thanks Mike for your input. 

  If you know its initial value, then you will assign it at creation time. --  what if I don't know the value of the variable during initialization and the value would come after initialization as a result of some equation like we do in traditional programming. how to handle such scenarios.

mikes0011
Brainy
July 3, 2023
the value would come after initialization as a result of some equation

You would declare its value after any other calculations (usually declared as their own local variables) on which it depends.

mathieud0001
Brainy
July 3, 2023

I think maybe having that variable being the key in the dictionary might be doing weird things.

Try this:

a!localVariables(
  local!parts: {
    a!map(
      partName: "part1",
      quantity: 1,
      unitCost: 1
    ),
    a!map(
      partName: "part2",
      quantity: 1,
      unitCost: 1
    )

  },
  {
    parts: local!parts,
    length: length(local!parts)
  }
)