Bulk Patch all items from a gallery

One of the functions it seems difficult to get some decent guidance on is the bulk patch. I want to submit everything from a gallery back into a database, changing one or more fields for all the records shown.

It occured to me that PowerApps simplest app is one where you look at a gallery, tap on the record to edit, and all the details are available in a form. There’s a submit button at the bottom, on which the OnSelect property is Submit(MyForm). There’s also a OnSuccess property for the form itself, where you set actions (like a Notify() message) which happen when the data is successfully saved.

The scenario

But what if you have a situation where you need to edit a number of records all at the same time. Perhaps you have an update to the population data, and you just need to change one field in 10-20 records. Do you really want to go into each record separately, type your update, click save, get taken back to the record list, and do the same for each?

No, of course not.

There is a way to bulk patch, which is used through the ForAll command. The ForAll command lets you run a command on every item in a collection of some sort , like this:

ForAll(Gallery1.AllItems,
    Patch('[dbo].[table]', First(Filter('[dbo].[table]', ID = ThisItem.ID)),
)

This, in theory, runs the Patch command on every line in Gallery1. Except I’ve found it really hard to get this to work, as have others. And as usual the official documentation looks (to me at least) like they’re jumping into a more complicated example from the off).

How to Bulk Patch a gallery in PowerApps

If, like me, you’ve been unable to get this to work (no error messages, but no records are updated, for example) then the key is twofold:

  • include the ID (or other unique) field in the gallery. You can even set its Visible property to false if you don’t want it showing.
  • Use UpdateIf instead of Patch

Then the code to bulk patch is like this:

ForAll(Gallery1.AllItems,
    UpdateIf(
        '[dbo].[table]', ID = Value(ID-Field.Text),
        {Property1: Property1TextInput.Text, Property2: Property2TextInput.Text}
    )
);

Note: You will need to use the Value() around the ID field text, like I have, if the ID is a numberical value in the database, as it often is.

Other options to bulk patch data

This might come in useful in a few different situations, but I am usually combining it with one of these two things:

Edit multiple fields on the gallery screen

My example lets you edit fields on the gallery screen without going to the edit form. Just replace the gallery field Labels (e.g. Title, Subtitle, Body) with TextInputs. In my example I’ve called them Property1TextInput and Property2TextInput.

Just make sure you include them in your ForAll statement.

Checkboxes

Instead of editing the same field on all gallery items, you might want to set a single control (e.g. a calendar / date picker) and then update some of the records in the gallery with that date. Add a checkbox to the gallery so that it shows up next to each record, and use this kind of code:

ForAll(Filter(Gallery1.AllItems, Checkbox1.Value = true),
    UpdateIf(
        '[dbo].[table]', ID = Value(ID-Field.Text),
        {StartDate: StartDatePicker.SelectedDate}
    )
);

Text in bold is the code I’ve added. In this scenario you only need one data picker on the whole screen.

Conclusion

I hope this presents a clear way to bulk update items in a gallery in PowerApps. The documentation for this feels lacking so far.

In summary: use UpdateIf on a ForAll loop. You can use this in conjunction with editable fields in the gallery, and/or checkboxes with single input controls outside the gallery itself.

And of course, this can also be used with Collections.

Image: Guardians, by Scott Cunningham, released under a Creative Commons license on Behance.

Adding barcodes to PowerApps at the list item level

You can create a barcode to associate with every item in a Sharepoint list. You can also associate a barcode with every item in a Sharepoint folder. But you do two different things to get each feature to work.

Adding a barcode to Sharepoint list items

Just a short one today, to share something that it took me too long to find. In fact, this is one of those times when the wrong way looks so right, that you assume either it’s broken or you’re an idiot. In my base, the latter was true.

I can also blame the typical lack of quality tutorials, so hopefully this short guide will make things clear.

  1. Open the Sharepoint list that you want to add a barcode to.
  2. Click the cog in the top right and select ‘List settings’.
  3. In the second column, click on ‘Information management policy settings’.
  4. This is the bit I got tangled in. Click on the ‘Item’ content type (and not ‘Folder’)
  5. On this final screen, tick the box labelled ‘Enable Barcodes’. Then click OK at the bottom.
Find list settings in the menu
Sharepoint list settings – enable barcodes
Select ‘Information management policy settings’

If you’re not taken to another screen here, click on OK again until you are. I think there’s something iffy there, and this won’t work unless your ‘OK’ takes hold.

Add a Barcode column to a Sharepoint list

You can now go back to your list and click on ‘Add column’ at the right end of the column headers. Click ‘Show/hide columns’ and, finally, tick the ‘Barcode’ box on the right side of your screen.

All new items will have a barcode, and I’ve found that editing a line will generate a barcode, even if you don’t change anything.

Image: Landscape. http://kansallisgalleria.fi/E39.Actor_3834BD59-6179-489D-AB48-8DF3F4C4FDE4. Finnish National Gallery. Public Domain

Dauerrotype landscape of Paris

Validate a field or form in PowerApps

A lot of people seem to be wondering how to validate a field in PowerApps. As usual, all the examples I’ve seen are either over-complicated or seem to be dodging the question about whether it’s even possible.

