Categories: Microsoft 365

Patch the Common Data Services with Power Apps

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.

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?

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

Share
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.

Recent Posts

Create new group alert using Power Automate

Do you want to know when a new group has been created with for example…

3 days ago

Delegation Warnings in Power Apps

We have all seen them, Delegation Warnings, but what is delegation and how do you…

3 days ago

1 Top tip: PowerPlatformLearn.com and master the Power Platform within no time

It can be difficult to get started with the Power Platform, blogs help, training helps,…

1 week ago

Compare 2 Excel files using Power Automate

This is such a common scenario. You receive Excel files on a regular basis and…

2 weeks ago

Rename SharePoint folders using Power Automate in 2 easy steps

Have you ever wanted to rename SharePoint folders with Power Automate? REST API call to…

3 weeks ago

Read dates from Excel with Power Automate

To read dates from Excel files can be tricky, as Excel only give you the…

4 weeks ago
%%footer%%