Mastering CRUD Operations in PowerApps Using Patch, LookUp, and Remove


In this post, let's learn all the CRUD operations using Patch in PowerApps. 

Before we start, let's understand the Patch function. The Patch function has 3 inputs as given below:






1 - This is the data source. It could be a table from Dataverse or SharePoint or a collection.

2 - By using Defaults with the data source, it notified to create a new record.

3 - You specify the name of the column and its value.



Use Patch to create a record:

There are 3 ways to create a record:

1 - Using hardcoded values.

As seen in above example,

Patch(MyDataSource, Defaults(MyDataSource), {Col1: "Some value", Col2: "Some more value", Col3: "Alot of values"});

2- Using variables

UpdateContext({TextVariable: "Monday"});

Patch(MyDataSource, Defaults(MyDataSource), {Col1: TextVariable});

3 - Reference value of a control

Patch(MyDataSource, Defaults(MyDataSource), {Col1: TextInput.Text});


Use Remove and RemoveIf to remove one or all records:

Remove() function is used to delete specific record. You can use LookUp function to specify which record needs to be deleted.

Remove(MyDataSource, LookUp(MyDataSource, Col1_ID: 1));

In case you have a delete button in the Gallery and you wish a particular row:

Remove(MyDataSource, ThisItem);

RemoveIf() function is used to delete records based on a condition. It will delete all records if it satisfy the condition.

RemoveIf(MyDataSource, Col1_ID > 10);

RemoveIf(MyDataSource, Col2_Status = "Completed");


In case, you wish to delete everything from a table without a condition:

RemoveIf(MyDataSource, true);

Caution: Do NOT use this in PRODUCTION

The reason this works is RemoveIf() checks every record in the data source to see if the equation equals true. In this case, the equation is set to true, so every record is deleted.


Use LookUp or ThisItem to Read a record:

Under the text property of a Label control, you can use (to get the value of a specific column for that row)

LookUp(MyDataSource, Col1_ID = 1).name


Or you can use (if you are in a Gallery control)

ThisItem.name


Use LookUp to Update a record:

Similar to create, there are 3+1 ways to create a record:

1 - Using hardcoded values.

Patch(MyDataSource, LookUp(MyDataSource, ID = 1), {Col1: "Some value", Col2: "Some more value", Col3: "Alot of values"});

2- Using variables

UpdateContext({TextVariable: "Monday"});

Patch(MyDataSource, LookUp(MyDataSource, ID = 1), {Col1: TextVariable});

3 - Reference value of a control

Patch(MyDataSource, LookUp(MyDataSource, ID = 1), {Col1: TextInput.Text});

4 - Using ThisItem

Patch(MyDataSource, ThisItem, {Col1: TextInput.Text});




Comments

Popular

How To Create MySQL Table with SequelizeJS

How To Read CSV Files in Java as Maven Project

How To Create A Hibernate Project in Maven