Canvas Apps Error Management Expressions

How to fix – Error when trying to retrieve data from the network

Is this you when you encounter this error?
Is this you when you encounter this error?

Introduction to the network error

Some errors in Power Apps are really annoying. Especially ones that the app checker does not flag unless you click on a button in preview mode or when playing the published app. These are called runtime errors. One such error is "Error when trying to retrieve data from the network". I had encountered this error for the 1st time a long time ago. But every time I thought about blogging it, I wasn't able to reproduce the error. Today, finally, when my co-worker and friend Edrei Mpanduki (LinkedIn / Twitter) reached out with the same issue, I decided to look at the specifics of the error so I can reproduce and blog about it.

Network error example

To illustrate this error, let's assume the following:

  1. Instructors table with a lookup to the Departments table (Department ID - column name)
  2. Courses table with a lookup to the Departments table (Department - column name)
  3. A collection of instructors (colInstructors)

The use case is to collect all courses that are related to the departments to which the instructions in the colInstructors collection belong.

The following code (OnSelect of a button) appears to be correct and it is, at least from a syntax perspective:

ForAll(
    colInstructors,
    Collect(
        colCourses,
        Filter(
            [@Courses],
            Department.Department = 'Department ID'.Department
        )
    )
)

The problem is you get a runtime error when you click on the button.

Network error
Network error

Note: You get this error if you turn on the "Formula-level error management" app setting. If you turn it off, you may see a different message e.g. The requested operation is invalid (followed by additional details).

The main culprit here is that Power Apps is not able to figure out who the Department ID column belongs to: the Courses table or the colInstructors collection. This happens when the code is nested within functions. Like in this example, the Filter is nested within Collect. Which, in turn, is nested within the ForAll function. We will now look at 3 ways to fix this error.

Fix 1

The 1st way is to use the disambiguation operator (since disambiguation is the main issue here).

ForAll(
    colInstructors,
    Collect(
        colCourses,
        Filter(
            [@Courses],
            Department.Department = colInstructors[@'Department ID'].Department
        )
    )
)

The @ symbol helps clarify that the Depratment ID column belongs to the colInstructors collection. To learn more about the disambiguation operator, click here.

Fix 2

The 2nd way is to add the Department GUID to the colInstructors collection itself, using a completely different name, so it can be referenced directly without any need for disambiguation.

ClearCollect(
    colInstructorsWithDepartmentID,
    AddColumns(
        colInstructors,
        "DepartmentGUID",
        'Department ID'.Department
    )
);
ForAll(
    colInstructorsWithDepartmentID,
    Collect(
        colCourses,
        Filter(
            [@Courses],
            Department.Department = DepartmentGUID
        )
    )
)

The DepartmentGUID column stores the GUID of the Department in the colInstructorsWithDepartmentID collection. It can then be referenced inside the Filter function without any need for disambiguation.

Fix 3

The 3rd way is to use the As operator that helps solve the issue in a way similar to the disambiguation operator.

ForAll(
    colInstructors As InstructorRecords,
    Collect(
        colCourses,
        Filter(
            [@Courses],
            Department.Department = InstructorRecords.'Department ID'.Department
        )
    )
)

The fact that this allows us to use InstructorRecords.'Department ID' clearly identifies Department ID as the column of the records in the colInstructors collection. To learn more about the As opeartor, click here.

Summary - Network error

Next time you see the dreaded "Error when trying to retrieve data from the network" message in your app checker, try out one of the above three methods!

Recent articles

  1. Fluent UI Combo Box Control - Better than classic?
  2. How to create checklists using galleries in Power Apps
  3. How to master Dataverse relationships in canvas apps!

2 thoughts on “How to fix – Error when trying to retrieve data from the network”

Leave a Reply