PowerApps – Patch/Upsert record in Dynamics 365 without using Power Automate

Hello Folks,  Today I am going to share second post of my PowerGuide Mentorship Program Series.

Today we\’ll learn about Patch function in PowerApps.

One of the most common requirement while pushing the record in Dynamics 365 is to check whether a record is already present or not. If not, then Create a new record else Update the existing record with updated information.

This requirement can easily be achieved through Power Automate as well, however today we shall learn how this can be achieved through PowerApps itself using Patch Function.

Basically, Patch Function is useful to perform following operations in any data source:

  • Create (single or multiple) records
  • Update (single or multiple) records
  • Merge records

Let\’s understand Patch function first,

For Example- I have a \”Customer\” Table in my Database and It has three columns/fields named \”ID\”, \”First Name\”, \”Job Title\”  and has two records as shown below:

Now, I will use following two Patch function and Let\’s see what result I get:

Below syntax use to Create a record in Database Table:

Patch(Customers, Defaults(Customers), {ID: \”3\”}, {First Name: \”Henry\”}, {Job Title: \”Developer\”})

Below syntax use to Update the record in Customer Table where ID = 2:

Patch(Customers,First(Filter(Customers, ID = \”2\” ) ), { Job Title: \”Analyst\” } )

After the above two formulas have been evaluated, the data source ends with these values:

A new record has been inserted in Customer Table and the Job Title of second row has been updated from Architect to Analyst.
Now, Let\’s understand this, by taking a very simple example from Dynamics 365 perceptive.

Scenario : I want to create a Lead in Dynamics 365, If Lead with same emailaddress is already present in Dynamics then only Update it\’s details else Create a new Lead record.

Let\’s get started…

Step 1: Create a new Canvas App (ignore if you already have) > Connect to Dynamics 365 Data Source.> Connect to Lead Entity

Step 2: Insert a Blank Screen in order to add few Text Input and Button Controls or you can design the app as per your requirement

Step 3: Write following formula on Button Control (onSelect property)

LookUp(Leads,emailaddress1 = emailAddress.Text)),

//Create a new Lead Record with following data
Patch(Leads, Defaults(Leads), { firstname: firstName.Text },{ lastname: lastName.Text },{emailaddress1: emailAddress.Text},{jobtitle: jobTitle.Text},{mobilephone: mobilephone.Text}),

//If Lead is already present of entered emailaddress then Update the Lead Record with following data
Patch(Leads,First(Filter(Leads, emailaddress1 = emailAddress.Text ) ), { firstname: firstName.Text },{ lastname: lastName.Text },{emailaddress1: emailAddress.Text},{jobtitle: jobTitle.Text},{mobilephone: mobilephone.Text})

//Navigate to Success screen

Step 4: Insert a Success Screen in order to show Success Message, once the record gets Created/Updated in Dynamics 365

Step 4: Run the App

Create Record

No Lead Record Present in Dynamics 365
Run Canvas App in order to Create Lead in Dynamics 365

Fill the details and Click on Upsert
Record will get created in Dynamics 365 and Navigate to Success Screen
A new Lead has been created in Dynamics 365
Lead Details

Update Record:

Now I am updating Last Name and Job Title and Click on Upsert
Submitted information in Dynamics 365 and Navigate to Success Screen 

Existing Lead Record has been updated with updated Last Name and Job Title

Stay Tuned for PowerGuide – Tip3 – How to use Local/Global Variables in PowerApps

Please do not forget to share your feedback. It means a lot for me 😀

Cheers 👍

Published by arpitpowerguide

My name is Arpit Shrivastava, who is a Microsoft MVP in the Business Applications category. I am a Microsoft Dynamics 365 and Power Platform enthusiast person who is having a passion for researching and learning new things and acquiring immense knowledge. I am providing consistent help, support, and sharing my knowledge through various Social Media Channels along with my Personal Blog, Microsoft Community, conducting online training and attending various 365 Saturday Events worldwide and sharing the best Solutions to the readers helping them achieve their goals and objectives in Customer Relationship Space.

4 thoughts on “PowerApps – Patch/Upsert record in Dynamics 365 without using Power Automate

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: