Patch the Common Data Service

Using the patch Function to update records in the Common Data Services can be easy, but a lot depends on the field types used.

Reasons for this post

This week i was asked about the lookup field. However I didn’t want to create this post just for updating lookups. Hence I’m first going through a couple of basic field types first.

Introduction to the Patch function

To get started I created an entity called test entity in my Common Data Services.

Then I created a small test app and connected a gallery to the test entities data source.

Then I added a giant button to my app and labelled it Patch.

Patch Common Data Service fields within Power Apps.

Then to make the button create an item I used the following code.

Patch('test entities', Defaults('test entities'),{Name: "Test 1"})

For people who aren’t familiar with the Patch function. The first parameter is my datasource. Defaults(‘entity name’) gives you a new record in the entity. And then the 3rd and final bit final will need to contain the new values for the item.

Ok, that is easy for a simple text field. But how about all those other field types?

Patch the Common Data Services with Power Apps Microsoft Office 365 image 24

In the following examples I will call my fields after the field type used.

Update a Whole Number with Patch

To update a whole number field simply drop the speech marks or double quotes as we used earlier and the update will succeed.

Patch('test entities', Defaults('test entities'),{Name: "Test 2", 'Whole Number': 1})

Update a Date and Time with Patch

The Data and Time updates will require a date and time object. You could for example use the Now function to create the current date and time.

Patch('test entities', Defaults('test entities'),{Name: "Test 3", 'Whole Number': 1, 'Date and Time': Now()})

If you want to use another time that is formatting as text then you could use the DateTimeValue function as shown below:

Patch('test entities', Defaults('test entities'),{Name: "Test 4", 'Whole Number': 1, 'Date and Time': DateTimeValue("2020-12-15T3:30Z")})

Update a lookup with Patch

Now things are becoming slightly more complicated.

I’m going to expand on my previous commands again. And Now I’m adding a Lookup field.

Patch('test entities', Defaults('test entities'),
{Name: "Test 5",
'Whole Number': 1,
'Date and Time': DateTimeValue("2020-12-15T3:30Z"),
Lookup: First(Filter('test entities',Name = "Test 2"))
})

In the above example I’m updating the lookup with the first item that match the filter of Name = Test 2.

So all you need is get the record that you want the lookup to connect to and the Patch function will link things up.

What’s next?

Are you struggling with any of the other fields types, Let me know and I will add them to the above list of field types

By Pieter Veenstra

Business Applications and Office Apps & Services Microsoft MVP working as a Microsoft Productivity Principal Consultant at HybrIT Services. You can contact me using contact@veenstra.me.uk.

3 thoughts on “Patch the Common Data Services with Power Apps”
  1. Hi, Can you please provide Lookup and Optionset patch from canvas app to MS crm online please.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.