Power Apps Error Handling Guidelines

Power Apps Error Handling Guidelines
Table Of Contents:
• Enable Formula-Level Error ManagementPatch Function Error-HandlingPower Apps Forms Error-HandlingPower Automate Flow Error-HandlingIfError FunctionHandling Unexpected Errors




Enable Formula-Level Error Management

Open Power Apps advanced settings and turn on formula-level error management.  This setting enables use of the IfError function, the IsError function and the app’s OnError property.




Patch Function Error-Handling

Check for errors anytime data is written to a datasource with the Patch function or Collect Function.  Even if the submitted record(s) are valid, good connectivity and the correct user permissions cannot be assumed.  This is not necessary for local collections stored in memory.


// Create a new invoice
If(
    IsError(
        Patch(
            'Invoices',
            Defaults('Invoices'),
            {
                CustomerNumber: "C0001023",
                InvoiceDate: Date(2022, 6, 13)
                PaymentTerms: "Cash On Delivery",
                TotalAmount: 13423.75
            }
        )
    ),
    // On failure
    Notify(
        "Error: the invoice could not be created",
        NotificationType.Error
    ),
    // On success
    Navigate('Success Screen')
)




Power Apps Forms Error-Handling

Write any code should be executed after a Power Apps form is submitted in its OnSuccess and OnFailure properties. If form submission is successful, use the OnSuccess property to control what happens next. Otherwise, use the OnFailure property to display an error message that tells the user what went wrong.

// OnSelect property of the form's submit button
SubmitForm(frm_Invoice);

// OnSuccess property of the form
Navigate('Success Screen');

// OnFailure property of the form
Notify(
    "Error: the invoice could not be created",
    NotificationType.Error
);



Do not write any code after the SubmitForm function used to submit the form. If form submission fails Power Apps will still move onto the next line of code. This can result in loss of data.

// OnSelect property of the form's submit button
SubmitForm(frm_Invoice);
Navigate('Success Screen');




Power Automate Flow Error-Handling

When a Power Automate flow is triggered from Power Apps its response must be checked for errors.  Flows can fail due to poor connectivity.  They can also return a failure response or a result with the incorrect schema.  If Power Apps does not know the flow failed it will continue as normal.


// Get customer invoices
If(
    IsError(
        GetAllCustomerInvoices.Run("C0001023")
    ),
    // On failure 
    Notify(
        "Error: could not retrieve customer invoices",
        NotificationType.Error
    ),
    // On success
    Navigate('Success Screen')
)




IfError Function

Use the IfError function to handle calculations that require a different value when an error occurs.  In this example, if gblTasksTotal equals 0 the IfError function will return 0 instead of throwing a “divide by zero” error.

// calculate the percentage of tasks completed
IfError(
    gblTasksCompleted/gblTasksTotal,
    0
)




Handling Unexpected Errors

An app’s OnError property is triggered when an unexpected error occurs.  An unexpected error is any error that is not handled using the IfError or IsError function.

Use this code to quickly locate the source of unexpected errors and fix them.  Do not leave this on in a production app since the error message is helpful for a developer but is confusing to a user.  If you want to see unexpected errors in a production app use the Trace function and Azure Application insights to silently log the errors.

// unexpected error notification message
Notify(
    Concatenate(
        "Error: "
        FirstError.Message,
        "Kind: ",
        FirstError.Kind,
        ", Observed: ",
        FirstError.Observed,
        ", Source: ",
        FirstError.Source
    ),
    NotificationType.Information  
);







Questions?

If you have any questions about Power Apps Error Handling Guidelines please leave a message in the comments section below. You can post using your email address and are not required to create an account to join the discussion.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments