I want get the product ID's of all selected items from my combobox in Power Apps, but the DisplayFields property of the combobox only shows the product name. How do I get the corresponding ID's?
Mulitselect ComboBox in Power Apps |
Solution
You can loop though the collection of selected items of your combobox with the SelectedItems function. Below you will find a couple of code examples. For this example we are using a table called MyProducts with data from Adventure Works.
Table with sample data |
Loop through selection
If multi select is turned off (basicly a DropDown with a search option) then you can use the Selected function, just like for the ComboBox.
ComboBox_singleselect.Selected.ProductKey
When multiselect is turned on, you must use SelectedItems. Getting a comma separated list of ProductKeys (instead of ProductNames) can be done with the following script. To test it you could add a label with this script in the Text property to show the result:
Concat( ComboBox_multiselect.SelectedItems.ProductKey, Concatenate( Text(ProductKey), "," ) )
This will result in a string with "PrdKey1,PrdKey3,PrdKey3," If you want to get ride of the last comma you can use a left: LEFT(mystring, LEN(mystring) - 1):
Left( Concat( ComboBox_multiselect.SelectedItems.ProductKey, Concatenate( Text(ProductKey), "," ) ), Len( Concat( ComboBox_multiselect.SelectedItems.ProductKey, Concatenate( Text(ProductKey), "," ) ) ) - 1 )
Or if you want to keep your code a little cleaner then you could first store the result in a variable and then use the LEFT on the variable. To test this add the UpdateContext to the OnChange of the ComboBox and the Left to the Text property of your label.
// Store concat with trailing comma in variable UpdateContext( { SelProds: Concat( ComboBox_multiselect.SelectedItems.ProductKey, Concatenate( Text(ProductKey), "," ) ) } ); // Remove last comma Left(SelProds, Len(SelProds) - 1)
Update: better solution to not repeat the code is by using the WITH function
You can also get specific items from the selection.
// First item First(ComboBox_multiselect.SelectedItems.ProductKey).ProductKey // Second item First( LastN( ComboBox_multiselect.SelectedItems.ProductKey, CountRows(ComboBox_multiselect.SelectedItems.ProductKey) - 1 ) ).ProductKey // Thirth item First( LastN( ComboBox_multiselect.SelectedItems.ProductKey, CountRows(ComboBox_multiselect.SelectedItems.ProductKey) - 2 ) ).ProductKey
To keep your code a little cleaner you could first store the selection in a collection in the OnChange event of the ComboBox.
// Store selection in collection ClearCollect( ProdSel, ComboBox_multiselect.SelectedItems.ProductKey ); // First item First(ProdSel).ProductKey // Second item First( LastN( ProdSel, CountRows(ProdSel) - 1 ) ).ProductKey // Thirth item First( LastN( ProdSel, CountRows(ProdSel) - 2 ) ).ProductKey
Maximize selection
An other need trick is that you could maximize the number of selected items by adding the following script to the OnChange of the ComboBox.
If( CountRows(ComboBox_multiselect.SelectedItems) > 4, Notify( "You reached the maximum of 4 items", NotificationType.Error, 2000 ) )
Or you could combine that with filling a boolean variable that you can use to disable/hide the save button
If( CountRows(ComboBox_multiselect.SelectedItems) > 4, UpdateContext({ValidationError: false}); Notify( "You reached the maximum of 4 items", NotificationType.Error, 2000 ), UpdateContext({ValidationError: true}) );
Conclusion
In this code snippet post you learned how to retrieve the selected items from a Power Apps ComboBox. When multi select is turned on you should use SelectedItems instead if Selected. This returns a collection of all selected Items from the ComboBox.
Bug: the formulas only work with string columns. Solution is to add the integerfield to the displayfields of the ComboBox.
Add non-string fields to DisplayFields |