It’s possible! And so this post is going to be about collecting the ways in which a PowerApps field can be validated. It’s an incredibly important thing to check that your users are putting useful data into your apps. For one thing, if a user tries to put letters into a numerical field, the PowerApp is likely not to give useful feedback to the user by default.

You can try messing with error handling. Alternatively, nip that thing in the bud by validating fields while the user is right there.

Validate a field which should have numbers in

I’ll start with an easy one, which I’ve used myself already.

IsNumeric()

PowerApps has an IsNumeric() function which checks if some string of characters is only composed of numbers.

There are countless ways of using this to validate a numbers-only field, but one is to display an error message. The error message would likely be a Label, with its Visible property set to something like this:

If(IsNumeric(FieldToValidate.Text),false,true)

Which means that if the user types in something other than numbers into FieldToValidate the error message will appear. (To be specific: if the text in this Label is numeric, Visible is set to ‘false’. Otherwise, Visible is set to ‘true’)

You can go further, such as adding an ‘OR’ to the above If statement: if the user leaves the field blank, then the message is still hidden. We’d do that because, if the box is OK to be empty, we don’t want the error message to show.

You can adapt the IsNumeric() function to do other things. For example, if you have a Save button on your screen, you could use it on its DisplayMode property like this:

If(IsNumeric(FieldToValidate.Text),disabled,edit)

So the save button is disabled until the correct content is put in the box (or it’s empty). Pro tip: I’d still make sure an error message appeared as well. You should explain things to the user!

Image: Attributed: París Landscape. #agent_0b79a939-813b-040d-8c0d-dc168242cd1d. Colección Pau Maynés, Spain. CC BY.

Psalm manuscriupt drawing of a hedgehog

Rename a PowerApp: why is your app locked?

Microsoft really seem to have dropped the ball when you come to rename a PowerApp.

It’s a bizarre couple of hoops to jump through that don’t make sense, but if you want to rename a PowerApp it’s easy, even if it’s not intuitive.

The wrong (but obvious) way to rename a PowerApp

The problem with the obvious way to rename a PowerApp (File -> App settings -> Edit app name) is that you’ll end up with a message telling you your app is ‘locked’. And locked by your good self too!

Screenshot showing error message in PowerApps
Error message showing that this PowerApp is locked by me

The only way (that’s impossible at first glance)

Here’s the silly part. Why’s the app locked? Because you’re using it! But of course you’re using it, because you have to in order to get to the renaming screen… 🙁

The infuriating solution to this is that you must close the PowerApp in order to rename it. So: once you’ve got to the point of changing the name (I’m adding ‘test’ to the end of the existing name in the screenshot above), go to the PowerApp, which is still open in another tab or window.

Then click on ‘Close’ at the bottom of the File menu. It’s on the left.

Once you’ve closed the PowerApp, you might as well close the whole tab, as you won’t be needing it again.

Go back to the tab where you’ve been trying to rename the thing, and try again.

Success?!

I hope so. Then you just need to close the right hand sidebar by clicking on the X in the top right. Do that twice, because of course you should.

Now you’re at a screen where you’ll be able to re-open your PowerApp by clicking on ‘Edit’ next to its name.

Screenshot showing steps for renaming a PowerApp
Rename your app by following these two steps (nicely reversed to keep things as confusing as PowerApps itself)

So that’s it. To rename a PowerApp you need to open its Settings from within the open app. But you can’t rename an open app because it’s locked!

Just thank Microsoft that they made the settings open in a new tab so you could close your PowerApp easily!

Image: Hedgehog from BL Royal 2 B VII, f. 97v. The British Library. Public Domain.

Patch a NULL value

Patching a NULL value in PowerApps seems to be a fraught business. There are countless people wanting to patch NULL values, and yet Microsoft’s own documentation falls short.

However, it’s quite simple to patch a NULL value – i.e. insert a NULL value into a data source – with a couple of simple steps.

Insert a NULL value into PowerApps data source

The first thing to do is to go into the File menu, where you’ll find all the PowerApps’ settings.

Screenshot of PowerApps settings screen
PowerApps settings screen

Then click or tap on ‘Advanced settings’ (you’ll see it highlighted in the screenshot above, as my mouse is hovering over it).

Then, you’ll see the list of Advanced settings. Scroll down to ‘Formula-level error management’.

Screenshot of PowerApps Advanced settings screen
PowerApps Advanced settings
Screenshot of some of the advanced settings in a PowerApp
Scroll down to here. Enable the error management by clicking or tapping on the ‘switch’.

I don’t really know why you need to turn on error handling, because Microsoft’s explanation was a bit flaky and I’m still learning. But you need to do this first.

Do that Patch

Now when you add a Patch command to button or other element on the screen, you can set it to patch an empty string by using double quotes: “”. This has worked for me on columns in my SQL Server database whatever the format. I’ve even been able to patch NULL values into date columns, which seemed particularly tricky.

So now, when your user has mistakenly added some data in, but wants to clear a field, you can let them do that. For example, in my app a person might record when they finished a task. Then, if they realise that they ‘finished’ the wrong task, they’d need to keep the task record, but empty the ‘TaskFinished’ column.

