cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Continued Contributor
Continued Contributor

Add ID to collection

 

Looking for help with creation of a collection utilized only within the app, no external datasource.  I want to include an ID column in the collection with a count of each item.  The trick is that the data being collected is from a ComboBox so all standard ways of adding the id are not working, i.e. Collect(colThisItem, {id: CountRows(colThisItem) + 1, Result: ComboBox.Selected}) (@ericonline - thanks for the formula).  

 

The ComboBox1 items are populated from another collection:

  • Items - colComboBoxChoices
  • OnChange - Collection(colSelectedItems, ComboBox.Selected)

This works fine for collecting.  However, if I add the CountRows within the OnChange formula , it does not work as intended.  Again, I believe it has to do with the results for the colSelectedItems coming from a ComboBox - just not sure how to resolve.

Help would be appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Microsoft
Microsoft

Re: Add ID to collection

Thank you for clarification. Pease try setting this to OnChange:

Clear(colThisItem);ForAll(ComboBox1.SelectedItems, Collect(colThisItem, {ID: CountRows(colThisItem)+1, Result:Last(FirstN(ComboBox1.SelectedItems, CountRows(colThisItem)+1)).Value}))

If you're experimenting with a new ComboBox control with a default data set then change the last "Value" to "Value1".

Let me know if you have any questions in regards to the proposed formula.

View solution in original post

8 REPLIES 8
Highlighted
Microsoft
Microsoft

Re: Add ID to collection

Hello tianaranjo,

 

Please note that "OnChange" on the ComboBox is actually fired every time the selection is changed internally or by user interaction. As a result, it fires far more often that expected. Please consider one of the following workarounds:

1. Replace ComboBox control with a DropDown, which fires the "OnChange" only on user interaction (on closing the flyout). DropDown does not support multiple selections but since you're using ComboBox.Selected (and not ComboBox.SelectedItems), I assume you do not need that.

2. Store the previously selected value and perform the increment (+1) only in case the currently selected value is different from the previously selected value (in which case the previous value is also updated). This way, the "OnChange" event will fire only once.

Hope this helps.

Highlighted
Continued Contributor
Continued Contributor

Re: Add ID to collection

@dinusc  Thanks for your reply.  Thing is, I do need the user to be able to select multiple items if they choose; that need eliminated ability to utilize drop down.  

 

Any other thoughts that would allow user to select one or more?

 

Thanks again!

Highlighted
Microsoft
Microsoft

Re: Add ID to collection

Just so I understand the intended behavior, let's say I have a ComboBox with 10 items. If I want to select 3 of them (1, 4 and 6 for example), do you want to have a collection with 3 rows as following?

{id:1, Result:["Item1"]},{id:2, Result:["Item1,"Item4""]},{id:3, Result:["Item1","Item4","Item6]}

 

If you need just one row (as the last one) in your collection then you will need to have a button that the user clicks when he/she is done selecting items.

 

In your formula, you use ComboBox.Selected which should not be used in multi-select mode.

Highlighted
Continued Contributor
Continued Contributor

Re: Add ID to collection

So, if you have 10 items and select three used in example below, I would like them to appear in collection as:

ID  Result

1    Item1

2    Item4

3    Item6

 

A user might pick one of the selected or all 10. Did this help?

 

If so, how do I:

  1. Create a collection so the results are as shown above
  2. What do I do in place of ComboBox.Selected

Thanks!  I've been banging my head against my desk for days on this one.

Highlighted
Microsoft
Microsoft

Re: Add ID to collection

Thank you for clarification. Pease try setting this to OnChange:

Clear(colThisItem);ForAll(ComboBox1.SelectedItems, Collect(colThisItem, {ID: CountRows(colThisItem)+1, Result:Last(FirstN(ComboBox1.SelectedItems, CountRows(colThisItem)+1)).Value}))

If you're experimenting with a new ComboBox control with a default data set then change the last "Value" to "Value1".

Let me know if you have any questions in regards to the proposed formula.

View solution in original post

Highlighted
Advocate I
Advocate I

Re: Add ID to collection

Thank you so much for this @dinusc !

It worked for me but I needed to experiment with it a bit.

But could you please explain in detail the formula

ForAll(ComboBox1.SelectedItems, 
Collect(colThisItem,
{ID: CountRows(colThisItem)+1,
Result:Last(
FirstN(ComboBox1.SelectedItems, CountRows(colThisItem)+1)).Value}))

 Especially the Result:Last(... part?

Thanks

Highlighted
Microsoft
Microsoft

Re: Add ID to collection

Hello @peter_not . As you probably noticed, the "Result" is just a field name. Note the "Collect" function which adds one item from SelectedItems to colThisItem pear each ForAll loop iteration. The Last(FirstN(sourceCol, number)) is just a workaround to extract the Nth item. The same can be done by First(LastN(sourceCol, number). For example, on the fourth iteration (N = 4), the thisColItem contains 3 items (since the first 3 iterations completed) so the "FirstN(ComboBox1.SelectedItems, CountRows(colThisItem)+1)" will reference the fourth item from "ComboBox1.SelectedItems" in order to add it to the "colThisItem". I hope this helps. 

Highlighted
Regular Visitor

Re: Add ID to collection

Thank you for sharing this solution! It really helped me in building my app!

Helpful resources

Announcements
Community Conference

Power Platform Community Conference

Find your favorite faces from the community presenting at the Power Platform Community Conference!

Watch Now

Experience what’s next for Power Apps

See the latest Power Apps innovations, updates, and demos from the Microsoft Business Applications Launch Event.

Power Platform ISV Studio

Power Platform ISV Studio

ISV Studio is designed to become the go-to Power Platform destination for ISV’s to monitor & manage published applications.

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Top Solution Authors
Top Kudoed Authors
Users online (5,949)