Logo of Power FX Cheat Sheet for Commands in Model-Driven Apps

Power FX Cheat Sheet for Commands in Model-Driven Apps

Introduction

This post covers different Power FX snippets for customizing Commands in Model-Driven Apps. Power FX is the new approach for creating logic and visibility rules on Commands in Model-Driven Apps instead of using JavaScript and RibbonWorkbench.

The examples are meant to be a look-up guide or a cheat sheet to have a place with frequently used snippets. This is no complete cheat sheet list, but some snippets that came to mind at the time of writing and with the experience I got with PowerFX so far. Hope you find it useful.

Table of Content

  • Prerequisite
  • Setting up a Trial Environment
  • Notifications in a Model-Driven App
    • Create a Success Dialog and Notification Message
    • Create an Abort Dialog and Notification Message
    • Notification Banners
  • Visibility Formulas in Model-Driven App
    • Show or Hide Commands in a List View
    • Hide a Command Commands During Record Creation
    • Show Commands Based on Record Data
  • Patch Function (Update And Create)
    • Create a Related Record on the Current Record
    • Update Current Record
    • Update Multiple Selected Records in a Grid
    • Update Multiple Selected Records with Dialog and With Certain Criteria
  • Navigation
    • Navigate to the Default View of the Table
    • Navigate to Specific System View of the Table
    • Open Multiple Records in a New Tab
    • Open Selected Records in Brønnøysundregisteret
  • Conclusion
  • Resource Links

Prerequisites

  • A trial or environment for customizing apps
  • Familiar with Model-Driven Apps
  • Familiar with Ribbon Customization in Model-Driven Apps
  • Familiar with Power FX

Setting up a Trial Environment

To create a Dynamics 365 CE trial environment you can follow the steps in the post, How to create Microsoft Dynamics 365 Customer Engagement Trial instance.

Notifications in a Model-Driven App

This section shows different notifications such as Dialogs and Banners.

Create an Abort Dialog and Notification Message

OnSelect event for opening an abort dialog with a notification message.

Confirm(
    "Failed to perfom action. Please try again or contact administrator.",
    {
        Title: "Invalid Status",
        Subtitle: "Check the status and try again.",
        ConfirmButton: "OK",
        CancelButton: "Cancel"
    }
);
Notify(
    "Your action was aborted or cancelled",
    NotificationType.Warning
)
Gif showing an abort dialog with a warning notification message.
Gif showing an abort dialog with a warning notification message.

Create a Success Dialog and Notification Message

OnSelect event for opening a dialog for asking the user if they want to take action and showing them a notification message when they confirm.

Confirm(
    "Do you want to perform this action?",
    {
        Title: "Conform action",
        Subtitle: "Do you want to confirm this action?",
        ConfirmButton: "Yes",
        CancelButton: "No"
    }
);
Notify(
    "Your action was successful",
    NotificationType.Success
)
Gif showing an abort dialog with a successful notification message.
Gif showing an abort dialog with a successful notification message.

Notification Banners

Different types of notification banners.

// Success
Notify("This is a Success banner", NotificationType.Success)

// Information
Notify("This is an Information banner", NotificationType.Information);

// Warning
Notify("This is a Warning banner", NotificationType.Warning);

// Error
Notify("This is an Error banner", NotificationType.Error);
Image of a success banner
Image of an information banner
Image of the warning banner
Image of an error banner

Visibility Formulas in Model-Driven App

This part shows different types of Visibility rules for displaying Commands in Model-Driven Apps.

Show or Hide Command in a List View

Visibility rule for showing the Command only if one or more records are selected in a grid view.

CountRows(Self.Selected.AllItems) > 0

// Or

!IsEmpty(Self.Selected.AllItems)
Showing and hiding Commands based on selected records in a list view.
Showing and hiding Commands based on selected records in a list view.

Hide a Command During Record Creation

Visibility rule for hiding a Command when creating a new record.

If(
    !IsBlank(Self.Selected.Item.'Created On'),
    true,
    false
)

// Or

Self.Selected.State <> FormMode.New
Gif showing the visibility rule for hiding a Command when creating a new record.
Gif showing the visibility rule for hiding a Command when creating a new record.

Show Command Based on Record Data

Visibility rule for showing Command for accounts with Credit Rating > 100K.

Self.Selected.Item.'Credit Rating' > 100000
Gif showing Command visibility based on a field value.
Gif showing Command visibility based on a field value.

Patch Function (Update And Create)

This part shows examples of how to use the Patch function for updating and creating records with PowerFX.

Create a Related Record on the Current Record

