Skip to main content

Notifications

Community site session details

Community site session details

Session Id : 0KjpAU/mxYePBv9o99F+yO
Power Apps - Building Power Apps
Unanswered

is there any way to clear a collection inside a ForAll()

Like (0) ShareShare
ReportReport
Posted on 15 Jun 2024 22:02:12 by 1,546 Super User 2024 Season 1

I want to list all the planner tasks assignments, so i am getting all the groups >> then all the groups's Planner plans >> then all the Plan's tasks >> then all the assignment of the task as follow:-

 

ClearCollect(finalResult,{PlanTitle:"",TaskTitle:"",Status:"",AssignedToUserId:"",DueDate:""});
Clear(colRelatedGroupsIds);
Clear(colRelatedPlanners);
Clear(colRelatedPlannerTasks);

ClearCollect(colTeamSitesItem,Filter('Team Sites',1=1));
ClearCollect(colAllGroups,Office365Groups.ListGroups().value);
ForAll(colAllGroups As group,
ForAll(colTeamSitesItem As i2,

If(Lower(group.displayName)=Lower(i2.'Team Site Name'),
Collect(colRelatedGroupsIds,group.id))));

ForAll(colRelatedGroupsIds As group,
Collect(colRelatedPlanners,
Planner.ListGroupPlans(group.Value).value);

ForAll(colRelatedPlanners As planner,
Collect(colRelatedPlannerTasks,
Planner.ListTasksV3(planner.id,group.Value).value);
ForAll(colRelatedPlannerTasks As task,
ForAll(task._assignments As taskAssignment,
Patch(finalResult,Defaults(finalResult),
{
PlanTitle:planner.title,
AssignedToUserId:taskAssignment.userId,
Status:task.percentComplete,
TaskTitle:task.title,
DueDate:task.dueDateTime
})


))));

 

 