You can also, if you’re using an If statement, simply leave the ‘false’ case blank, as in:

If(SomeVariable = 20, Patch(...) )

Notice there’s no ‘false’ code to run. We just don’t Patch anything – or rather we Patch ‘nothing’.

You can code a normal NULL Patch like this:

Patch('[dbo].[TaskList]', First(Filter('[dbo].[TaskList]',
   TaskNo = Dropdown1.Selected.Value)),
   {
      Notes: TextInput1.Text,
      DateFinished: ""
   }
)

That works for Numeric fields. For text fields you might be better off using the Blank() function to replace that “”. So it would look like this:

Patch('[dbo].[TaskList]', First(Filter('[dbo].[TaskList]',
   TaskNo = Dropdown1.Selected.Value)),
   {
      Notes: TextInput1.Text,
      DateFinished: Blank()
   }
)
Screenshot showing a dropdown being populated in PowerApps

The column “Value” of the table wasn’t found

I’ve found an odd error message in PowerApps which I can’t fathom. I can stop it happening, and so get the app working, but it looks like a bug.

When you populate a drop-down menu, you can hard code the items in it like this:

["First item","Second item","Third item"]

Alternatively, you can populate a dropdown using items in a column in your data source. Simply set the Items attribute of the Dropdown to the table name, e.g. ‘[dbo].[UserRoles]’ for a hypothetical tool where a user must state what kind they are from a predetermined list.

In the right hand sidebar, under ‘Advanced’, find the Data section and you’ll see something like this:

Screenshot showing a dropdown being populated in PowerApps
Populating a dropdown from a database table

Here the Value attribute shows all the columns in the UserRoles table. I have just two, one of which is a unique ID.

The column “Value” of the table wasn’t found

I kept getting this error message when I ran the app in Android and iOS:

The column "Value" of the table wasn't found

It took me a while to realise the link between this ‘Value’ and the Value attribute under Data!

Of course, Value isn’t a column in this table, nor will it likely be in any other table I use for a Dropdown (it’s a bit vague).

Changing the name of the column ‘UserRoles’ in my database to Value, it started showing the same message but complaining that UserRoles was now missing. Of course it was! And I certainly didn’t want to rename my column as ‘Value’ permanently.

What worked was to add an empty column named ‘Value’ in my database, allow nulls, and leave it like that. Well, it worked!

Not an ideal situation but I can’t see this as anything other than a bug. It always worked when I ran the app on the laptop I was building it on (a Surface Pro in case you’re interested in testing), but not the mobile apps.

If you’ve got any insight into this, please do let me know!

Screenshot of primary key being selected

The first argument of ‘Patch’ should be a collection

There’s an incredibly common – and infuriating – error message that I keep running into, so I’m making a note here so I don’t forget! If you see “The first argument of ‘Patch’ should be a collection”, and you’re stuck, I have a quick fix for you!

In PowerApps, a Patch command sends information to your database. So, for example, if your app is connected to a table called ‘[dbo].[FriendsList]’ and you want to change Monica’s phone number to 555-12345 (whether it’s blank at the moment or has an old number in there), you could add this code to the OnSelect function of a button:

Patch(‘[dbo].[FriendsList]’, First(Filter(‘[dbo].[FriendsList]’, Name = ‘Monica’)), {PhoneNo: “555-12345”})

In order, these commands:

  • Determine the ‘collection’ that a record is going into (in this case ‘[dbo].[FriendsList]’). A collection can be a database table, or a local collection built with the Collect() command, for example.
  • Find the First (assuming the only, but more of that another time) item in the ‘[dbo].[FriendsList]’ database table where the ‘Name’ column has ‘Monica’ in it
  • Fill in the ‘PhoneNo’ column of the table with “555-12345”

The first argument of ‘Patch’ should be a collection

So we know that the first argument of this Patch is a collection. So why the error?!

If you swear you have this correct (and I’ve sworn a lot over this when making PowerApps), but you’re still getting the message “The first argument of ‘Patch’ should be a collection” head to your database. For example, if it’s a SQL Server database, you can open SQL Server Management Studio (SSMS) and find your table. Right click on the table name in the Object Explorer and select ‘Design’.

You need to make sure your table has an ID column with a primary key. Right click on the column where the little black arrow is, and select ‘Set Primary Key’.

Screenshot of primary key being selected
Set your primary key by right clicking on the column to the left

Next, make the column the ‘Identity’ column. Do this by going to the bottom of the screen and finding ‘Identity Specification’ and clicking the little black arrow next to it. Then to the right of ”(Is Identity)’ click on ‘No’, to bring down the drop-down menu. Now you can click on ‘Yes’. This means your ID number will increase by one automatically each time you add a new record.

Screenshot of Identity Column being created
Turn the Identity Specification of your column to ‘Yes’

These two measures make sure each of your records can be distinguished from the next one, and your error message will be no more! Why that has anything to do with collections being missing is, for now, beyond me.

Damn Microsoft for its idiotic error messages!