cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
BrianHFASPS
Continued Contributor
Continued Contributor

Multiple functions inside a ForAll loop? Can you?

I am trying to figure out if it is possilbe to do two things inside a ForAll loop or if I must do two loops. I need to do a an If to set a variable and then use that variable in a patch. I guess I could do a nested if or reproduce the patch statement lots of times. It would just be so much cleaner if I could do If then a Patch within the same loop.

1 ACCEPTED SOLUTION

Accepted Solutions

Hi @BrianHFASPS 

You should be able to run multiple functions in a ForAll loop by using the semi-colon character to separate each function. Maybe you could post the error that you're receiving?

Here's an example I just put together that works for me.

 

ClearCollect(
    InputCollection,
    {legs: 4,desc: "cat"},
    {legs: 4,desc: "dog"},
    {legs: 2,desc: "monkey"}
);
ForAll(
    InputCollection,
    If(legs = 2,Collect(TwoLegCollection, {desc: desc}));
    If(legs = 4,Collect(FourLegCollection, {desc: desc}))
)

Notice the 'trick' I'm using here. Although you can't use ClearCollect or Set inside a ForAll, you can use Collect. So instead of storing data values in variables, you can Collect them into a temporary collection and carry out whatever tasks you need to carry out after the ForAll loop.

View solution in original post

8 REPLIES 8
timl
Super User
Super User

Hi @BrianHFASPS 

Although it's possible to do multiple things inside a ForAll loop, setting variables is something that isn't allowed. Theoretically, PowerApps can execute the contents of a ForAll loop in parallel, so this is the reason why calls to Set or ClearCollect are not permitted.  

BrianHFASPS
Continued Contributor
Continued Contributor

Very good to know. That is great thing for performance but would definitely break my methods. How can you do multiple things in ForAll() loop though? I can't figure out syntax?

 

ForAll(Collection,

If(),

If()

)

Doesn't work or ; it seems. What am I missing?

Hi @BrianHFASPS 

You should be able to run multiple functions in a ForAll loop by using the semi-colon character to separate each function. Maybe you could post the error that you're receiving?

Here's an example I just put together that works for me.

 

ClearCollect(
    InputCollection,
    {legs: 4,desc: "cat"},
    {legs: 4,desc: "dog"},
    {legs: 2,desc: "monkey"}
);
ForAll(
    InputCollection,
    If(legs = 2,Collect(TwoLegCollection, {desc: desc}));
    If(legs = 4,Collect(FourLegCollection, {desc: desc}))
)

Notice the 'trick' I'm using here. Although you can't use ClearCollect or Set inside a ForAll, you can use Collect. So instead of storing data values in variables, you can Collect them into a temporary collection and carry out whatever tasks you need to carry out after the ForAll loop.

pbongard
New Member

Hello,

 

The solution don't work with me :

I get the msg "Number of arguments not ok : 3 given, 2 asked" :

 

pbongard_0-1655907307459.png

 

See my code below :

 

 

ForAll(
    LISTE_ELEMENTS.Elements;
    ForAll(
        Elements;
        Collect(
            ExportCSVCol;
            {
                Titre4:Text(field_1);
                Titre5:Text(Title);
                Titre6:Text(field_2);
                Titre7:Text(field_3);
                Titre8:Text(field_4);
                Titre9:Text(field_5);
                Titre10:Text(field_6)
            }
        )
    );;
    Collect(
        ExportCSVCol;
        {
                Titre1:LISTE_ELEMENTS.field_9;
                Titre2:LISTE_ELEMENTS.field_10
        }
    )
)

 

 

 

Maybe that's no longer possible ?

Have you got any idea @timl ?

 

I think some of your ; (semi-colons) need to be , (commas)

 

  LISTE_ELEMENTS.Elements;

 

  Elements;

 

  ExportCSVCol;

Hi,

In my version of power apps, the commas are not recognized

pjdiii
Frequent Visitor

You're missing a ) to close out the inner ForAll()

pbongard
New Member

i don't think so... but the the msg "Number of arguments not ok : 3 given, 2 asked" is strange if it's possible to put two functions in one ForAll()

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 (5,050)