Power Apps Patch

A student asked me if it is possible to create a new record in a Canvas App with a default value for a field. The field is based on a drop-down list. The value depends on which screen is opened.

My thoughts immediately went to the patch statement. Patch can be used to create new records, or to modify existing records. At the same time, I thought about the fact that a screen has to be opened to create a new record. When a screen is opened the screen becomes visible. The ‘On Visible method’ of a screen can be used to define a default value with the help of a variable. There is a difference between local and global variables, but for this blog that is out of scope.

Suggested solutions

  • In the OnVisible method of the screen, set a ‘Variable’ and specify the default value specifically for that screen.
  • Patch (‘Tabel Name’, Defaults(‘Table Name’), { ‘Name Field’:”default value”}). The parameter ‘Defaults’ creates a new record with default values as defined in the fields on the table. Passing the field names with a value overrides the default values defined on table level.

Example code variable

In the OnVisible of the Canvas App screen, add the following example code.

Set( MyVar, 'Choice Name'.Item1)

Example code patch

In the screen where you create the new record, add the following code in the OnSelect of a button (or icon).

Patch(
    'Table Name',
    Defaults('Table Name'),
    {
        'First Name': TextInput1.Text,
        'Last Name': TextInput2.Text,
        Company: TextInput3.Text,
        'Visitor Type': MyVar,
    }
);