Showing results for 
Search instead for 
Did you mean: 
Helper I
Helper I

Calculations in Behavior Custom Functions not behaving as expected on subsequent Button Clicks

Hi all,

I have built an app with the following logic: 

  1. Creates a collection A with data.
  2. Creates a collection Loop used to loop through collection items.
  3. Uses a ForAll() to loop for a specific number of times using Loop, and filters accordingly
  4. For each iteration in the loop, a behavior custom function Function1 is called which performs the following:
    1. Clears a temporary collection Temp
    2. Uses a ForAll() function to filter collection using the row id (using the Loop collection's data) and creates a JSON object to convert the row data to column, storing the column data in Temp. 
    3. A new collection is created using Collect() called Temp2 which is used to format the data in a particular order and to include specific data (rather than all the data extracted using the JSON() function. 
  5. For each iteration in the loop, another behavior custom function is called which creates a new collection from Temp2

The loop collection is created as follows: 


    {Index: 1},
    {Index: 2},
    {Index: 3},
    {Index: 4},
    {Index: 5}


The following is the code used in Function1: 


ForAll(Filter(A,id=rowId),//gets row from costmodel collection 
ForAll(Split(Substitute(Substitute(JSON(ThisRecord,JSONFormat.IgnoreBinaryData),"{",""),"}",""),","),//uses JSON object to convert it to a column
Collect(Temp,Split(Result,":").Result)));//stores the result in a collection
   //adding row data, using data passed as parameters and data from the column created from the row


The following is the code used in Function2: 


ClearCollect(B, Temp2);



This does work when I click the button, which is the component. However, if I click the button again, the values generated are not as expected (incorrect). If I wait 40 seconds before pressing the button, it works as expected. If I click it sooner, it does not work as expected.


Has anyone ever experienced this? Any suggestions on how I can fix it? 


Thanks in advance! 

Super User
Super User

Thinking out loud but can you add a Refresh(Temp2); before ClearCollect(B, Temp2);

Please Accept as Solution if it solves your question. Or just give it a Thumbs Up if it is helpful because this can help others.


That does not work because Temp2 is a collection.

When I tried that code I got an error "Only managed connections can be refreshed. The function 'Refresh' has some invalid arguments".

Ahh yes sorry I forgot that Temp2 was a collection

Helper I
Helper I

These are the values in collection Temp2 and collection B when the values are incorrect (just a snippet, there are more values but they did not fit the app screen).  


These are the values in collection Temp2 and collection B when the values are correct



Seems that the ForAll is looping sporadically maybe? Perhaps operations are not being performed sequentially? 

Helpful resources

Ignite 2022


Explore the latest innovations, learn from product experts and partners, level up your skillset, and create connections from around the world.

Power Apps Africa Challenge 2022

Power Apps Africa Challenge

Your chance to join an engaging competition of Power Platform enthusiasts.

Top Kudoed Authors
Users online (3,045)