Skip to main content

Notifications

Power Apps - Building Power Apps
Suggested answer

Create a collection on click of add button and save the collection to list

Posted on by 490
Need to create a collection on click of add button. The collection should allow me to add only 3 empty rows into it. If i add more than 3 rows, it should show me warning.
 
Once 3 rows are added, i should be able to edit those rows and add details into it. Something similar to below. 
 
On click of submit button in the form, the collection should get saved to the SharePoint list in backend.
 
 
May i know how to achieve this?
 
 
 
Categories:
  • LessCodePaths Profile Picture
    LessCodePaths 100 on at
    Create a collection on click of add button and save the collection to list
    Could you clarify which part does not work?
    If I would like to sum it up:
    1. You are able to add blank rows to the gallery and restrict rows count to 3
    2. You are able to save the rows to the SharePoint when editing is done. 
     
    If you want to edit rows separately, I advise add new column RowGuid to yourCollection
    Collect(
        yourCollection,
        {
            Name: "",
            Email: "",
            Designation: "",
            Contact: "",
            RowGuid: GUID()
        }
    )

    Then you can set variable on edit icon to specify which record is in edit mode:
    EditIcon.OnSelect
    Set(
       varGuidInEditMode,
       ThisItem.RowGuid
    )
    This variable should be used in DisplayMode property on all text inputs:
    TextInput.DisplayMode
    If(
       ThisItem.RowGuid=varGuidInEditMode,
       DisplayMode.Edit,
       DisplayMode.View
    )
    Then, when you are done with editing, you should clear varGuidInEditMode variable:
    Set(
       varGuidInEditMode,
       Blank()
    )
     
    I'm not sure about Save.
    It is working as expected? Are you want to save the data to SharePoint at once or row by row?
     
    L.
  • Iantaylor2050 Profile Picture
    Iantaylor2050 490 on at
    Create a collection on click of add button and save the collection to list
    Hi,
     
    I tried above code but its not working. If i click on edit button inside gallery, it should allow me to edit only that row and not other rows. Currently if i click on individual edit button, it allows me to edit all rows. 
     
    Also on form submit button, it saves 3 rows in datasource, however all 3 rows are blank, despite collection being populated with below rows. May i know why?
     
    Add button OnSelect
     
    Collect(
        yourCollection,
        {
            Name: "",
            Email: "",
            Designation: "",
            Contact: ""
        }
    )
     
    Add button Displaymode (to allow only 3 rows to be added)
     
    If(
        CountRows(yourCollection) < 3,
        DisplayMode.Edit,
        DisplayMode.View
    )
     
    Gallery Items property
    yourCollection
    Gallery Edit icon
    Set(editenabled,true)
    Form Submit button
    Patch(
        'User Details',
        ForAll(
            yourCollection As Items,
            {
                Title: 'Name'.Text,
                'Email': 'Email'.Text,
                'Designation': 'Designation'.Text,
                'Phone': 'Contact'.Text,
                User_List_ID:Form1_9.LastSubmit.ID
            }
        )
    )
     
  • Suggested answer
    LessCodePaths Profile Picture
    LessCodePaths 100 on at
    Create a collection on click of add button and save the collection to list
    Hi,
     
    I would recommend this:
     
    "Add" button:
    OnSelect - add blank row to collection
    Collect(yourCollection, {Name: "", Email: "", Designation: "", Contact: "", ID: Blank()})
    Visible - hide button when there are 3 rows in the collection
    CountRows(yourCollection)<=3
     
    1. Patching rows separately
    "Save" icon in each row:
    OnSelect - save to SharePoint and recognize it is required to create new or update existing row
    Patch(
        YourSharePointList,
        //define if new row should be created or existing row updated
        If(
           IsBlank(ThisItem.ID),
           Defaults(YourSharePointList),
           LookUp(YourSharePointList,ID=ThisItem.ID)
        ),
        {
            Title: ThisItem.Name,
            Email: ThisItem.Email,
            Designation: ThisItem.Designation,
            Contact: ThisItem.Contact
        }
    );
     
    2. Patching rows at once - Bulk patch
    "Save" icon below the gallery
    Patch(
        YourSharePointList,
        ForAll(
           YourGallery As Items,
           {
                 Title: TitleBox.Text,
                 Email: EmailBox.Text,
                 Designation: DesignationBox.Text,
                 Contact: ContactBox.Text,
                 ID: Items.ID
                 //based on this ID column SharePoint automatically know if new record should be created or existing should be patched re
                 
           }
        )
    )
     
    Hope it helps
    L.
  • Iantaylor2050 Profile Picture
    Iantaylor2050 490 on at
    Create a collection on click of add button and save the collection to list
    Hi All,
     
    Can please help me on the above?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November 2024 Newsletter…

November 2024 Community Newsletter…

Community Update Oct 28…

Power Platform Community Update…

Tuesday Tip #7 Community Profile Tips…

Welcome to a brand new series, Tuesday Tips…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 143,532

#2
RandyHayes Profile Picture

RandyHayes 76,308

#3
Pstork1 Profile Picture

Pstork1 64,050

Leaderboard