Lab - Using the ForAll function in a gallery

Completed

Important

For this lab, don't sign in with your credentials. Use the following steps to sign in to your lab environment with the correct credentials:

  1. Select Sign in to launch VM mode in this unit.
  2. A PowerShell window and a Windows Command Prompt window appear. After two to three minutes, they close, and Power Apps opens automatically. Wait for the Power Apps home screen.

To see the lab instructions, select the Instructions tab on the lab sidebar.

This hands-on lab shows you how to use the ForAll function in a gallery.

  1. Sign in to Power Apps. (If you're using the virtual environment, skip to step 2.)

  2. From the Home screen left-side navigation panel, select + Create > Blank app > Create.

    Screenshot of the Canvas app from blank feature.

  3. Name your app ForAll Exercise, leave the format as Tablet, and select Create (skip the popup that might appear on the app canvas).

  4. Select the Insert tab and add a Button control, and set its OnSelect property to this formula:

    ClearCollect(CityPopulations, 
    {City:"London", Country:"United Kingdom", Population:8615000}, 
    {City:"Berlin", Country:"Germany", Population:3562000}, 
    {City:"Madrid", Country:"Spain", Population:3165000}, 
    {City:"Rome", Country:"Italy", Population:2874000}, 
    {City:"Paris", Country:"France", Population:2273000}, 
    {City:"Hamburg", Country:"Germany", Population:1760000}, 
    {City:"Barcelona", Country:"Spain", Population:1602000}, 
    {City:"Munich", Country:"Germany", Population:1494000}, 
    {City:"Milan", Country:"Italy", Population:1344000})
    

    If you're using the virtual environment: to copy and paste the text in the virtual environment, select the Commands button on the top left corner of the screen, select Type text, then select Type clipboard text. Paste the copied text in the window.

  5. Select the Button control, and set its Text property to "Collect." Hold the Alt Key and press the Button control. This action creates a collection called CityPopulations and stores all the information in the collection.

  6. Select the + Insert option from the command bar, select a Vertical gallery control, and then choose CityPopulations as the data source.

    Screenshot of the more options selected and the gallery button highlighted.

  7. With the gallery selected, in the properties panel on the right, change the layout to Title, subtitle, and body.

  8. Select the Fields property and change the Body1 field to Population.

  9. With your gallery control selected, ensure that you can edit inside of the top item. Select the + Insert button again, then search for and add a Check box input. Drag it to the right side of the gallery, and set its Text property to "Add".

    Screenshot of the Canvas app with Gallery, Collect button and checkbox.

  10. Insert another button control, set its Text property to "ForAll Collect" and set its OnSelect property to this formula:

       Clear(NewCity);ForAll(Gallery1.AllItems,If(Checkbox1.Value= true ,Collect(NewCity,{City:City,Country:Country,Population:Population})))
    
  11. Insert another Vertical gallery, and choose NewCity as the data source.

  12. On the Properties panel, change the layout to Title, subtitle, and body.

  13. Select the Fields option to change the Body label to Population.

  14. Put your app in Preview mode, check the Add checkbox for several of the cities in your main gallery, and then select the ForAll Collect button. Notice how the new gallery reflects the items that you checked from the original gallery.

You have now used the ForAll command to add all checked items into a collection that displays these items in a new gallery.