Powering Your Way to Better Command Customization: Power Fx Snippets for Model-Driven Apps

Powering Your Way to Better Command Customization: Power FX Snippets for Model-Driven Apps

Introduction

Customizing commands in Model-driven Apps can greatly enhance user experience and productivity. With the introduction of Power Fx, a low-code programming language, customizing commands has become even easier and more accessible for non-developers.
Power Fx provides a wide range of functions and expressions that can be used to modify commands in Model-driven Apps. These functions include actions such as creating new records, updating existing records, and displaying error messages. In addition, Power Fx allows for the creation of custom logic and automation, making it a powerful tool for building complex workflows.
In this article, we will explore different Power Fx snippets that can be used to customize commands in Model-driven Apps. Whether you’re new to Power Fx or a seasoned developer, this article will provide you with useful tips and tricks for creating customized commands that meet your business needs. So, let’s dive in and explore the world of Power Fx!

Table of contents

Notification

Power Fx Dialogs and Banners provide a simple and effective way to communicate important information to users in Model-Driven Apps. Dialogs are pop-up windows that can display messages or prompt users for input, while Banners are notifications that appear at the top of the screen. By using these features, you can create custom alerts, notifications, and confirmations that improve user experience and streamline workflows. In this article, we’ll explore how to use Power Fx Dialogs and Banners to create customized alerts and notifications in your Model-Driven Apps.

How to create a Confirm Dialog and Notification Message

Confirm(
    "Do you want to perform this action?",
    {
        Title: "Confirm action",
        Subtitle: "Do you want to confirm this action?",
        ConfirmButton: "Yes",
        CancelButton: "No"
    }
);
Notify(
    "Your action was successful",
    NotificationType.Success
)

How to create an Abort Dialog and Notification MessagePar

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
)

Notification Banners

There are different types of notification banners:

The Notify function displays a banner message to the user at the top of the screen. The notification will remain until the user dismisses it or the timeout expires which defaults to 10 seconds.

An appropriate color and icon are used depending on the type of the message. The type is specified by the second argument to the function:

NotificationType ArgumentDescription
NotificationType.ErrorDisplays the message as an error.
NotificationType.Information (Default)Displays the message as informational.
NotificationType.SuccessDisplays the message as success.
NotificationType.WarningDisplays the message as a warning.
// 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);

Visibility Formulas in Model-driven App

Power Fx provides several types of visibility rules that can be used to control the display of commands in Model-Driven Apps. By using these rules, you can create custom interfaces that show or hide commands based on specific conditions or user permissions. This allows you to streamline workflows and improve user experience by only showing the commands that are relevant to each user or scenario. In this article, we’ll explore different types of visibility rules in Power Fx and how they can be used to customize commands in your Model-Driven Apps.

Display or Hide Commands

One useful type of visibility rule in Power Fx is the ability to show a command only if one or more records are selected in a grid view. This type of rule allows you to create more efficient workflows by only showing commands that are relevant to the selected records.

To create this type of visibility rule, you can use the Self.Selected.AllItems function in Power Fx. This function returns a table of records that are currently selected in a grid view. By checking the length of this table, you can determine whether one or more records are selected, and then use this information to show or hide the command.

For example, you could use the following formula in the “Visible” property of a command button to only show the command if at least one record is selected:

CountRows(Self.Selected.AllItems) > 0

Hide Commands during record creation

In Power Fx, you can use the FormMode property to determine whether a form is in Edit mode, New mode, or View mode. You can use this property to hide a command when creating a new record. For more information about the FormMode property check this link.

Self.Selected.State <> FormMode.New

Also you can use the If function and the IsBlank function to hide a command when creating a new record. The IsBlank function checks whether a value is blank or null, and the If function returns one of two values based on a logical expression.

To hide a command when creating a new record, you need to use If function that checks if the ‘Created On‘ column of the selected item is blank:

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

Show Commands based on record data

You can evaluate the value of a column to determine whether to hide a command or not.

