MSBA-271 · Lesson 4 of 12 · 19 min
Power Fx in commands
▶ Video coming soon A video version of this lesson is on the way.
Maribeth arrived at the fourth session with a question that had bothered her since the last one. "If I can write Power Fx in a canvas app, why can I not just write the Mark In Progress button myself?" Signe's answer was that she could, and that the point of this session was to show her where Power Fx in commands is the right tool, where it is not, and what a developer should check when a maker builds a command. Wiremu, who had never written Power Fx, sat next to Maribeth and learned it from her.
Power Fx as a command language
Power Fx is the low-code formula language of Power Platform, the same language used in canvas apps, custom pages, Dataverse formula columns and parts of Copilot Studio. In modern commands it is used in two places: the action formula, which runs when the button is selected, and the visibility formula, which decides whether the button shows. Formulas are written in the command designer and stored in the app's command component library, the same kind of component library a canvas app uses, with Dataverse tables added as data sources.
The command knows what it is acting on through Self.Selected. On a form, Self.Selected.Item is the open row. On a grid or subgrid, Self.Selected.Item is the first selected row and Self.Selected.AllItems is the table of every selected row. That one idea covers most of what command formulas need.
// Action: Mark In Progress, main grid
ForAll(
Self.Selected.AllItems,
Patch('Work Orders', ThisRecord, { Status: 'Status (Work Orders)'.'In Progress' })
);
Notify(CountRows(Self.Selected.AllItems) & " job(s) marked in progress", NotificationType.Success)
// Visibility: show when at least one row is selected and all selected rows are New or Assigned
CountRows(Self.Selected.AllItems) > 0 &&
IsEmpty(
Filter(Self.Selected.AllItems,
Not(Status in ['Status (Work Orders)'.New, 'Status (Work Orders)'.Assigned]))
)Maribeth wrote most of that formula herself. Wiremu was struck by how much the language did for him: Patch with ThisRecord updated each selected row without any ID handling, Notify showed a toast in the app, and the choice values were referred to by name rather than by number. "In JavaScript I would have had to look up 100000002," he said.
What Power Fx in commands can and cannot do
Power Fx in commands is deliberately narrower than Power Fx in a canvas app, and Microsoft documents its limits. Not every Power Fx function is supported in commands, and the formula bar's IntelliSense may suggest functions that are not supported without showing an error, so a formula can look correct in the designer and fail at run time. Dataverse is the only supported data source for command formulas; a command that must reach SharePoint, SQL or another connector should open a custom page, which can use connectors, and do the work there.
| Limit | What it means in practice |
|---|---|
| Not every Power Fx function is supported | Test every command in the running app, not just in the designer |
| IntelliSense can suggest unsupported functions | A missing error does not prove a function works in commands |
| Dataverse is the only data source | Use a custom page for connectors and external data |
| One command component library per app | Duplicate libraries cause an error when the designer opens |
| Commands belong to one app | Copy and paste them into other apps; they cannot be shared |
| Not all classic visibility rules exist in Power Fx | Some commands still need classic rules |
| Power Fx commands do not run in the Dynamics 365 app for Outlook or in a model-driven app hosted in a portal | Use JavaScript or another design if those hosts matter |
| Column metadata changes may not show in formulas | Reopen the component library and refresh the data source |
Signe added one practical point not in the list: deleting a command component library is awkward. It cannot simply be removed; Microsoft's documented route is to delete the app, or to delete the app element record behind the library in Dataverse first. Lumenvale's rule was therefore never to experiment with Power Fx commands in an app that mattered. Experiments happened in a copy of the app in a developer environment.
Visibility formulas
A visibility formula returns true or false. It is evaluated when the command bar is drawn and again when the selection changes, so it should be quick: no heavy lookups, no counting large tables. The best visibility formulas test the selected row or rows and perhaps the user's role. A formula that looks up related rows for every selection change slows the grid for everyone who opens it.
// Main form: show Request Quote only for external jobs that are not closed
Self.Selected.Item.Category = 'Category (Work Orders)'.External &&
Self.Selected.Item.Status <> 'Status (Work Orders)'.ClosedVisibility is not security. A hidden button stops nobody who can reach the row another way, as lesson 3 explained. Visibility formulas make the interface tidy and save people from choosing commands that do not apply; privileges decide what people may actually do. When a visibility formula checked the user's role, Signe insisted that the underlying security role made the same restriction.
Writing, navigating and confirming
Command actions can write data with Patch, Collect and Remove, show messages with Notify, and move the user with Navigate to another row or screen. Where supported, the Confirm function asks the user to confirm before continuing, which is gentler than a JavaScript dialog for routine checks. Power Fx can also call Dataverse custom APIs and actions through the Environment object in apps where that feature is available; check that it works in your app's command component library before depending on it, because command formulas support fewer features than canvas apps. Wiremu used this to let Kwabena's planned Export to Research button call the export custom API from Power Fx, but kept the JavaScript version in his notes in case the API's parameters proved awkward.
// Action: close the job, with confirmation
If(
Confirm("Close this job? Technicians will no longer see it.", { ConfirmButton: "Close job" }),
Patch('Work Orders', Self.Selected.Item, { Status: 'Status (Work Orders)'.Closed });
Notify("Job closed", NotificationType.Success)
)The Close Job command shows how layers fit together. The visibility formula hides the button when the job is already closed; the confirmation stops an accidental click; the client OnSave script from lesson 2 warns about missing photos on the form; and the server plug-in from MSBA-270 refuses the change if there is still no photo, whatever route the change took. Each layer has a different job, and none replaces the plug-in.
Power Fx or JavaScript?
Microsoft supports both for modern commands, and the exam expects candidates to choose sensibly. Power Fx is quicker to write and read, refers to tables, columns and choices by name, needs no web resource and can be maintained by makers. JavaScript reaches the whole Client API: it can save the form first, open custom pages and web resources with full options, use Xrm.WebApi with any query, call any custom API and reuse form library code.
| Choose Power Fx when | Choose JavaScript when |
|---|---|
| The command updates or creates Dataverse rows simply | The command needs the form saved, refreshed or inspected first |
| A maker will maintain it | It shares logic with form scripts |
| Visibility depends only on the selected rows | Visibility needs classic rules or data outside the formula's reach |
| The host is the model-driven app in a browser or the mobile app | The command must also work where Power Fx commands are not supported |
| External data is not needed | Complex navigation, files or device features are needed |
Worked problem
Reviewing a maker's command
A Ridgeview maker built a Send Reminder command on the registrar's transcript request grid. Its action uses Office 365 Outlook to email the student for each selected request, and its visibility formula looks up the student's other requests to count holds. The command shows no errors in the designer but fails in the app, and the grid has become slow. What should the review say?
Show solution
- The action uses a connector, but Dataverse is the only supported data source for command formulas. The email must be sent another way: a cloud flow triggered by the command updating a Send Reminder column, or a custom page that uses the connector.
- The visibility formula queries related rows on every selection change, which slows the grid. It should test only the selected rows, for example their status, and leave hold checks to the flow or the page.
- The lack of designer errors proves nothing; IntelliSense can accept unsupported functions. The fix must be tested in the running app.
Answer: Move the email out of the command into a flow or custom page, simplify the visibility formula to the selected rows, and test in the app. The maker can still own the command; it just needs a supported design.
Patterns for common command formulas
Most command formulas at Lumenvale followed a handful of patterns, and Signe kept them in the facilities repository's documentation so that makers could copy them. The first updates every selected row. It uses ForAll over the selection, Patch for each row, and Notify to tell the user what happened.
ForAll(
Self.Selected.AllItems,
Patch('Work Orders', ThisRecord, { Priority: 'Priority (Work Orders)'.High })
);
Notify(CountRows(Self.Selected.AllItems) & " work orders set to high priority", NotificationType.Success)The second pattern updates the row open on a form, using Self.Selected.Item, and the third asks first. Confirm returns true or false, so an If around the change runs it only when the user agrees. A fourth pattern controls visibility, covered above: a formula that returns true only when the command makes sense, such as when at least one row is selected and every selected row is still open.
If(
Confirm("Close this work order? Technicians will no longer see it.", { ConfirmButton: "Close", CancelButton: "Keep open" }),
Patch('Work Orders', Self.Selected.Item, { Status: 'Status (Work Orders)'.Closed });
Notify("Work order closed", NotificationType.Success)
)Each pattern has a limit worth remembering. Patching many rows in ForAll sends one request per row, which is fine for the handful a dispatcher selects but wrong for hundreds; for large updates, a custom API or a flow is the right tool. Formulas run as the user, so a user without write permission on the table sees an error, which is correct behaviour but should be anticipated with a visibility rule. And formulas cannot reach data sources other than Dataverse, so anything involving another system belongs in a flow or a custom API called from the command.
Testing and supporting command formulas
Command formulas are easy to write and easy to break, so they need the same testing as scripts. Wiremu tested each command with every role that could see it, with none, one and many rows selected, and on both the form and the grid where it appeared. He opened the app with Monitor running to see each formula's calls and errors, which showed one formula calling Dataverse once for every row on screen in its visibility rule. Rewriting the rule to check only the selected rows fixed a slow grid.
Ownership mattered as much as testing. Each command's formula lives in the app's component library, which is part of the solution and moves with it through pipelines. Maribeth's registrar commands and Delphine's facilities commands each had a named owner and a reviewer, and changes went through the same pull request and release as any other change. Rupert's rule was simple: no command reached Lakeshore Ops that had not been tested in Facilities Test by someone other than its author.
| Test | Why |
|---|---|
| Each role that can see the command | Permissions change both visibility and whether a Patch succeeds |
| No rows, one row and many rows selected | Visibility and ForAll behave differently in each case |
| Form and grid | Self.Selected.Item and Self.Selected.AllItems differ by location |
| With Monitor running | Shows calls, timing and errors from formulas |
Common mistakes
MistakeUsing connectors other than Dataverse in command formulas.
InsteadOpen a custom page or trigger a flow for external data.
MistakeTrusting the designer's lack of errors.
InsteadTest every Power Fx command in the published app; unsupported functions may not be flagged.
MistakeHeavy lookups in visibility formulas.
InsteadTest only the selected rows; visibility runs often.
MistakeTreating a visibility formula as a permission.
InsteadSet privileges in security roles; visibility only tidies the interface.
MistakeExperimenting with command libraries in a production app.
InsteadExperiment in a developer copy; command component libraries are hard to delete.
By the end of the session Maribeth had built two commands in a copy of Transcript Office in Wiremu Dev, and Wiremu had written his first Power Fx formula without help. Rupert asked who would own Maribeth's commands. "I will," she said, "and Wiremu reviews them." That became the pattern for maker-built commands in Lumenvale's solutions.
Summary and key terms
- Modern commands can use Power Fx for actions and visibility, stored in one command component library per app.
- Self.Selected.Item is the open or first selected row; Self.Selected.AllItems is every selected row on a grid.
- Dataverse is the only data source for command formulas; use custom pages or flows for other data.
- Not all functions are supported, IntelliSense can mislead, and Power Fx commands do not run in the Dynamics 365 app for Outlook or portal-hosted apps.
- Visibility formulas should be quick and are not security.
- Choose Power Fx for simple Dataverse actions that makers maintain; choose JavaScript when the command needs the full Client API.
- Power Fx
- The low-code formula language used across Power Platform.
- Action formula
- The Power Fx formula that runs when a command is selected.
- Visibility formula
- The Power Fx formula that decides whether a command is shown.
- Self.Selected.AllItems
- In a grid command, the table of all selected rows.
- Notify
- A Power Fx function that shows a message banner in the app.
- Confirm
- A Power Fx function that asks the user to confirm before an action continues, where supported.
Practice problems
Work each problem on paper first, then reveal the answer to check.
A command must read data from SharePoint. Can its Power Fx formula use the SharePoint connector?
Show answer
No; Dataverse is the only supported data source for command formulas. Use a custom page or a flow.
What does Self.Selected.Item refer to on a grid?
Show answer
The first selected row; use Self.Selected.AllItems for every selected row.
Why should visibility formulas avoid looking up related rows?
Show answer
They run whenever the command bar is drawn or the selection changes, so heavy queries slow the app.
Lesson quiz · 5 questions
Score 60% or more to complete this lesson.
Create a free account to save your progress, take quizzes and earn a certificate.