To save multiple values as a same target in a!save()

Hi Folks,

I am building the below logic i.e comparing the two local variables values if that is true then it has to perform the logic (i.e) 1*50 or else should return the null values. And it has to save the value when I click the button continue. As we cannot save multiple values as a same target in a!save(), Now I am facing issue in saving the values. If I define any one logic in a!save()  that is working, whereas for multiple logic it is not working.

Below I have attached my code, you can refer from line 101 to 147. Please suggest If there is any alternative approach for the below logic.

a!localVariables(
 local!type, 
 local!quanity,
 local!button,
 {
a!cardLayout(
  style:"SUCCESS",
  contents:{
    a!columnsLayout(
      columns:{
        a!columnLayout(
          width:"MEDIUM",
          contents: 
          {
            a!textField(
              label:"MENU",
              readOnly:true(),
            ),
            /*a!textField(readOnly:true() ),*/
            a!dropdownField(
              label:"Categories",
              placeholder:" --- SELECT THE CATEGORY ---",
              choiceLabels:cons!CLS_PIZZA_TYPE,
              choiceValues:cons!CLS_PIZZA_TYPE,
              value:local!type,
              saveInto:local!type
            ),
            a!imageField(
              label:"Margherita",
              labelPosition:"ADJACENT",
              align:"CENTER",
              showWhen:local!type="Margherita",
              size:"LARGE",
              style:"AVATAR",
              isThumbnail:true,
              images:a!documentImage(
                document:cons!CLS_IMAGES[2],
              )
            ),
            a!imageField(
              label:"BBQ",
              labelPosition:"ADJACENT",
              align:"CENTER",
              showWhen:local!type="BBQ",
              size:"LARGE",
              style:"AVATAR",
              isThumbnail:true,
              images:a!documentImage(
                document:cons!CLS_IMAGES[3],
              )
            ),
            a!imageField(
              label:"Cheese",
              labelPosition:"ADJACENT",
              align:"CENTER",
              showWhen:local!type="Cheese",
              size:"LARGE",
              style:"AVATAR",
              isThumbnail:true,
              images:a!documentImage(
                document:cons!CLS_IMAGES[4],
              )
            ),
            a!imageField(
              label:"Sicilian",
              labelPosition:"ADJACENT",
              align:"CENTER",
              showWhen:local!type="Sicilian",
              size:"LARGE",
              style:"AVATAR",
              isThumbnail:true,
              images:a!documentImage(
                document:cons!CLS_IMAGES[6],
              )
            )
          }
        ),
        a!columnLayout(
          width:"MEDIUM",
          contents: {
            a!textField(readOnly:true() ),
            a!textField(readOnly:true() ),
            a!dropdownField(
              label:"Quantity",
              placeholder:"---SELECT THE QUANTITY ---",
              choiceLabels:{1,2,3},
              choiceValues: {1,2,3},
              value:local!quanity,
              saveInto:local!quanity
            )
          }
        ),
        a!columnLayout(
          contents:{
            a!textField(
             readOnly:true(),
            ),
            a!textField(
              readOnly:true(),
            ),
            a!textField(
              label:"Total Amount",
              value:{
             if(and(local!type="Margherita",local!quanity=1),1*50,{}),
              if(and(local!type="Margherita",local!quanity=2),2*50,{}),
              if(and(local!type="Margherita",local!quanity=3),3*50,{}),
              if(and(local!type="BBQ",local!quanity=1),1*100,{}),
              if(and(local!type="BBQ",local!quanity=2),2*100,{}),
              if(and(local!type="BBQ",local!quanity=3),3*100,{}),
              if(and(local!type="Cheese",local!quanity=1),1*150,{}),
              if(and(local!type="Cheese",local!quanity=2),2*150,{}),
              if(and(local!type="Cheese",local!quanity=3),3*150,{}),
              if(and(local!type="Sicilian",local!quanity=1),1*200,{}),
              if(and(local!type="Sicilian",local!quanity=2),2*200,{}),
              if(and(local!type="Sicilian",local!quanity=3),3*200,{})
              },
           )
          }
        )
      }
    )
  }
),
a!sectionLayout(),/* line break */
a!buttonLayout(
  primaryButtons: a!buttonWidget(
    label:"CONTINUE",
    style:"PRIMARY",
    submit:true(),
    confirmMessage:"Do you want to continue",
    value:"Continue",
    saveInto:
    {
    local!button,
    a!save(ri!amount,if(and(local!type="Margherita",local!quanity=1),1*50,{})),
    a!save(ri!amount,if(and(local!type="Margherita",local!quanity=2),2*50,{})),
    a!save(ri!amount,if(and(local!type="Margherita",local!quanity=3),3*50,{})),
    a!save(ri!amount,if(and(local!type="BBQ",local!quanity=1),1*100,{})),
    a!save(ri!amount,if(and(local!type="BBQ",local!quanity=2),2*100,{})),
    a!save(ri!amount,if(and(local!type="BBQ",local!quanity=3),3*100,{})),
    a!save(ri!amount,if(and(local!type="Cheese",local!quanity=1),1*150,{})),
    a!save(ri!amount,if(and(local!type="Cheese",local!quanity=2),2*150,{})),
    a!save(ri!amount,if(and(local!type="Cheese",local!quanity=3),3*150,{})),
    a!save(ri!amount,if(and(local!type="Sicilian",local!quanity=1),1*200,{})),
    a!save(ri!amount,if(and(local!type="Sicilian",local!quanity=2),2*200,{})),
    a!save(ri!amount,if(and(local!type="Sicilian",local!quanity=3),3*200,{})),
    }
  )
)
}
)

