cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
dyee4614
Helper V
Helper V

ForAll Loop Issue (Counting each iteration)

Background:

- Submit data is the collection where I store my data.
- Success Count is where I want to count each record that had an empty error table.   However, my code currently records ALL the items in SubmitData for each loop instead of just the count that went through without an error.  How can I filter it to only the item that is being evaluated in the ForAll loop? 

 

 

 

ForAll(
      SubmitData,
           If(
               IsEmpty(
                   Errors('H2A Housing Inspection',
                       (Patch('H2A Housing Inspection'
                           ,Defaults('H2A Housing Inspection'),
                                {Title: "",
                                 DDate: Now()}
                                           )
                                        )
                                     )
                                  ), 
                            Collect(SuccessCount,SubmitData.Counter)
                               )
                        );
Notify(
       CountA(SuccessCount.Counter) & " out of " & 
       CountA(SubmitData.region5),NotificationType.Success)

 

 

2 ACCEPTED SOLUTIONS

Accepted Solutions
mdevaney
Super User
Super User

@dyee4614 

My approach to the solution is different from yours since it increments a Value stored in a 1 row collection each time there is a success and it does not rely on filters.  Please study the changes I made carefully and let me know if you have any questions.

 

// reset success counter to 0
ClearCollect(colSuccessCount,{Value: 0})

ForAll(
    SubmitData,
    If(
        IsEmpty(
            Errors('H2A Housing Inspection',
                (
                    Patch(
                        'H2A Housing Inspection',
                        Defaults('H2A Housing Inspection'),
                        {Title: "", DDate: Now()}
                    )

                )
            )
        ),

        // increment success counter if there are no error
        Patch(colSuccessCount,{Value: First(colSuccessCount).Value + 1})
    )
);

Notify(
        // display the counter result
        First(colSuccessCount).Value & " out of " & 
        CountA(SubmitData.region5),NotificationType.Success
)

 

---
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."

View solution in original post

v-siky-msft
Community Support
Community Support

Hi @dyee4614 ,

 

Please modify your codes as below:

 

ForAll(
      SubmitData,
           If(
               IsEmpty(
                   Errors('H2A Housing Inspection',
                       (Patch('H2A Housing Inspection'
                           ,Defaults('H2A Housing Inspection'),
                                {Title: "",
                                 DDate: Now()}
                                           )
                                        )
                                     )
                                  ), 
                            Collect(SuccessCount,Counter)
                               )
                        );
Notify(
       CountA(SuccessCount.Value) & " out of " & 
       CountA(SubmitData.region5),NotificationType.Success)

 

I modify 'SubmitData.Counter' to Counter, because 'SubmitData.Counter' returns a single-line table, so every time it loops, the whole single-line table is stored into the SuccessCount collection.

If you change to use Counter, each time it loops, it will only return the counter value of the currently traversed record without error, and collect to SuccessCount collection.

Meanwhile, you need to modify 'SuccessCount.Counter' to 'SuccessCount.Value', because the column name is changed.

Sik

View solution in original post

3 REPLIES 3
mdevaney
Super User
Super User

@dyee4614 

My approach to the solution is different from yours since it increments a Value stored in a 1 row collection each time there is a success and it does not rely on filters.  Please study the changes I made carefully and let me know if you have any questions.

 

// reset success counter to 0
ClearCollect(colSuccessCount,{Value: 0})

ForAll(
    SubmitData,
    If(
        IsEmpty(
            Errors('H2A Housing Inspection',
                (
                    Patch(
                        'H2A Housing Inspection',
                        Defaults('H2A Housing Inspection'),
                        {Title: "", DDate: Now()}
                    )

                )
            )
        ),

        // increment success counter if there are no error
        Patch(colSuccessCount,{Value: First(colSuccessCount).Value + 1})
    )
);

Notify(
        // display the counter result
        First(colSuccessCount).Value & " out of " & 
        CountA(SubmitData.region5),NotificationType.Success
)

 

---
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."

v-siky-msft
Community Support
Community Support

Hi @dyee4614 ,

 

Please modify your codes as below:

 

ForAll(
      SubmitData,
           If(
               IsEmpty(
                   Errors('H2A Housing Inspection',
                       (Patch('H2A Housing Inspection'
                           ,Defaults('H2A Housing Inspection'),
                                {Title: "",
                                 DDate: Now()}
                                           )
                                        )
                                     )
                                  ), 
                            Collect(SuccessCount,Counter)
                               )
                        );
Notify(
       CountA(SuccessCount.Value) & " out of " & 
       CountA(SubmitData.region5),NotificationType.Success)

 

I modify 'SubmitData.Counter' to Counter, because 'SubmitData.Counter' returns a single-line table, so every time it loops, the whole single-line table is stored into the SuccessCount collection.

If you change to use Counter, each time it loops, it will only return the counter value of the currently traversed record without error, and collect to SuccessCount collection.

Meanwhile, you need to modify 'SuccessCount.Counter' to 'SuccessCount.Value', because the column name is changed.

Sik

dyee4614
Helper V
Helper V

It worked!  Thanks for your help.

Helpful resources

Announcements
Power Apps News & Annoucements carousel

Power Apps News & Announcements

Keep up to date with current events and community announcements in the Power Apps community.

Community Call Conversations

Introducing the Community Calls Conversations

A great place where you can stay up to date with community calls and interact with the speakers.

Power Apps Community Blog Carousel

Power Apps Community Blog

Check out the latest Community Blog from the community!

Top Solution Authors
Top Kudoed Authors
Users online (4,819)