Values not updating

Hi, 

I'm trying to create a basic interface, but I'm having some problems with the variables not updating as I expect them to.

Here is a code snippet:

 

load(
    local!date: today(),
    local!dateRange: rule!getWeekDateArray(local!date),
    a!formLayout(
        contents: {
            a!boxLayout(
                contents: {
                    with(
                        {
                            a!dateField(
                                label: "Date picker",
                                value: local!date,
                                saveInto: {
                                    local!date,
                                    a!save(target: local!dateRange, value: rule!getWeekDateArray(local!date))
                                }
                            ),
                            a!paragraphField(
                                label: "Dates",
                                value: local!dateRange
                            )
                        }
                    )
                }
            )
        }
    )
)

Once the user selects a date, the paragraph field should update with a list/array of values that represent the days of the month for a given week. For example, suppose the user selects the 17th of January 2019, the values listed will be "13; 14; 15; 16; 17; 18; 19" because these correspond with the days of the week for the selected date.

I have an expression rule that takes a date and returns this list (rule!getWeekDateArray()). I've tested the expression rule and it works as expected. For some reason, when the user changes the date, the array (and paragraph field) is only updated once. After that, it doesn't update any more - changes to the date picker have no effect. I have a feeling it has something to do with the way I declare/use local variables inside with() or load() but I can't figure it out. 

Here is my getWeekDateArray() expression rule:

 

load
(local!dateRange: choose(
  weekday(
    ri!date
  ),
  /* 1 */
  {
    left(ri!date, 2),
    left(ri!date + 1, 2),
    left(ri!date + 2, 2),
    left(ri!date + 3, 2),
    left(ri!date + 4, 2),
    left(ri!date + 5, 2),
    left(ri!date + 6, 2)
  },
  /* 2 */
  {
    left(ri!date - 1, 2),
    left(ri!date, 2),
    left(ri!date + 1, 2),
    left(ri!date + 2, 2),
    left(ri!date + 3, 2),
    left(ri!date + 4, 2),
    left(ri!date + 5, 2)
  },
  /* 3 */
  {
    left(ri!date - 2, 2),
    left(ri!date - 1, 2),
    left(ri!date, 2),
    left(ri!date + 1, 2),
    left(ri!date + 2, 2),
    left(ri!date + 3, 2),
    left(ri!date + 4, 2)
  },
  /* 4 */
  {
    left(ri!date - 3, 2),
    left(ri!date - 2, 2),
    left(ri!date - 1, 2),
    left(ri!date, 2),
    left(ri!date + 1, 2),
    left(ri!date + 2, 2),
    left(ri!date + 3, 2)
  },
  /* 5 */
  {
    left(ri!date - 4, 2),
    left(ri!date - 3, 2),
    left(ri!date - 2, 2),
    left(ri!date - 1, 2),
    left(ri!date, 2),
    left(ri!date + 1, 2),
    left(ri!date + 2, 2)
  },
  /* 6 */
  {
    left(ri!date - 5, 2),
    left(ri!date - 4, 2),
    left(ri!date - 3, 2),
    left(ri!date - 2, 2),
    left(ri!date - 1, 2),
    left(ri!date, 2),
    left(ri!date + 1, 2)
  },
  /* 7 */
  {
    left(ri!date - 6, 2),
    left(ri!date - 5, 2),
    left(ri!date - 4, 2),
    left(ri!date - 3, 2),
    left(ri!date - 2, 2),
    left(ri!date - 1, 2),
    left(ri!date, 2)
  }
),
local!dateRange
)

Any help on this would be great!

Regards, 

Ben

  Discussion posts and replies are publicly visible

Parents Reply Children
  • +1
    Certified Lead Developer
    in reply to fabiant

    The error you're experiencing is caused by your use of load() within the getWeekDateArray() expression rule. I recommend to everyone never to use load() in an expression rule unless you have a specific reason to.  What's happening is that the load() variable is being set within your expression rule the first time it's called, and subsequent times the expression rule is called, it's using the original load() value, contrary to what you might be expecting it to do.

    If you swap out the "load" for a "with" in the rule, I'm guessing you'll find it works right away.

    As a further suggestion, if you were to simply use a with() variable in your interface, to hold the value of local!dateRange, you wouldn't need to have the extra saveInto in your dateField component. (You would still need to do the aforementioned fix to the expression rule, though.)  My suggested implementation of this is as follows:

    load(
      local!date: today(),
      
      with(
        local!dateRange: rule!getWeekDateArray(local!date),
        
        a!formLayout(
          contents: {
            a!boxLayout(
              contents: {
    						/* removed the unnecessary with() call here, as it wasn't doing anything */
                a!dateField(
                  label: "Date picker",
                  value: local!date,
                  saveInto: {
                    local!date
                    /* a!save(target: local!dateRange, value: rule!getWeekDateArray(local!date)) */
                  }
                ),
                a!paragraphField(
                  label: "Dates",
                  value: local!dateRange
                ) 
              }
            )
          }
        )
      )
    )