cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
martinav
Super User
Super User

Increment in a ForAll() with a condition

Shane young did a good job of explaining how to use Sequence() for incrementing in a ForAll() (Click for Shane's Video) .  However, I need to increment inside my ForAll() if a condition is met.  Thus, Sequence is a no-go.  Since Set() and UpdateContext() arent happy inside ForAll(), is there another option?

 

Just to start discussion, this is the theory of what I want to do.  However, it wont work because UpdateContext() isnt valid.

 

 

With({_FData:FilteredData},
    ForAll(_FData,Patch(FilteredData,LookUp(FilteredData,Index=ThisRecord.Index),{GroupIndex:GpIndx});If(LookUp(FilteredData,Index=Index+1).Valve_Type<>ThisRecord.Valve_Type,UpdateContext({GpIndx:GpIndx+1})))

 

 

2 ACCEPTED SOLUTIONS

Accepted Solutions
PaulD1
Community Champion
Community Champion

Although you cannot do a Set or UpdateContext in a ForAll, I believe you can patch to a collection, so you can update a specific record in an existing collection and refer back to it.

Note that ForAll is not guaranteed to process records in order so do be careful if that could be an issue in your scenario.

View solution in original post

Adrian_Celis
Super User
Super User

Hi @martinav 

@PaulD1 is right. What I would do is create a temp Collection and then increment the value there.

Sample:

ClearCollect(collTemp,{Value:0});

With({_FData:FilteredData},
    ForAll(_FData,Patch(FilteredData,LookUp(FilteredData,Index=ThisRecord.Index),{GroupIndex:GpIndx});
If(LookUp(FilteredData,Index=Index+1).Valve_Type<>ThisRecord.Valve_Type,Patch(collTemp,First(collTemp),{Value:Value+1})));

UpdateContext({varResult:First(collTemp).Value});

varResult should be your total incremented.

Addt'l notes :  You can use With inside a ForAll but I still can't find a way to transfer a With variable to a context or global variable.

 

View solution in original post

4 REPLIES 4
PaulD1
Community Champion
Community Champion

Although you cannot do a Set or UpdateContext in a ForAll, I believe you can patch to a collection, so you can update a specific record in an existing collection and refer back to it.

Note that ForAll is not guaranteed to process records in order so do be careful if that could be an issue in your scenario.

Adrian_Celis
Super User
Super User

Hi @martinav 

@PaulD1 is right. What I would do is create a temp Collection and then increment the value there.

Sample:

ClearCollect(collTemp,{Value:0});

With({_FData:FilteredData},
    ForAll(_FData,Patch(FilteredData,LookUp(FilteredData,Index=ThisRecord.Index),{GroupIndex:GpIndx});
If(LookUp(FilteredData,Index=Index+1).Valve_Type<>ThisRecord.Valve_Type,Patch(collTemp,First(collTemp),{Value:Value+1})));

UpdateContext({varResult:First(collTemp).Value});

varResult should be your total incremented.

Addt'l notes :  You can use With inside a ForAll but I still can't find a way to transfer a With variable to a context or global variable.

 

martinav
Super User
Super User

@Adrian_Celis 

 

Yes! Patch!  I should have thought of that.. I mean, I'm doing that already.  Thanks for the reminder!

 

ROd

@PaulD1 

 

THats an interesting point about records not going in order.  I will watch my output.  Since this is a one time thing.  I can explicity lookup each time, and override the order.   GOod point!


Thanks

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Carousel_PP_768x460_Wave2 (1).png

2022 Release Wave 2 Plan

Power Platform release plan for the 2022 release wave 2 describes all new features releasing from October 2022 through March 2023.

365 EduCon 768x460.png

Microsoft 365 EduCon

Join us for two optional days of workshops and a 3-day conference, you can choose from over 130 sessions in multiple tracks and 25 workshops.

Top Solution Authors
Users online (3,341)