Skip to content

Using Power Fx on Power Apps command bar

The functionality of model-driven Power Apps can be expanded in many ways. One way is to add custom commands to the command bar.

In the past, the implementation of commands required JavaScript skills. Now commands can also be implemented with Power Fx, familiar to (canvas) Power Apps makers.

Let’s go through what you can do with Power Fx commands. And what you can’t.

Example – Condos management

An example is the application for managing condos presented in my personal blog. We like to add a new command to the apartment (Huoneistot) form. We Open the app in edit mode and select “Edit command bar” from the navigation for the apartment.

Next, we select which command bar we are making changes to. We want to modify the commands visible on the main form, so we select that one.

Out of the box commands cannot be modified. If you want to get rid of them, you should read my previous article. This time a new command is added (New -> Command).

Select Power Fx and press Continue. A component library is automatically created to store Power Fx formulas.

The created component library appears in our solution. This library contains the Power Fx formulas of all command bars in the application.

Basics

We added a new button to the command bar. What can we now do with Power Fx?

The most important thing is that the information displayed on the form can be accessed (Self.Selected.Item). For example, the name of the record can be accessed like this:

Self.Selected.Item.Name

Other records related to the record can also be accessed. The renovation notices (Remontti-ilmoitukset) related to the apartment can be found as follows

Self.Selected.Item.'Remontti-ilmoitukset'

You can check whether the form has unsaved changes (Self.Selected.Unsaved) or whether a new record is being created (Self.Selected.State = FormMode.New).

Note! If AutoSave is on, the form is always saved before running PowerFx.

In addition to the actual functionality, Power Fx can be used to control the visibility of the command.

The button is displayed, for example, only when the current user has created the record.

Visible: Self.Selected.Item.'Created By'.'Azure AD Object ID' = User().EntraObjectId

Next, let’s look at a few practical examples.

Make a copy of record

Next, we create a command that makes a copy of the record presented on the form.

The command to run is the following (Huoneistot = Apartments).

IfError(
    Patch(Huoneistot, 
        Defaults(Huoneistot), 
            {Name:Self.Selected.Item.Name & " copy", 
            Taloyhtiö:Self.Selected.Item.Taloyhtiö, 
            Huoneistotyyppi: Self.Selected.Item.Huoneistotyyppi, 
            'Pinta-ala':Self.Selected.Item.'Pinta-ala', 
            Osakkeet:Self.Selected.Item.Osakkeet}
        );
    Notify("Copy created!",NotificationType.Success)   ,
    Notify("Error! Could'n make copy of the record",NotificationType.Error))

A copy can only be made from an existing record, so let’s show the button only on the editing form:

Visibile = Self.Selected.State = FormMode.New

Save and Publish and test by pressing Copy.

The copy was created successfully.

And there it is.

Navigating to the record

After copying, it would be nice to open the created record.

You can navigate to an empty form with the command Navigate(Defaults(_TableName_)). The editing form of a record is navigated by giving the record as a parameter to the Navigate command. Like Navigate(galItems.Selected).

Let’s modify the command from the previous example a bit.

IfError(
    Navigate(Patch(Huoneistot, 
        Defaults(Huoneistot), 
            {Name:Self.Selected.Item.Name & " copy", 
            Taloyhtiö:Self.Selected.Item.Taloyhtiö, 
            Huoneistotyyppi: Self.Selected.Item.Huoneistotyyppi, 
            'Pinta-ala':Self.Selected.Item.'Pinta-ala', 
            Osakkeet:Self.Selected.Item.Osakkeet}
        ));
    Notify("Copy created!",NotificationType.Success) ,
    Notify("Error! Could'n make copy of the record",NotificationType.Error))

Now, after copying, we open the created record.

You can also navigate to the view with Power Fx

  • Navigate(Accounts) – The default view of the Accounts table or
  • Navigate( ‘Accounts (Views)’.’My Active Accounts’)

Or to a custom page

  • Navigate(myPage)

Confirmation dialog

We like to have confirmation whether the user really wants to make a copy of this record. This can be done with the Confirm function.

Let’s modify the command a bit again.

If(Confirm("You are creating copy of this record. Are you sure?", {Title:"Copy record",
  ConfirmButton: "Yes", CancelButton: "No" }) = true,
  IfError(
    Navigate(Patch(Huoneistot, 
        Defaults(Huoneistot), 
            {Name:Self.Selected.Item.Name & " copy", 
            Taloyhtiö:Self.Selected.Item.Taloyhtiö, 
            Huoneistotyyppi: Self.Selected.Item.Huoneistotyyppi, 
            'Pinta-ala':Self.Selected.Item.'Pinta-ala', 
            Osakkeet:Self.Selected.Item.Osakkeet}
        ));
    Notify("Copy created!",NotificationType.Success) ,
    Notify("Error! Could'n make copy of the record",NotificationType.Error)
   )
)

Now the user is asked for confirmation before copying.

Created component library

Let’s open the component library corresponding to the command menu. The added buttons can be found there as components.

The library also contains data sources. By default, commands can only use the tables related to used command bars.

By adding Dataverse tables to the library, you can use them in Power Fx commands.

You can also add other data sources.

They do appear in the editor. But they don’t work.

You cannot use Power Fx to send an email or add rows to a SharePoint list, etc.

Starting a flow from the command bar

Is it finally possible to trigger a cloud flow directly from the command bar?

No. It is not possible to add flows to the component library.

What if you make a custom action, which you trigger with Power Fx from the command menu? This, in turn, triggers the flow.

It won’t work either. At least not yet.

Summary

If you are not yet familiar with Power Fx, I recommend getting familiar. Power Fx cannot be avoided when working with Power Platform.

But those commands and Power Fx… In short

  • Really convenient and easy to implement for certain needs
  • Excecution of the Power Fx from the command bar seems to be a little slow
  • Debugging is difficult
command barModel-driven appsPower AppsPower Fx

Leave a Reply

Your email address will not be published. Required fields are marked *