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 )
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 )
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);
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)
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
Show Command Based on Record Data
Visibility rule for showing Command for accounts with Credit Rating > 100K.
Self.Selected.Item.'Credit Rating' > 100000
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" } )
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"} )
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" } ) )
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" } ) ) )
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 )
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')
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) )
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' ) ) ) )
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.
Resource Links
The links below are resources that gave me inspiration and use cases in this post.
- Customize The Command Bar In A Power Apps Model-Driven App (matthewdevaney.com)
- https://xrmtricks.com/2022/04/10/model-driven-apps-power-fx-how-to-open-multiple-rows-in-new-tabs-using-power-fx-commands/
- Use Power Fx with commands – Power Apps | Microsoft Learn
- https://develop1.net/public/post/2021/07/25/RibbonWorkbench-vs-PowerFx
- https://jukkaniiranen.com/2021/10/clone-records-with-power-fx-custom-command-bar-button/