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?
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.
@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?
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.
@Pstork1 any further advice how this worked in my case, i mean in my case the FoAll() worked as regular loop
@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?
@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));
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.
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]
WarrenBelz
146,776
Most Valuable Professional
RandyHayes
76,287
Super User 2024 Season 1
Pstork1
66,093
Most Valuable Professional