Canvas Apps Performance

How to optimize passing a record to Patch

Applying a patch to someone

Introduction to Patch

Patch function is used to update existing records or create new records. When updating an existing record, the 2nd parameter to the function is the record that needs to be updated.

Patch with nested calls

Typically, you would use Lookup to find the 1st record that meets a condition. However, sometimes a Lookup is not good enough. For example, if there are multiple records with the first name Hardit and you want the one that was created last, you would use the following code:

First(
        SortByColumns(
            Filter(
                Users,
                'First Name' = "Hardit"
            ),
            "createdon",
            SortOrder.Descending
        )
    )

To update this record, one approach can be:

Patch(
    Users,
    First(
        SortByColumns(
            Filter(
                Users,
                'First Name' = "Hardit"
            ),
            "createdon",
            SortOrder.Descending
        )
    ),
    {
        'Main Phone': "0123456789"
    };
)

The optimized Patch

As you can see, the problem with that approach is that it has nested data calls which increase the response time. To optimize the patch. use the following code:

With(
    {
        varUser: First(
            SortByColumns(
                Filter(
                    Users,
                    'First Name' = "Hardit"
                ),
                "createdon",
                SortOrder.Descending
            )
        )
    },
    Patch(
        Users,
        varUser,
        {'Main Phone': "9876543210"};
    )
)

Here is a screenshot of the monitor tool that shows the difference in times for the 2 approaches:

Data calls for both Patch approaches

The total time for the 1st approach is: 124 + 4013 + 674 = 4811 ms

Whereas the total time for the 2nd approach is: 175 + 195 + 109 = 479 ms (1/10th of the time for approach 1)

Recent articles

  1. How to optimize fetching multiple columns of a record
  2. How to optimize Lookup function by not using dot operator
  3. Responsive pop-up using layout containers

3 thoughts on “How to optimize passing a record to Patch”

  1. Thanks Hardit. Super helpful. What is the overall principle here? I look at both methods and it looks like the same amount of processing, but in the second case the filtering is done before the patch.

Leave a Reply