Create a task related to the current account record.

Patch(
    Tasks,
    Defaults(Tasks),
    {Regarding: Self.Selected.Item},
    {
        Subject: "Subject of the related record",
        Description: "Description of the related record"
    }
)
Gif showing the command for creating a task related to the current account record.
Gif showing the command for creating a task related to the current account record.

Update Current Record

An example of how to update a field on the current record.

Patch(
    Accounts,
    Self.Selected.Item,
    {'Account Name': Self.Selected.Item.'Account Name' & " Updated"}
)
Gif shows an example of how to update a field with a PowerFX command on the current record.
Gif shows an example of how to update a field with a PowerFX command on the current record.

Update Multiple Selected Records in a Grid

An example of how to update a field on selected records.

Patch(
    Accounts,
    ForAll(
        Self.Selected.AllItems,
        {
            Account: ThisRecord.Account,
            'Account Name': ThisRecord.'Account Name' & " Updated"
        }
    )
)
Gif that shows an example of updating selected rows in a grid.
Gif that shows an example of updating selected rows in a grid.

Update Multiple Selected Records with Dialog and With Certain Criteria

This example shows how to ask the user for confirmation and update records based on specific criteria and give the user feedback if the criteria don’t match. In this example, the criteria are “Status is inactive and Job Role equals CTO”. In conclusion, the criteria might not be the most realistic, on the other side it shows the concept.

Note that all records must match the criteria to be able to update the records otherwise, you will get the dialog with information on what to do.

If(
    And(
        Confirm(
            "Do you want to activate inactive CTO(s)?",
            {
                Title: "Confirm reactivate CTO(s)",
                ConfirmButton: "Yes",
                CancelButton: "No"
            }
        )
    ),
    If(
        CountRows(
            Filter(
                Self.Selected.AllItems,
                And(
                    ThisRecord.Status = 'Status (Contacts)'.Inactive,
                    ThisRecord.'Job Title' = "CTO"
                )
            )
        ) = CountRows(Self.Selected.AllItems),
        Patch(
            Contacts,
            ForAll(
                Self.Selected.AllItems,
                {
                    Contact: ThisRecord.Contact,
                    Status: 'Status (Contacts)'.Active
                }
            )
        );
        Notify(
            "Contacts updated successfully",
            NotificationType.Success
        ),
        Confirm(
            "Contact cannot be activated. Make sure Contact Status is deactivated and Job role is CTO, then try again.",
            {
                Title: "Invalid Status and Job Role",
                Subtitle: "Control Contact Status and Job Role",
                ConfirmButton: "OK",
                CancelButton: "Cancel"
            }
        )
    )
)
Gif showing how to update selected records with specific criteria.
Gif showing how to update selected records with specific criteria.

Navigation

Navigate to the Default View of the Table

Example of how to use Power FX to navigate to the user’s default view.

Navigate( Contacts )
Gif showing navigation to default view using Power FX.
Gif showing navigation to default view using Power FX.

Navigate to Specific System View of the Table

Example of how to use Power FX to navigate to a specified view.

Navigate('Contacts (Views)'.'All Contacts')
Gif showing navigation to a specified view using Power FX.
Gif showing navigation to a specified view using Power FX.

Open Multiple Records in a New Tab

This example opens the selected records in a new tab.

ForAll(
    Self.Selected.AllItems,
    Launch("/main.aspx?pagetype=entityrecord&etn=contact&id=" & ThisRecord.Contact)
)
GIF showing how to open selected records in new tabs.
GIF showing how to open selected records in new tabs.

Open Selected Records in Brønnøysundregisteret

For Norwegian readers an example of how to open single or multiple records in Brønnøysundregisteret from a list view.

ForAll(
    Self.Selected.AllItems,
    With(
        ThisRecord,
        Launch(
            Concatenate(
                "https://w2.brreg.no/enhet/sok/detalj.jsp?orgnr=",
                'Account Number'
            )
        )
    )
)
GIF showing how to open Accounts in the Norwegian register for businesses.
GIF showing how to open Accounts in the Norwegian register for businesses.

Conclusion

There are many cool things you can do with Power FX and Commands in Model-Driven Apps and hopefully, the examples helped solve problems and gives inspiration for possible new features.

To inform you, there are some limitations to the function for customizing Commands that can be found in the link below.

https://learn.microsoft.com/en-us/power-apps/maker/model-driven-apps/commanding-use-powerfx#functions-not-supported

Resource Links

The links below are resources that gave me inspiration and use cases in this post.


For More Content See the Latest Posts