Thanks in Advance!!

Regards,
Charulatha

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer

    FYI, there's no need to post duplicates in different forum categories, as everything shows up on the same main page.

    For your question: you'd probably have an easier time if you utilize built-in local variable functionality to keep track of your resulting value instead of doing such complex saveInto operations.

    I've tweaked your above code such that it can be copied and pasted into anyone's blank interface editor, and shifted the burden of calculating the total value to a local variable (such that it only has to do the calculation once, among other things):

    a!localVariables(
      local!typeChoices: {"Margherita", "BBQ", "Cheese", "Sicilian"},
      local!type, 
      local!quanity: tointeger(null()),
      local!button,
      
      local!calculatedValue: index({
        if(and(local!type="Margherita",local!quanity=1),1*50,{}),
        if(and(local!type="Margherita",local!quanity=2),2*50,{}),
        if(and(local!type="Margherita",local!quanity=3),3*50,{}),
        if(and(local!type="BBQ",local!quanity=1),1*100,{}),
        if(and(local!type="BBQ",local!quanity=2),2*100,{}),
        if(and(local!type="BBQ",local!quanity=3),3*100,{}),
        if(and(local!type="Cheese",local!quanity=1),1*150,{}),
        if(and(local!type="Cheese",local!quanity=2),2*150,{}),
        if(and(local!type="Cheese",local!quanity=3),3*150,{}),
        if(and(local!type="Sicilian",local!quanity=1),1*200,{}),
        if(and(local!type="Sicilian",local!quanity=2),2*200,{}),
        if(and(local!type="Sicilian",local!quanity=3),3*200,{})
      }, 1, null()),
      
      {
        a!cardLayout(
          style:"SUCCESS",
          contents:{
            a!columnsLayout(
              columns:{
                a!columnLayout(
                  width:"MEDIUM",
                  contents: 
                  {
                    a!textField(
                      label:"MENU",
                      readOnly:true(),
                    ),
                    /*a!textField(readOnly:true() ),*/
                    a!dropdownField(
                      label:"Categories",
                      placeholder:" --- SELECT THE CATEGORY ---",
                      choiceLabels:local!typeChoices,
                      choiceValues:local!typeChoices,
                      value:local!type,
                      saveInto:local!type
                    ),
                    /*a!imageField(*/
                      /*label:"Margherita",*/
                      /*labelPosition:"ADJACENT",*/
                      /*align:"CENTER",*/
                      /*showWhen:local!type="Margherita",*/
                      /*size:"LARGE",*/
                      /*style:"AVATAR",*/
                      /*isThumbnail:true,*/
                      /*images:a!documentImage(*/
                        /*document:cons!CLS_IMAGES[2],*/
                      /*)*/
                    /*),*/
                    /*a!imageField(*/
                      /*label:"BBQ",*/
                      /*labelPosition:"ADJACENT",*/
                      /*align:"CENTER",*/
                      /*showWhen:local!type="BBQ",*/
                      /*size:"LARGE",*/
                      /*style:"AVATAR",*/
                      /*isThumbnail:true,*/
                      /*images:a!documentImage(*/
                        /*document:cons!CLS_IMAGES[3],*/
                      /*)*/
                    /*),*/
                    /*a!imageField(*/
                      /*label:"Cheese",*/
                      /*labelPosition:"ADJACENT",*/
                      /*align:"CENTER",*/
                      /*showWhen:local!type="Cheese",*/
                      /*size:"LARGE",*/
                      /*style:"AVATAR",*/
                      /*isThumbnail:true,*/
                      /*images:a!documentImage(*/
                        /*document:cons!CLS_IMAGES[4],*/
                      /*)*/
                    /*),*/
                    /*a!imageField(*/
                      /*label:"Sicilian",*/
                      /*labelPosition:"ADJACENT",*/
                      /*align:"CENTER",*/
                      /*showWhen:local!type="Sicilian",*/
                      /*size:"LARGE",*/
                      /*style:"AVATAR",*/
                      /*isThumbnail:true,*/
                      /*images:a!documentImage(*/
                        /*document:cons!CLS_IMAGES[6],*/
                      /*)*/
                    /*)*/
                  }
                ),
                a!columnLayout(
                  width:"MEDIUM",
                  contents: {
                    a!textField(readOnly:true() ),
                    a!textField(readOnly:true() ),
                    a!dropdownField(
                      label:"Quantity",
                      placeholder:"---SELECT THE QUANTITY ---",
                      choiceLabels:{1,2,3},
                      choiceValues: {1,2,3},
                      value:local!quanity,
                      saveInto:local!quanity
                    )
                  }
                ),
                a!columnLayout(
                  contents:{
                    a!textField(
                      readOnly:true(),
                    ),
                    a!textField(
                      readOnly:true(),
                    ),
                    a!textField(
                      label: "Total Amount",
                      readOnly: true(),
                      value: local!calculatedValue
                      /* moved to local variable */
                      /*{*/
                        /*if(and(local!type="Margherita",local!quanity=1),1*50,{}),*/
                        /*if(and(local!type="Margherita",local!quanity=2),2*50,{}),*/
                        /*if(and(local!type="Margherita",local!quanity=3),3*50,{}),*/
                        /*if(and(local!type="BBQ",local!quanity=1),1*100,{}),*/
                        /*if(and(local!type="BBQ",local!quanity=2),2*100,{}),*/
                        /*if(and(local!type="BBQ",local!quanity=3),3*100,{}),*/
                        /*if(and(local!type="Cheese",local!quanity=1),1*150,{}),*/
                        /*if(and(local!type="Cheese",local!quanity=2),2*150,{}),*/
                        /*if(and(local!type="Cheese",local!quanity=3),3*150,{}),*/
                        /*if(and(local!type="Sicilian",local!quanity=1),1*200,{}),*/
                        /*if(and(local!type="Sicilian",local!quanity=2),2*200,{}),*/
                        /*if(and(local!type="Sicilian",local!quanity=3),3*200,{})*/
                      /*}*/
                    )
                  }
                )
              }
            )
          }
        ),
        a!sectionLayout(),/* line break */
        a!buttonLayout(
          primaryButtons: a!buttonWidget(
            label:"CONTINUE",
            style:"PRIMARY",
            submit:true(),
            confirmMessage:"Do you want to continue",
            value: "Continue",
            saveInto: {
              local!button,
              a!save(
                ri!amount,
                local!calculatedValue
              )
              /*a!save(ri!amount,if(and(local!type="Margherita",local!quanity=1),1*50,{})),*/
              /*a!save(ri!amount,if(and(local!type="Margherita",local!quanity=2),2*50,{})),*/
              /*a!save(ri!amount,if(and(local!type="Margherita",local!quanity=3),3*50,{})),*/
              /*a!save(ri!amount,if(and(local!type="BBQ",local!quanity=1),1*100,{})),*/
              /*a!save(ri!amount,if(and(local!type="BBQ",local!quanity=2),2*100,{})),*/
              /*a!save(ri!amount,if(and(local!type="BBQ",local!quanity=3),3*100,{})),*/
              /*a!save(ri!amount,if(and(local!type="Cheese",local!quanity=1),1*150,{})),*/
              /*a!save(ri!amount,if(and(local!type="Cheese",local!quanity=2),2*150,{})),*/
              /*a!save(ri!amount,if(and(local!type="Cheese",local!quanity=3),3*150,{})),*/
              /*a!save(ri!amount,if(and(local!type="Sicilian",local!quanity=1),1*200,{})),*/
              /*a!save(ri!amount,if(and(local!type="Sicilian",local!quanity=2),2*200,{})),*/
              /*a!save(ri!amount,if(and(local!type="Sicilian",local!quanity=3),3*200,{})),*/
            }
          )
        )
      }
    )

    Further, it seems like you could eliminate some of the hardcoding here if you actually use your local!quanity value in your calculation, instead of using it in hardcoded multiplications.  Suggestion:

      local!basePrice: if(
        local!type = "Margherita",
        50,
    
        local!type = "BBQ",
        100,
    
        local!type = "Cheese",
        150,
    
        local!type = "Sicilian",
        200,
    
        /* else */
        0
      ),
      local!calculatedValue: local!basePrice * local!quanity,
      
      /* replaces multiple executions of hardcoded logic -- */
      /*index({*/
        /*if(and(local!type="Margherita",local!quanity=1),1*50,{}),*/
        /*if(and(local!type="Margherita",local!quanity=2),2*50,{}),*/
        /*if(and(local!type="Margherita",local!quanity=3),3*50,{}),*/
        /*if(and(local!type="BBQ",local!quanity=1),1*100,{}),*/
        /*if(and(local!type="BBQ",local!quanity=2),2*100,{}),*/
        /*if(and(local!type="BBQ",local!quanity=3),3*100,{}),*/
        /*if(and(local!type="Cheese",local!quanity=1),1*150,{}),*/
        /*if(and(local!type="Cheese",local!quanity=2),2*150,{}),*/
        /*if(and(local!type="Cheese",local!quanity=3),3*150,{}),*/
        /*if(and(local!type="Sicilian",local!quanity=1),1*200,{}),*/
        /*if(and(local!type="Sicilian",local!quanity=2),2*200,{}),*/
        /*if(and(local!type="Sicilian",local!quanity=3),3*200,{})*/
      /*}, 1, null()),*/