Latest Posts

PowerApps remove all the records from collection


The Clear function in Power Apps is used to remove all records from a collection. This function is useful when you want to reset a collection to an empty state, either to prepare it for new data or to clear it as part of an app's logic.

PowerApps – Clear() function Syntax

Clear(CollectionName)

  • CollectionName: The name of the collection you want to clear.

Notes:

  1. Confirmation: If clearing a collection removes important data, consider adding a confirmation step to prevent accidental data loss.
  2. Default State: After clearing a collection, you might want to set it back to a default state or reload it with initial values if necessary.
  3. Check for Empty Collection: Before performing operations that depend on the collection, check if the collection is empty to avoid errors or unexpected behavior.

Example:

If(CountRows(Products) > 0, Notify("Collection is not empty"), Notify("Collection is empty"))

  1. Resetting Data: Use the Clear function to reset collections when starting a new process or workflow, ensuring that old data does not interfere with new data.
  2. Form Submission: Clear temporary collections used to hold form data after the form is submitted to prepare for the next submission.
  3. Conditional Logic: Use the Clear function as part of conditional logic to dynamically reset collections based on user actions or other events.

PowerApps – Clear a Simple Collection

Suppose you have a collection named MyCollection that you want to clear.

  1. Create the Collection:

ClearCollect(MyCollection, [1, 2, 3, 4, 5]);

 

  1. Clear the Collection:
    • Add a Button control.
    • Set the OnSelect property of the Button to:

Clear(MyCollection)

When you press the button, MyCollection will be cleared and will no longer contain any records.

PowerApps – Clear a Collection with Records

Suppose you have a collection named Products with records that you want to clear.

  1. Create the Collection:

ClearCollect(Products,

    Table(

        {ProductID: 1, ProductName: "Laptop", Price: 1000},

        {ProductID: 2, ProductName: "Tablet", Price: 500},

        {ProductID: 3, ProductName: "Monitor", Price: 300}

    )

);

  1. Clear the Collection:
    • Add a Button control.
    • Set the OnSelect property of the Button to:

Clear(Products)

When you press the button, Products will be cleared, removing all records from the collection.

PowerApps – Clear Multiple Collections

You can clear multiple collections by calling the Clear function for each collection.

  1. Create Multiple Collections:

ClearCollect(Collection1, [1, 2, 3]);

ClearCollect(Collection2, ["A", "B", "C"]);

  1. Clear Both Collections:
    • Add a Button control.
    • Set the OnSelect property of the Button to:

Clear(Collection1);

Clear(Collection2)

When you press the button, both Collection1 and Collection2 will be cleared.


We value your Feedback:

Page URL:

Name:

Email:


Suggestion:

© 2024 Code SharePoint