web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Clear and ClearCollect...
Power Apps
Unanswered

Clear and ClearCollect Based on FormNew or FormEdit

(1) ShareShare
ReportReport
Posted on by 1,265

I am working with an editable grid gallery shown with a Main Form.  When the screen opens (becomes visible), I have a button (btnLoadData) with OnSelect = ClearCollect(colSFDetail,ShortFormDetail).  This populates the gallery based on a common text field with the two SP Lists.  This seems to work fine when editing a record.  However, when creating a Main Form New Record, it populates the gallery with data where the Main Form text is empty.

 

Does the button need an "If" in the OnSelect based on whether the FormMode is Edit or New (or am I making this too complicated)?

Categories:
I have the same question (0)
  • poweractivate Profile Picture
    11,078 Most Valuable Professional on at

    First, I want to inquire about why you're not using SubmitForm in this scenario. If it's a simple submission, that function often provides an easier approach. However, since you have a specific requirement for using Patch on a Gallery, we can continue with that method.

    Given your requirements, let's approach this using Power Apps' built-in FormMode for better clarity.

    Here's the process:

    1. Set Form Mode Variable: On the OnVisible property of the Screen, you can hard code the variable varFormMode as FormMode.New. It will be up to you to determine how to decide what mode it should be in based on user interactions or other logic.

     

    Set(gloFormMode, FormMode.New)
    

     

    It will be up to you to determine how to decide when the DefaultMode should be FormMode.New and when it should be FormMode.Edit based on user interactions or other logic for your specific app.

    If you only need it on the one screen, use UpdateContext.

     

    UpdateContext({varFormMode: FormMode.New}) //Use FormMode.Edit for Edit mode

     

    I'll be assuming the context variable for the next steps.

     

    2. Go to your Form's DefaultMode property and put this in

     

    varFormMode

     

     

    3. Use Different Collections Based on Form Mode: You'll want to ClearCollect to different collections based on the form mode:

     

    If(
     varFormMode = FormMode.New,
     ClearCollect(colNewRecords, ShortFormDetail),
     ClearCollect(colBaseRecords, ShortFormDetail)
    )
    

     

    4. Patch Records Based on Form Mode: Wherever you may be Patching the records, use another If to differentiate between new and existing records:

     

    If(
     varFormMode = FormMode.New,
     Patch(splistdatasource, Defaults(splistdatasource), colNewRecords),
     Patch(splistdatasource, colBaseRecords, colChangeRecs)
    )
    

     

     This approach sets up the form mode explicitly and uses it to control behavior for new and existing records.

    Now you should have clear and maintainable behavior for editing and creating records based on your gallery.

     

    Adjust the collection names, datasource, and other parts of the formula to match your specific app.

     

    Se if it helps @RJF61 

     

  • RJF61 Profile Picture
    1,265 on at

    @poweractivate Thanks for the response.  I am still learning Power Apps so your initial question regarding the SubmitForm may be a better method.  Following is a current pic of a form in the PowerApp.

    RJF61_1-1691774621998.png

    When the App initially opens, there is a Gallery Screen.  With selection of gallery record, it then opens the form shown.  The Main Form is based on SP List "ShortForm".  The Detail Form is based on SP List "ShortFormDetail".  When editing an existing record, all works fine.  When at the originally gallery (if I create a new record) the Detail Form maintains the previous record.  That is what is driving my question regarding clearing the Collection.

     

    All suggestions are appreciated.

     

     

     

     

     

     

     

  • poweractivate Profile Picture
    11,078 Most Valuable Professional on at

    @RJF61 

    How are you creating new Gallery record? If it's a button, go to its OInSelect property and add a formula like this:
    Clear(yourCollection)
    where yourCollection is the collection you're using to keep the last opened record.

    See if it helps @RJF61 

  • RJF61 Profile Picture
    1,265 on at

    @poweractivate So as I have created this App, creating a new gallery record is one of my questions that I have not resolved.  When I select "Create A New Record" from the opening gallery, I receive the following screen...

    RJF61_0-1691778771825.png

    As shown, the Main Form has been reset, but the Collection (gallery with the red arrow) contains data.

  • poweractivate Profile Picture
    11,078 Most Valuable Professional on at

    @RJF61 

    Where did you click "Create A New Record" from the opening gallery?
    Wherever you did that, check for the control you clicked, go to the OnSelect property of that Control, add this formula after what's already there now (terminate the formula that's there with a semicolon if needed)

    Clear(colSFDetail);

    See if it helps @RJF61 

  • poweractivate Profile Picture
    11,078 Most Valuable Professional on at

    @RJF61 

    Regarding checking the FormMode  it's possible to verify the FormMode of a Form like this:

     

    If(yourForm.Mode = FormMode.Edit,...,...)

     

     However, it's possible you want to both set the FormMode and use the determined FormMode to take an action, in which case a context variable may still be more appropriate than checking the FormMode directly. However you may use either/both approaches as you see fit.

  • RJF61 Profile Picture
    1,265 on at

    @poweractivate Did that and it worked until the NewForm Screen opens (which has a button with the following)...

    ClearCollect(colSFDetail,ShortFormDetail).  This is collecting the related records to the MainForm.

     

    What syntax do I use to ClearCollect the NewForm (something like)...

     

    ClearCollect(colSFDetal, ShortFormDetail, Gallery.Selected.Title = ShortFormDetail.Title)

     

    This doesn't work.  Indicates I am comparing Text to a Table.  I suppose it also would not work when creating New record.

  • poweractivate Profile Picture
    11,078 Most Valuable Professional on at

    ClearCollect already clears it, so should be just

    ClearCollect(colSFDetail, what you want to collect here)

    See if it helps @RJF61 

  • RJF61 Profile Picture
    1,265 on at

    @poweractivate Understood.  Just appears to me that it is "collecting" grid records where the "Titles" are not equal.Whn the MainForm is a new record, not Title is present; yet, the collection is still pulling records.

     

    Make sense?

  • poweractivate Profile Picture
    11,078 Most Valuable Professional on at

    @RJF61 
    You may need to try something like this
    When you click on the Create a New Record specifically you should Clear the collection entirely using the OnSelect of Create a New Record, like this:

    Clear(colSFDetail);

    To take the last detail item that was in there out as I think it is still keeping the item you had before when selecting from the Gallery I believe. 


    Your existing OnSelect of Gallery can remain the same, this is where I assume you may have your existing ClearCollect there. So when you select an existing record those should still work. However when you Create a new record you want the Collection Cleared if I understand correctly. 

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 356 Most Valuable Professional

#2
11manish Profile Picture

11manish 225 Super User 2026 Season 2

#3
Mohsin Ali Profile Picture

Mohsin Ali 211

Last 30 days Overall leaderboard