cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
FaisalAkbar
Level: Powered On

ForAll with UpdateContext

Hello, 

We have a situation where we have to create Reviews selecting Multiple Reviews from list, we are using checkboxes for multiple selection. There is no problem in selecting the checkbox and adding the required fields to a collection. My challenge is to run ForAll loop on that Collection.

My challenge is that I have to use UpdateContext within the ForAll. Unfortunately if I use UpdateContext within Forall, I get an error “This function cannot be invoked within ForAll”.

To create a Review, There are number of tables we have to Patch and Update, The first piece of code inserts the value in Site_Review Table, I need to use UpdateContext here to collection the Value of Site_Review_ID which is auto-generated Primary Key in SQL Server, I need this Site_Review_ID to attach Site Features in Site_Feature_Tab.

I tried to wrap this whole thing in ForAll but due to UpdateContext, I am unable to make this code functional. My code is below

ForAll(SiteReviewCol,
UpdateContext(
    {
        SiteReviewContext: Patch(
            '[dbo].[Site_Review]',
            Defaults('[dbo].[Site_Review]'),
            {
                Owner_ID: User().Email,
                Site_Name_ID: SiteReview_List.Selected.Site_Name_ID,
                Site_Name: Site_Review_List.Selected.Site_Name,
                DTRegion: Filter_Region_DD.Selected.Value,
                MeetingDate: TodaysDate.Text,
                ModifiedBySmiley Surprisedffice365Users.MyProfile().Mail,
               SubcriptionYear: Year(Today())
            }
        )
    }
);


ForAll(
    SiteForReview,
    Patch(
        '[dbo].[Site_Features_Tab]',
        Defaults('[dbo].[Site_Features_Tab]'),
        {
            Feature_ID: Feature_ID,
            Site_Review_ID: CPIReviewContext.CPI_Review_ID,
            Site_Name_ID: CPI_Name_ID,
            Attributes: Attributes,
            Significance: Significance_Note,
            SSIC_KM_L1: SSIC_KM_L1,
            SSIC_KM_L2: SSIC_KM_L2,
            SSCC_CM_L1: SSCC_CM_L1,
            SSCConditionCategory: SSCConditionCategory,
            SSCConditionBandValue: SSCConditionBandValue,
            SSCC_CM_L2: SSCC_CM_L2,
            SSCCandCategory: SSCCCategory,
            SSCCScoreValue: SSCCScoreValue,
            IsPrevious: "false"
             

        }
    )
)
)

So this is ForAll within ForAll and to run the nested ForAll, I need the Site_Review_ID to patch it to all the Features  associated with the Site_Review_ID. 

Can anyone help please?

5 REPLIES 5
rmmcgr
Level 8

Re: ForAll with UpdateContext

Could you try to seperate out the ForAll functions, so they are not nested and run one after the other, using the Collect function in the first ForAll to get the set of records you need?


Then in the second ForAll you iterate over the collection of newly created records and create the second set of records?

FaisalAkbar
Level: Powered On

Re: ForAll with UpdateContext

Hello, Thank you for responding, The problem is that One Site_Review has minimum 5 to 14 features attached with it, Features are created prior to the review is created and then Site_Review_ID is attached to those features. This is very easy if I have to do it for One Site Review, However, the sequence has to be like this

 

  1. Create Site Review
  2. Collect the features attached with the user name from a Bridge table
  3. Insert the features from Bridge table to Site_Feature_Tab with the Site_Review_ID created in the First step

Now I have to use ContextUpdate as you know the nature of the ContextUpdate is to store the relative record, I then have to use that Site Review ID to associate the related features to it. 

Any other possible solution please?

Highlighted
Community Support Team
Community Support Team

Re: ForAll with UpdateContext

Hi @FaisalAkbar,

Do you want to use UpdateContext function within the ForAll function in PowerApps?

Currently, within PowerApps, UpdateContext function is not supported to used within the ForAll function.

As an alternative solution, you could consider put the UpdateContext function outside the ForAll function to achieve your needs. I have made a test on my side, please take a try to modify your formula as below:

UpdateContext({               /* Please modify formula here -- Start */
      SiteReviewContext: ForAll(
                                SiteReviewCol,
                                Patch(
                                       '[dbo].[Site_Review]',
                                        Defaults('[dbo].[Site_Review]'),
                                        {
                                          Owner_ID: User().Email,
                                          Site_Name_ID: SiteReview_List.Selected.Site_Name_ID,
                                          Site_Name: Site_Review_List.Selected.Site_Name,
                                          DTRegion: Filter_Region_DD.Selected.Value,
                                          MeetingDate: TodaysDate.Text,
                                          ModifiedBySmiley Surprisedffice365Users.MyProfile().Mail,
                                          SubcriptionYear: Year(Today())
                                        }
                                    )
                                )
});                                  /* <-- Modify formula End */
ForAll( SiteForReview, Patch(
...

Or

ClearCollect(              /* Please modify formula here -- Start */
      SiteReviewContext, 
ForAll( SiteReviewCol, Patch( '[dbo].[Site_Review]', Defaults('[dbo].[Site_Review]'), { Owner_ID: User().Email, Site_Name_ID: SiteReview_List.Selected.Site_Name_ID, Site_Name: Site_Review_List.Selected.Site_Name, DTRegion: Filter_Region_DD.Selected.Value, MeetingDate: TodaysDate.Text, ModifiedBySmiley Surprisedffice365Users.MyProfile().Mail, SubcriptionYear: Year(Today()) } ) ) ); /* <-- Modify formula End */
ForAll( SiteForReview, Patch(
...

Please take a try with above solution I provided, then check if the issue is solved.

 

Best regards,

 

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Administrator
Administrator

Re: ForAll with UpdateContext

Hi @FaisalAkbar thank you for posting in the community. Can you review the latest reply and let us know if you need further assistance? 

 

@TopShelf-MSFT

Super User
Super User

Re: ForAll with UpdateContext

Although you cannot use an UpdateContext or a Set inside a ForAll loop, you can use a Collect.

So instead of (say) UpdateContext({SiteReviewContext: ...})

You can try the following

Outside of your ForAll, set up a collection as follows, creating a single row:

ClearCollect(colSiteReviewContext, {Value:0})

Then within your ForAll loop you can assign a value to that row:

ClearCollect(colSiteReviewContext, {Value:0});
ForAll(SiteReviewCol, Collect(ColSiteReviewContext,First(colSiteReivewContext), { Value: Patch( '[dbo].[Site_Review]', Defaults('[dbo].[Site_Review]'), { Owner_ID: User().Email, Site_Name_ID: SiteReview_List.Selected.Site_Name_ID, Site_Name: Site_Review_List.Selected.Site_Name, DTRegion: Filter_Region_DD.Selected.Value, MeetingDate: TodaysDate.Text, ModifiedByffice365Users.MyProfile().Mail, SubcriptionYear: Year(Today()) } ).Site_Review_ID } );

Note that we are returning your Site_Review_ID value from the Patch.

In your nested ForAll you can retrieve the value by using First(colSiteReviewContext).Value

This solution relies on overwriting the first value in an existing collection and then referencing that value.

 

Note: I don't have your data source so can't test the code above - there may be typos! But this approach has worked for me before to store a value from one ForAll loop and then use that value in a nested loop (in my case I was assigning a GUID rather than the result of a Patch, but give it a try!).