Blog
Dataverse - How to Patch the 5 most complex data types
January 19. 2021
With Dataverse, the syntax to patch certain data types can look very complicated. In this post, we'll summarise the syntax to patch lookup, single choice, multi-choice, yes/no, single person/group, and multi person/group columns.
In a previous post, I covered the more complex use cases of the Patch function against a SharePoint data source.
As I mentioned, Patch is the core function to add or update individual record in a data
source. Although it's simple to set the value of text and numeric fields, the syntax to set other data types can be complicated.
Reminder - Dataverse Data Types
To begin, let's take a moment to refamiliarise ourselves with the Dataverse data types. These are shown in the illstration beneath.
The majority of these data types are simple to work with. Column types such as email URL and phone number behave exactly like strings. When we set one of these column values through a call to patch, we can simply pass a string value.
Likewise, all the data types that appear beneath the whole number and date time groups behave as numbers and dates respectively. For these column types, we can simply patch numer or date values.
The more complex data types that we cover in this post appear in the lower section. These include choice, choices, lookup, and customer.
Customer is a notable data type because it's a special type of lookup column, called a polymorphic lookup. The thing that makes this data type special is that we can assign values from either the Account or Contact tables.
Likewise, all the data types that appear beneath the whole number and date time groups behave as numbers and dates respectively. For these column types, we can simply patch numer or date values.
The more complex data types that we cover in this post appear in the lower section. These include choice, choices, lookup, and customer.
Customer is a notable data type because it's a special type of lookup column, called a polymorphic lookup. The thing that makes this data type special is that we can assign values from either the Account or Contact tables.
Example Dataverse Table
To demonstrate the use of Patch, we'll use the Dataverse table shown here.
Column name | Data type | Description |
Property | Lookup | A lookup to the property table |
DocumentType | Choice | Single selection choice column with the following options - Document, Image, PDF,Audio |
SharePermissions | Choices | Multi-selection choice column with the following options - Can Share On Website Can Share With Customer Can Share Internally Can Share With Team |
ContainsPersonalInfo | Yes/No | A boolean yes/no field |
PropertyAgent | Customer | A lookup to the account table |
DocumentContributors | Person or Group | Allows multiple selections of persons or groups |
Let's now look at the Patch synax to add new records to this table.
1 . Lookup Column
To
patch a lookup column, we assign a record from the assoicated table by calling the LookUp function.
Here's the syntax to create a new record and to set the property lookup
value to the first property record with an Address1 value that matches
“10 High
Street”.
Patch(PropertyDocuments,
Defaults(PropertyDocuments),
{Name:"Contract Doc",
Property: LookUp(Properties, Address1="10 High Street")
}
)
If we were creating or updating a record and want to set a lookup value to null or blank, we can do this by setting the lookup value to Blank.
Patch(PropertyDocuments,
Defaults(PropertyDocuments),
{Name:"Contract Doc",
Property: Blank()
}
)
However, there's a very important caveat when we want to patch null or blank values. We must enable the 'formula level error management' setting. If this setting is not enabled, the patch function will not correctly set the null value.
2 . Choice column
Here's the syntax to create a new record and to
set the document type choice field to “PDF”
Patch(PropertyDocuments,
Defaults(PropertyDocuments),
{Name:"Contract Doc",
DocumentType: DocumentType.PDF
}
)
With choice columns, Power Apps is helpful because it exposes all available choice values through an enumeration. In
this example (shown in the screenshot beneath), we can access the available choices by typing the name of the column, followed by a
period symbol. The IntelliSense in the editor then shows the available choices.
3 . Choices column
To assign multiple values to a Choices column, we assign an array of
choice values. The syntax to create a new record and to
set the SharePermissions value to "Can Share On Website" and "Can Share
With Customer" looks like this.
Patch(PropertyDocuments,
Defaults(PropertyDocuments),
{Name:"Contract Doc",
SharePermissions: [SharePermissions.'Can Share On Website',
SharePermissions.'Can Share With Customer']
}
)
4 . Yes/No column
With
Dataverse yes/no fields, we can simply pass a boolean true or false
value. Here's the syntax to create a new record and to set the
ContainsPersonalInfo field to “yes”.
Patch(PropertyDocuments,
Defaults(PropertyDocuments),
{Name:"Contract Doc",
ContainsPersonalInfo: true
}
)
5 . Customer column
To
patch a customer column, we assign a record from either the Account or Contact table by calling the LookUp function.
Here's the syntax to create a new record and to set the PropertyAgent
value to the first record in the Account table with an account name that matches
“ACME Corp”.
Patch(PropertyDocuments,
Defaults(PropertyDocuments),
{Name:"Contract Doc",
PropertyAgent: LookUp(Accounts, 'Account Name'="ACME Corp")
}
)
Alternatively, here's the syntax to create a new record and to set the PropertyAgent
value to the first record in the Contact table with a Full Name value that matches
“Tim Leung”.
Patch(PropertyDocuments,
Defaults(PropertyDocuments),
{Name:"Contract Doc 6 - Account Lookup",
PropertyAgent: LookUp(Contacts, 'Full Name'="Tim Leung")
}
)
6 . User columns
Finally, here's a quick note on user columns. To add a 'user' column to a Dataverse table, we add a look up column that references the user table in Dataverse. To set this value with Patch, we can lookup the user by email address like so.
Patch(PropertyDocuments,
Defaults(PropertyDocuments),
{Name:"Contract Doc",
DocumentApprover: LookUp(Users, 'Email'="tim1@powerappsguide.com")
}
)
A mistake that some app builders make (particularly those from a SharePoint background) is to attempt patch a value that they retrieve from the 'Office 365 Users' connector. This will fail with an error and therefore, the correct approach is to look up the user record from the users table.
Conclusion
The syntax to patch complex data types to Dataverse can be complicated. This post provides a reference summary to help carry out this task.
Related posts
- Error - Diagnosing the error "Network error when using the Patch function" when saving data with a form
- Dataverse - How to work around the error "multiple levels of many-to-one relationship expansion aren't supported"
- Dataverse - The easiest way to modify Dataverse data outside of Dataverse and Power Apps
- Dataverse - How to use a checkbox control a set a yes/no field in Dataverse
- Dataverse - How to create Entity Relationship diagrams
- Dataverse - How you can more quickly bulk update data using the SQL language
- Dataverse - How to fix the bug in the 'Business Rules' editor that prevents numeric values from saving
- Dataverse - What are the benefits, and how to create formula columns
- Dataverse - How to sort the available choice items from a choice column
- Error - Unable to modify Dataverse tables with error message, Language id should not be null
- Dataverse - How to retrieve FetchXML or SQL for Dataverse views
- Data - Why is Dataverse so fast?
- Dataverse - How to create and use Dataverse views in Canvas Apps
- Dataverse - How to filter Dataverse choice columns
- Dataverse - How to switch to the classic designer when the option is not available
- Licensing - What are Dataverse Restricted tables?
- Media - How to save and work with microphone recordings
- Dataverse - how to access tables from another environment