Multiply two integer fields and save the results to another field.

I'm trying to multiply the values stored in two integer fields and save the results in another integer field. I'm not sure if I have the correct syntax for the multiplier in my code or if my saveInto statement is correct. The Total estimated capacity field is not displaying the results of the value in the Number of files field multiplied by the value in the Average file size field  This is my code:

a!integerField(
  label: "Total estimated capacity",
  value: ri!totalEstCapacity,
  saveInto: {
    a!save(ri!totalEstCapacity,ri!numberOfFiles*ri!averageFileSize)},
  refreshAfter: "UNFOCUS",
  required:true,
  validations: {}
)

Can someone help with this?  Thanks...

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer
    If the value of "total estimated capacity" is supposed to be auto-calculated, then the saveInto of its field is not the place to do this.

    Assuming that you require the multiplied value to be saved into the variable ri!totalEstCapacity as soon as both of the precedent fields are filled, with strictly no further input from the user, then you would do the saveInto in both of the precedent fields (do it the same way in both of them, and in both cases make them check to ensure both values are non-null before doing the operation). The "total estimated capacity" field should probably then be read-only as you won't be allowing its value to be changed directly.

    A more flexible way (though it gets more complicated) is to do the math operation within a with() variable which will auto-update as soon as the two precedent fields' values change. But getting the multiplied value saved into your ri! is a step more complex.
  • This will do the trick:

    load(
      with(
        a!formLayout(
          contents: {
            a!sectionLayout(
              contents: {
                a!columnsLayout(
                  columns: {
                    a!columnLayout(
                      contents: {
                        a!integerField(
                          label: "int1",
                          labelPosition: "ABOVE",
                          value: ri!int1,
                          saveInto: {
                            ri!int1,
                            a!save(
                              ri!integerOUT,
                              if(
                                and(
                                  not(isNull(ri!int1)),
                                  not(isNull(ri!int2))
                                ),
                                ri!int1 * ri!int2,
                                {}
                              )
                            )
                          },
                          refreshAfter: "KEYPRESS"
                        )
                      }
                    ),
                    a!columnLayout(
                      contents: {
                        a!integerField(
                          label: "int2",
                          labelPosition: "ABOVE",
                          value: ri!int2,
                          saveInto: {
                            ri!int2,
                            a!save(
                              ri!integerOUT,
                              if(
                                and(
                                  not(isNull(ri!int1)),
                                  not(isNull(ri!int2))
                                ),
                                ri!int1 * ri!int2,
                                {}
                              )
                            )
                          },
                          refreshAfter: "KEYPRESS"
                        )
                      }
                    ),
                    a!columnLayout(
                      contents: {
                        a!integerField(
                          label: "product",
                          labelPosition: "ABOVE",
                          value: ri!integerOUT,
                          saveInto: {
                            ri!integerOUT,
                            a!save(
                              ri!integerOUT,
                              save!value
                            )
                          },
                          refreshAfter: "UNFOCUS",
                          readonly: true()
                        )
                      }
                    )
                  }
                )
              }
            )
          }
        )
      )
    )

  • You could just place this ri!numberOfFiles*ri!averageFileSize in value.
  • While this suggestion will calculate the value on the form, it doesn't pass it back out to the rule input as is required by .
  • Of course we will not make any changes into  code except for the value to display. 
    I think williams code is already doing the job of saving into the rule inputs, so I suggested to display the value as well.
    Here is the code I suggested:
    a!integerField(
    label: "Total estimated capacity",
    value: ri!numberOfFiles*ri!averageFileSize,
    saveInto: {
    a!save(ri!totalEstCapacity,ri!numberOfFiles*ri!averageFileSize)},
    refreshAfter: "UNFOCUS",
    required:true,
    validations: {}
    )

    or we could also do this:

    a!integerField(
    label: "Total estimated capacity",
    value: ri!numberOfFiles*ri!averageFileSize,
    saveInto: ri!totalEstCapacity,
    refreshAfter: "UNFOCUS",
    required:true,
    validations: {}
    )

    Thanks
    Yeswanth.

  • 0
    Certified Lead Developer
    in reply to yeswanththiyarir
    These won't work for what requires, though - the integer field's saveInto won't execute unless the user interacts with that field, so the calculated value (despite being displayed correctly) wouldn't be saved into the RI variable unless the user went in and attempted to make a change in the result field, which is not intuitive. 's approach should work.
  • Hi Yeswanththiyarir,
    As Informed by Mike The calculated value will be displayed in the Total Estimated Capacity Field, but it will not save in the corresponding rule Input until the user interacts with the Total Estimated capacity Component.

    The other way is like you can always save the calculated value on any of the button's saveInto. But this is not a recommended way since the form may contain multiple buttons and you need to have this piece of code in Every button's saveInto.

    So the better way is as per Robert Shankin's Approach.