Canvas Apps Expressions

How to fix a weird behavior with search function?

A screenshot of Google search screen
Google search

Introduction to Search function

Search is one of the most common use cases in canvas apps. The concept of the search function is pretty straightforward. You specify the following parameters:

  1. A table name
  2. A search text
  3. Search column(s) against which the search needs to be performed

To learn more about the function, click here.

Usage of Search function

The behavior of search is supposed to be very simple. With no search text, all records will show up, say, in a gallery. With search text, all those records will show that have the search text within the specified column in the search function.

For example, the following will show all accounts when search text is blank (txtAccountName_Search is a text input control):

Search(
        Accounts,
        txtAccountName_Search.Value,
        'Account Name'
    )

But if the search text isn't blank or an expression like below will show only those Accounts where the Account Name has the word coffee in it:

Search(
        Accounts,
        "coffee",
        'Account Name'
    )

A bug in Search function?

I noticed this recently that my Search was functioning fine within the studio but not when playing the published app. I spent a lot of time trying to figure out the root cause of this behavior. The issue was that the gallery was returning no results when the search text was blank. But as soon as you would type something in the search text box, the gallery would start showing appropriate results.

Behavior in the studio:

Search functionality within the studio
Search functionality within the studio

Behavior of the published app:

Search functionality in published app
Search functionality in published app

Fixing Search

I am not sure if this is a bug or the "new" expected behavior, but the only way out I could think of is to use the Search function only when the search text isn't blank:

If(
    !IsBlank(txtAccountName_Search.Value),
    Search(
        Accounts,
        txtAccountName_Search.Value,
        'Account Name'
    ),
    Accounts
)

So if the search text is blank, the Items property of the gallery gets set to the Accounts table and thus, returns all records. When the search text isn't blank, the Search function kicks in and shows only the appropriate records.

Recent articles

  1. How to choose between the different types of Power Apps
  2. How to make Power Apps more accessible?
  3. How to optimize performance in Power Apps

Leave a Reply