You are currently viewing How PowerApps check if a record exists or add new record?
powerapps check if a record exists

How PowerApps check if a record exists or add new record?

  • Post author:
  • Post category:Power Apps
  • Reading time:8 mins read

In this blog post, we will learn how PowerApps check if a record exists and update it or add new record if it doesn’t exist.

PowerApps check if a record exists

In PowerApps, you can check if a record exists in a data source by using the LookUp function that is used to search for records based on specific criteria and determine if a record meeting those criteria exists, If such a record exists, the LookUp function will return it, and you can use IsBlank function to determine if the record exist or not,!IsBlank will return true if the LookUp find the record it search for, indicating that the record exists.

Syntax of LookUp

LookUp(dataSource, formula)

formula: This is the condition or criteria that you want to apply to search for the record.

This is How PowerApps check if a record exists

PowerApps check if record exists in SharePoint list

Set(varexist, !IsBlank(LookUp(Employee,Employee.Email=ComboBox1.Selected.Mail)))

The formula using !IsBlank to check if the record exist or not, if the record exist in the SharePoint list, it will return true as it is not blank record, else it will return false.

In this example, I searched for an employee that already exist in the DataSource the LookUp function is used to search for a record in Employee SharePoint list where the Email of the “Employee” field equals the Email selected in the ComboBox, If such a record exists, the LookUp function will return it, and !IsBlank will return true, indicating that the record exists. You can use this result in conditional statements to perform actions based on whether the record exists or not.

PowerApps check if a record exists

this is how PowerApps check if record exists in SharePoint list.

PowerApps update existing record in SharePoint list

In PowerApps, you can update an existing record in a SharePoint list by using the Patch function that allows you to modify specific fields or values within an existing record without overwriting the entire record.

Example:

Let’s assume you want to update the “Title” field of a record with the returned ID of the selected person that we used in the previous example to a new value.

The basic syntax of the Patch function is as follows:

Patch(DataSource, Defaults(DataSource), {ID: RecordID, Field1: NewValue1, Field2: NewValue2, ...})

So the formula will be as shown bellow.

Set(varrecord, LookUp(Employee,Employee.Email=ComboBox1.Selected.Mail));
If(!IsBlank(varrecord),Patch(Employee,varrecord,{Title:"New employee"}))

First, I set a variable with the lookup record that we search for in the SharePoint list in order not to use the lookup function more than time, this will reduce the performance.

Next, use the IF function to check is the record not blanck and update the record.

PowerApps update existing record in SharePoint list

PowerApps patch create new record if not exists

In PowerApps, you can use the Patch function to create a new record in a data source. The Patch function can be used to both update and create records in a data source. When you specify a unique identifier that doesn’t exist in the data source, the function will create a new record with the provided data using the Defaults(DataSource) function that retrieves the default record structure from the data source.

Example:

In this example we search for a record if exist it will update it and if the record does not exist it will create new record.

Set(varrecord, LookUp(Employee,Employee.Email=ComboBox1.Selected.Email));
If(!IsBlank(varrecord),Patch(Employee,varrecord,{Title:"Old employee"}),Patch(Employee,Defaults(Employee),{Title:"New Regisiterd employee",Employee:ComboBox1.Selected}))

The Patch function will create a new record in your data source if it doesn’t find a record with the specified identifier .

Conclusion

The Patch function offers a flexible way to create new records or Update existing record dynamically in your data source when specific conditions or triggers are met.

See Also
Join us

Heba Kamal

Microsoft MVP, MCT, Technical Speaker, Blogger, and Microsoft 365 and Power Platform Consultant.