Skip to main content
February 23, 2022
Solved

Save duplicate value with submit button

  • February 23, 2022
  • 3 replies
  • 0 views

Hi,

i have a list of items I want to save to the data.

The output should be ID=4, ID=5, ID=6

However, when submitted,  it duplicates first ID:  ID=4, ID=4, ID=4.

please help.

thanks

a!localVariables(
  local!list: { 4, 5, 6 },
  a!formLayout(
    label: "Form",
    buttons: a!buttonLayout(
      primaryButtons: {
        a!buttonWidget(
          label: "Submit",
          saveInto: {
            a!save(

              target:ri!Data.ID,

              value:a!forEach(items: local!list, expression: fv!item)
            )
          },
          submit: true,
          style: "PRIMARY"
        )
      },
      secondaryButtons: {
        a!buttonWidget(
          label: "Cancel",
          value: true,
          saveInto: ri!cancel,
          submit: true,
          style: "NORMAL",
          validate: false
        )
      }
    )
  )
)

Best answer by mikes0011

Is "ri!Data" an Array?  Because you're basically telling your Submit button to write into a (generic, non-array-indexed) ID field of ri!Data, each of the values in local!list in succession.  This will have unpredictable results as-written, even if you pre-populate the array with 3 empty elements.  And if it's not meant to be an array then I'm just lost.

Going out on a limb a bit (since you didn't really make this clear), I'm guessing what you might really want to end up with is a list of ri!Data elements where the ".ID" properties of that list end up looking like the local!list items (and would cleanly follow the number of items in local!list).  My recommended approach for that is always what I usually describe as "assembling your array first, then saving it into its desired location".

a!localVariables(
  local!list: { 4, 5, 6 },
  
  a!formLayout(
    label: "Form",
    
    buttons: a!buttonLayout(
      primaryButtons: {
        a!buttonWidget(
          label: "Submit",
          saveInto: {
            a!save(
              target: ri!Data,
              value: a!forEach(
                items: local!list,
                
                /* this should create a new list of dictionaries containing the above values, then (over)write them into ri!Data. */
                expression: {
                  ID: fv!item
                }
              )
            )
          },
          submit: true,
          style: "PRIMARY"
        )
      }
    )
  )
)

3 replies

mikes0011
mikes0011Answer
Brainy
February 23, 2022

Is "ri!Data" an Array?  Because you're basically telling your Submit button to write into a (generic, non-array-indexed) ID field of ri!Data, each of the values in local!list in succession.  This will have unpredictable results as-written, even if you pre-populate the array with 3 empty elements.  And if it's not meant to be an array then I'm just lost.

Going out on a limb a bit (since you didn't really make this clear), I'm guessing what you might really want to end up with is a list of ri!Data elements where the ".ID" properties of that list end up looking like the local!list items (and would cleanly follow the number of items in local!list).  My recommended approach for that is always what I usually describe as "assembling your array first, then saving it into its desired location".

a!localVariables(
  local!list: { 4, 5, 6 },
  
  a!formLayout(
    label: "Form",
    
    buttons: a!buttonLayout(
      primaryButtons: {
        a!buttonWidget(
          label: "Submit",
          saveInto: {
            a!save(
              target: ri!Data,
              value: a!forEach(
                items: local!list,
                
                /* this should create a new list of dictionaries containing the above values, then (over)write them into ri!Data. */
                expression: {
                  ID: fv!item
                }
              )
            )
          },
          submit: true,
          style: "PRIMARY"
        )
      }
    )
  )
)

The Expression Guru
kend0001Author
February 23, 2022

Thanks, it works perfectly

mikes0011
Brainy
February 23, 2022

Great, thanks for confirming :)

The Expression Guru