Microsoft Dataverse have two different names to refer to the same table or column of data:

  • Logical name – A name that is guaranteed to be unique, doesn’t change after being created, usually doesn’t allow spaces or other special characters, and isn’t localized into different languages. As a result, the name can be cryptic. These names are used by professional developers. For example, paymenttermscode. This name may also be referred to as schema name or just name.
  • Display name – A name that is user-friendly and intended to be seen by end users. This name may not be unique, may change over time, may contain spaces and any Unicode character, and may be localized into different languages. Corresponding to the example above, the display name may be Payment Terms with space in between the words.
Self.Selected.Item.'Payment Terms' = 1

Using logical name:

Self.Selected.Item.paymenttermscode = 1

Creating, Updating record

Following examples show how to use the Patch function for updating, creating and cloning records with Power Fx.

Patch(
    Tasks,
    Defaults(Tasks),
    {
        Subject: "New task",
        Description: "New Description",
        Regarding: Self.Selected.Item
    }
)

Update record

Patch(
    Accounts,
    Self.Selected.Item,
    {'Account Name': Self.Selected.Item.'Account Name' & " Updated"}
)

Update multiple selected records

Patch(
    Accounts,
    ForAll(
        Self.Selected.AllItems,
        {
            Account: ThisRecord.Account,
            'Account Name': ThisRecord.'Account Name' & " Updated"
        }
    )
)

Updating Multiple Selected Records with User Confirmation Based on Specific Criteria

In this blog section, we will demonstrate how to ask a user for confirmation before updating records based on specific criteria using Power Fx in Power Apps. We will also provide feedback to the user if the criteria do not match. In this example, the criteria are “Status is inactive and Job Role equals Owner.” Although the criteria might not be the most realistic, it serves as a useful example for illustrating the concept.

Please note that all records must match the criteria to be able to update the records; otherwise, you will get a dialog with information on what to do!

If(
    And(
        Confirm(
            "Do you want to activate inactive Owner(s)?",
            {
                Title: "Confirm reactivate Owner(s)",
                ConfirmButton: "Yes",
                CancelButton: "No"
            }
        )
    ),
    If(
        CountRows(
            Filter(
                Self.Selected.AllItems,
                And(
                    ThisRecord.Status = 'Status (Contacts)'.Inactive,
                    ThisRecord.'Job Title' = "Owner"
                )
            )
        ) = 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 Owner, then try again.",
            {
                Title: "Invalid Status and Job Role",
                Subtitle: "Control Contact Status and Job Role",
                ConfirmButton: "OK",
                CancelButton: "Cancel"
            }
        )
    )
)

Clone record

Clone a single record

The following code will clone a single record. The Patch function will create a new record in the contact table with the same name:

With(
    {
        newContact: Patch(
            Contacts,
            Defaults(Contacts),
            {
                'First Name': Self.Selected.Item.'First Name',
                'Last Name': Self.Selected.Item.'Last Name',
                'Job Title': Self.Selected.Item.'Job Title',
                Email: Self.Selected.Item.Email
            }
        )
    },
    Navigate(newContact);
    Notify("Contact cloned");
    
);

Clone multiple selected records

The following code will clone multiple selected records in the Grid-view:

ForAll(
    Self.Selected.AllItems As contacts,
    Patch(
        Contacts,
        Defaults(Contacts),
        {
            'First Name': contacts.'First Name',
            'Last Name': contacts.'Last Name',
            'Job Title': contacts.'Job Title',
            Email: contacts.Email
        }
    )
)

Navigation

Navigate to the Default view of the table

Navigate (Contacts)

Navigate to specific view of the table

Navigate('Contacts (Views)'.'Inactive Contacts')

Conclusion

In conclusion, Power Fx provides a powerful toolset for customizing commands in Model-Driven Apps. From creating custom logic and automation to modifying existing commands, there are endless possibilities for enhancing user experience and productivity.

We hope that the examples we’ve provided in this article have helped to solve problems and provide inspiration for new features. However, it’s important to note that there are some limitations to the function for customizing commands. For more information on these limitations, please refer to the link provided in the resources section.

Overall, Power Fx and Model-Driven Apps provide a low-code solution for businesses looking to improve their workflows and productivity. By taking advantage of the features and functions provided by Power Fx, you can create customized commands that meet your specific business needs. So, why not give it a try and see what you can accomplish?

These links below gave me inspiration and use cases in this post:

Comments are closed.