web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Calculations in Behavi...
Power Apps
Unanswered

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

(0) ShareShare
ReportReport
Posted on by 47

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: 

 

ClearCollect(
 Loop,
 {Index: 1},
 {Index: 2},
 {Index: 3},
 {Index: 4},
 {Index: 5}
);

 

The following is the code used in Function1: 

 

Clear(Temp);
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
Collect(Temp2,
{
 //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! 

Categories:
I have the same question (0)
  • MVP-Phipps Profile Picture
    3,521 Super User 2024 Season 1 on at

    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.

    LinkedIn: https://www.linkedin.com/in/charlie-phipps-%F0%9F%91%A8%E2%80%8D%F0%9F%92%BB-91338715b/
    YouTube: https://www.youtube.com/channel/UChmFBGU1YKIU91sNMQ7buGg
    Twitter: https://twitter.com/phipps0218

  • mart11 Profile Picture
    47 on at

    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".

  • MVP-Phipps Profile Picture
    3,521 Super User 2024 Season 1 on at

    Ahh yes sorry I forgot that Temp2 was a collection

  • mart11 Profile Picture
    47 on at

    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).  

    mart11_1-1659081768672.png

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

    mart11_2-1659081926108.png

     

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

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 325 Most Valuable Professional

#2
11manish Profile Picture

11manish 165

#3
MS.Ragavendar Profile Picture

MS.Ragavendar 88 Super User 2026 Season 1

Last 30 days Overall leaderboard