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:
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:
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...
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
Navigate(Success,ScreenTransition.Fade);
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 👍
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
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)
If(IsBlank(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
Navigate(Success,ScreenTransition.Fade);
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 |
|
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 |
|
Update Record:
|
Submitted information in Dynamics 365 and Navigate to Success Screen |
Existing Lead Record has been updated with updated Last Name and Job Title |
Please do not forget to share your feedback. It means a lot for me 😀
Cheers 👍
Would you like to do the same example with a lookup filed? (Like Account)
ReplyDeleteHi Moly, Please have a look my following article where I have used lookup field in Patch Function.
ReplyDeleteHope it helps.
https://arpitmscrmhunt.blogspot.com/2020/03/powerapps-how-to-use-variables-in.html
Thanks
It is truly a great and useful piece of information. how can i subscribe for a blog site? It is truly a great and useful piece of information.thanks for sharing these information
ReplyDeletec language interview questions
Thanks
ReplyDelete