Custom Functions in Canvas App

Custom Functions in Canvas app are user-defined functions that can be created to perform specific tasks or calculations within the app. These functions can be created using the Power Apps formula language and can be used anywhere within the app where formulas are supported.

Creating custom functions in a Canvas app can be useful for several reasons. For example, it can help reduce the amount of formula code needed in an app, making it easier to maintain and update. Additionally, custom functions can be reused throughout the app, providing a consistent way of performing certain tasks.

To create a custom function in a Canvas app, you will need to follow these general steps:

  1. Open the formula bar and click on the “Edit” button.
  2. Click on “Functions” to open the functions pane.
  3. Click on “New Function” to create a new function.
  4. Provide a name for the function and specify the inputs and outputs.
  5. Write the formula code for the function.
  6. Save the function.

Once the function is saved, it can be used within the app by calling its name and passing in any required input values. Custom functions can also be organized into libraries, making it easier to manage and reuse them throughout the app.

Some examples of tasks that could be performed using custom functions in a Canvas app include data validation, text formatting, and complex calculations. Custom functions can be particularly useful for performing calculations that involve multiple steps or complex logic, as they can help simplify and streamline the code needed to perform these calculations.

Example of Custom Function:

Let’s say you have a text input control where users enter their email address, and you want to ensure that the email address is in a valid format before it’s saved to your data source. You can create a custom function to validate the email address.

  1. Open the formula bar and click on the “Edit” button.
  2. Click on “Functions” to open the functions pane.
  3. Click on “New Function” to create a new function.
  4. In the “Name” field, enter a name for the function, such as “IsValidEmail”.
  5. In the “Inputs” section, add an input parameter called “emailAddress” with a data type of “Text”.
  6. In the “Output” section, set the data type to “Boolean”.
  7. In the formula bar, write the formula code for the function:
    • If(
    •     !IsMatch(emailAddress, “^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$”),
    •     false,
    •     true
    • )
  8. Save the function.

Once you have created this custom function, you can use it in any formula throughout your app by calling its name and passing in the email address as an argument. For example, you could use it in the OnSelect property of a button that saves the email address to a data source:

If(

    IsValidEmail(TextInput1.Text),

    Patch(MyDataSource, {Email: TextInput1.Text}),

    Notify(“Invalid email address”, NotificationType.Error)

)

In this example, the IsValidEmail function is used to check if the email address entered in TextInput1 is valid before attempting to save it to the MyDataSource data source. If the email address is not valid, the user is notified with an error message.


Posted

in

by

Tags: