How do I save multiple selections in a checkboxField? I tried displaying the cho

Certified Senior Developer
How do I save multiple selections in a checkboxField? I tried displaying the choiceValues using a rule (e.g., rule!psh_getAllFruits) that pulled data from a cdt (PSH_Fruits). The CDT is associated with a table (pshfruits) that has 2 columns: id (auto-generated integer) and fruitType (text). I wanted to store the selected id(s) in a field in another CDT (specifically newList_cdt.fruitType). I originally had 'fruit' as an integer. When that didn't work, I decided to change my approach to displaying the choiceValues using a constant (cons!PSH_FRUITS) and using a checkboxFieldByIndex; couldn't get that to work, so I went with the following and changed the data type of the column in the table (and CDT for this item) to 'text'. It works as long as I only select one item. (I actually have 8 choices so I'd really prefer to pull the choiceValues from a CDT - lookup table). I cannot get it to save or display multiple selections. At one point...

OriginalPostID-192632

OriginalPostID-192632

  Discussion posts and replies are publicly visible

  • 0
    Certified Senior Developer
    ... I did check 'Array' for 'fruitType' in the newList_cdt and when I saved it - the Data Store couldn't be updated because it said a table could not be found. Do I need to create a new table to store the choiceValues?

    What am I doing wrong? Here's my code for the checkboxField.

    a!checkboxField(
    label: "What type of fruit?",
    labelPosition: "ABOVE",
    instructions: "Choose all that apply.",
    choicelabels: {"Bananas", "Apples", "Pears", "Grapes" },
    choiceValues: {"B", "A", "P", "G"},
    value: if(rule!APN_isEmpty(ri!newList_cdt.fruitType),null, ri!newList_cdt.fruitType),
    saveInto: ri!newList_cdt.fruitType,
    refreshAfter: "UNFOCUS",
    readOnly: false,
    validation: {}
    ),
  • @judy, in this case generally what we would do is, save the array directly in the database as VARCHAR (uniformed string) and while display use the split function to display them (i mean convert to array).

    a!checkboxField(
    label: "What type of fruit?",
    labelPosition: "ABOVE",
    choicelabels: {"Bananas", "Apples", "Pears", "Grapes" },
    choiceValues: {"B", "A", "P", "G"},
    value: if(rule!APN_isEmpty(ri!fruitType),null, split(ri!fruitType,"; ")),
    saveInto: ri!fruitType,
    instructions:touniformstring(ri!fruitType),
    refreshAfter: "UNFOCUS",
    readOnly: false,
    validation: {}
    ),
  • Please make sure that the rule input newList_cdt is defined as multiple in the interface (and adjust naming, perhaps newList_cdts or similar). I don't think that you would want to define this variable as multiple in the CDT. If I understand correctly, I would look to make each value selected a different row in the 'selected' table - that means you would have one 'object' saved to database for each fruit 'checked'.
  • in this case, if you would want it to be in multiple rows then you are going to define a new table and then created a nested CDT.. then we can achieve this. or create a temporary variable and then do the processing of the split and assigning inside the process model and have it as single while passing to the interface.
  • 0
    Certified Senior Developer
    I would rather not have to create a new table with a nested CDT if I can avoid it. I replicated the code above with the 'split' and got the following error when I selected 2 items:

    Could not display interface. Please check definition and inputs.
    Interface Definition: Expression evaluation error at function a!checkboxField [line 191]: A checkbox component [label="What type of fruit? - Need to fix..."] has an invalid value for "value". All selected values must be present in the choiceValues array, but value was B; P and choiceValues was B; A; P; G.
  • 1. My approach doesn't necessarily entail a new table, or a nested CDT. It could be the same table as is.
    2. If using the 'split' method, make sure to consider the effect of "name changes" - what happens if "Bananas" go by "BN" instead of "B" in the future? What are the application effects? If necessary to account for this, a lookup table could be used & this is compatible with other approach.
  • @Jack, if there are any changes to the existing values of the reference values, i mean the choice values, then the code will break in order to resolve, you can pass them to a rule, which will maintain the list of inactive codes and their respective active code. You can do it through a displayValue() function.
  • 0
    Certified Senior Developer
    In looking at your previous response @jackm829, the value captured in this checkbox field would be part of 1 record in a table. Each value selected would not be a different row in the selected table. It sounds like I'm probably going to have to go with a nested CDT. I've never used one...
  • @nara, the larger issue is the data integrity in database. Consider the effect of the introduction of reporting requirement. Maybe user can specify filters for fruit selection. User says, "give me all selections of banana and apples" - but those have been renamed and some entries in database are legacy. The query becomes a lot tougher.
  • In the end, it depends on the use case, just wanted to bring the aforementioned up as a consideration to take into account.