but since i can not use Clear() inside a ForAll, i am getting all the tasks under all the plans.. how i can fix this?

 

  • Pstork1 Profile Picture
    66,093 Most Valuable Professional on 16 Jun 2024 at 01:57:40
    Re: is there any way to clear a collection inside a ForAll()

    As I said, there is no guarantee that those steps will happen in a specific order. If the data is all about the same then it will probably work. But those ForAll() formulas aren't guaranteed to run in succession.

     

    This is the description in the documentation

    "The ForAll function evaluates a formula for all the records in a table. The formula can calculate a value and/or perform actions, such as modifying data or working with a connection. Use the With function to evaluate the formula for a single record.

    Use the Sequence function with the ForAll function to iterate based on a count."

     

    Please note that you have to use the Sequence function to impose an order on the records. Otherwise the function is evaluated on all records in the table at the same time.

     

    It may work, but I wouldn't recommend depending on it.

     

     

  • johnjohnPter Profile Picture
    1,546 Super User 2024 Season 1 on 16 Jun 2024 at 00:33:40
    Re: is there any way to clear a collection inside a ForAll()

    @Pstork1 i am using

    emoveIf(colRelatedPlannerTasks,true));RemoveIf(colRelatedPlanners,true));

    inside ForAll as the last step of each iteration,, so seems we have a point in time where we can clear the collection using RemoveIf() and start over. am i correct? or i am missing something?

     

     

  • Pstork1 Profile Picture
    66,093 Most Valuable Professional on 16 Jun 2024 at 00:27:19
    Re: is there any way to clear a collection inside a ForAll()

    Yes, that seems to work but there isn't a Clear or ClearCollect inside the formula of any of the ForAll().  The Clear() and ClearCollect() functions are all executed before you get to the first ForAll().  That wasn't the scenario you were asking about. You were asking "is there any way to clear a collection inside a ForAll()'. There isn't for the reasons I mentioned.  The ones you are using aren't inside a ForAll.

  • johnjohnPter Profile Picture
    1,546 Super User 2024 Season 1 on 16 Jun 2024 at 00:14:19
    Re: is there any way to clear a collection inside a ForAll()

    @Pstork1 any further advice how this worked in my case, i mean in my case the FoAll() worked as regular loop

  • johnjohnPter Profile Picture
    1,546 Super User 2024 Season 1 on 15 Jun 2024 at 23:09:30
    Re: is there any way to clear a collection inside a ForAll()

    @Pstork1  but using RemoveIf(), as follow in 2 places, fixed my problem, and it actually clear the collection after each iteration:-

     

    ClearCollect(finalResult,{PlanTitle:"",TaskTitle:"",Status:"",AssignedToUserId:"",DueDate:""});
    Clear(colRelatedGroupsIds);
    Clear(colRelatedPlanners);
    Clear(colRelatedPlannerTasks);
    
    ClearCollect(colTeamSitesItem,Filter('Team Sites',1=1));
    ClearCollect(colAllGroups,Office365Groups.ListGroups().value);
    ForAll(colAllGroups As group,
    ForAll(colTeamSitesItem As i2,
    
    If(Lower(group.displayName)=Lower(i2.'Team Site Name'),
    Collect(colRelatedGroupsIds,group.id))));
    
    ForAll(colRelatedGroupsIds As group,
    Collect(colRelatedPlanners,
    Planner.ListGroupPlans(group.Value).value);
    
    ForAll(colRelatedPlanners As planner,
    Collect(colRelatedPlannerTasks,
    Planner.ListTasksV3(planner.id,group.Value).value);
    ForAll(colRelatedPlannerTasks As task,
    ForAll(task._assignments As taskAssignment,
    Patch(finalResult,Defaults(finalResult),
    {
    PlanTitle:planner.title,
    AssignedToUserId:taskAssignment.userId,
    Status:task.percentComplete,
    TaskTitle:task.title,
    DueDate:task.dueDateTime
    })
    
    
    ));RemoveIf(colRelatedPlannerTasks,true));RemoveIf(colRelatedPlanners,true));

    not sure if this is compatible with ur assumption, or i am missing something?

  • johnjohnPter Profile Picture
    1,546 Super User 2024 Season 1 on 15 Jun 2024 at 23:08:21
    Re: is there any way to clear a collection inside a ForAll()

    @rzuber yes it worked, i modified my formula as follow to do 2 RemoveIf() and now there is not any duplicate tasks:-

     

    ClearCollect(finalResult,{PlanTitle:"",TaskTitle:"",Status:"",AssignedToUserId:"",DueDate:""});
    Clear(colRelatedGroupsIds);
    Clear(colRelatedPlanners);
    Clear(colRelatedPlannerTasks);
    
    ClearCollect(colTeamSitesItem,Filter('Team Sites',1=1));
    ClearCollect(colAllGroups,Office365Groups.ListGroups().value);
    ForAll(colAllGroups As group,
    ForAll(colTeamSitesItem As i2,
    
    If(Lower(group.displayName)=Lower(i2.'Team Site Name'),
    Collect(colRelatedGroupsIds,group.id))));
    
    ForAll(colRelatedGroupsIds As group,
    Collect(colRelatedPlanners,
    Planner.ListGroupPlans(group.Value).value);
    
    ForAll(colRelatedPlanners As planner,
    Collect(colRelatedPlannerTasks,
    Planner.ListTasksV3(planner.id,group.Value).value);
    ForAll(colRelatedPlannerTasks As task,
    ForAll(task._assignments As taskAssignment,
    Patch(finalResult,Defaults(finalResult),
    {
    PlanTitle:planner.title,
    AssignedToUserId:taskAssignment.userId,
    Status:task.percentComplete,
    TaskTitle:task.title,
    DueDate:task.dueDateTime
    })
    
    
    ));RemoveIf(colRelatedPlannerTasks,true));RemoveIf(colRelatedPlanners,true));

     

  • rzuber Profile Picture
    545 Super User 2025 Season 1 on 15 Jun 2024 at 22:50:08
    Re: is there any way to clear a collection inside a ForAll()

    @Pstork1 nice explanation!

  • Pstork1 Profile Picture
    66,093 Most Valuable Professional on 15 Jun 2024 at 22:45:28
    Re: is there any way to clear a collection inside a ForAll()

    The problem is you are thinking of a ForAll() as a looping structure. Its not. Power Apps is declarative. A ForAll() applies the expression you specify to all the items in the scope you specify simultaneously. So there isn't a point in time where you can clear the collection and start over.  If you want to do that you should call a flow and have the flow do it in an actual loop.

  • rzuber Profile Picture
    545 Super User 2025 Season 1 on 15 Jun 2024 at 22:17:21
    Re: is there any way to clear a collection inside a ForAll()

    You could use RemoveIf()?

     

    RemoveIf(collection, true)

     

    That should be the same as Clear(collection)..

     

    If that doesn't work, add a boolean column to your collection for deletion and UpdateIf() it for deletion later, and filter out the 'deleted' columns?

     

    One thing though... I don't think you can remove something from a collection that is the source for your ForAll()... Which is probably why the Clear() function isn't working.. You might want to figure out a way to filter the collection into a table, and then use the table in the ForAll() loop. I suspect (but can't verify atm) the Clear() function would work fine if you're not looping through the collection you're trying to clear.

     

    [about to fire up my work laptop to check]

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

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,776 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 66,093 Most Valuable Professional

Leaderboard
Loading started