Clone records with Power Fx & custom command bar button - Jukka Niiranen blog (2024)

In my previous post I built my very first command bar button based on the modern commanding feature currently in preview. This time we’ll continue creating similar generic features often requested yet missing in the standard Model-driven Power Apps or Dynamics 365 Customer Engagement apps.

Repeated entry of data that already exists in the system should always be a task we aim to minimize in the business applications we build, to increase the likelihood of A) the data being recorded in the first place and B) reducing errors resulting from manual data entry. Sometimes there may be a perfectly valid business need why several nearly identical records should exist in the Dataverse database. In such scenarios, it’s common that users will at some point request a “clone this record” feature to be added into the app when they get tired of typing in the same values over and over again.

Such functionality has traditionally been implemented via a custom command bar button added with Ribbon Workbench, which then runs JavaScript & possibly a plugin. You could also perform the action with an on-demand XRM workflow. How about the modern command designer, could we do all this in Power Fx? Let’s find out!

Power Fx for cloning asset records

In our scenario we are working with a custom IT asset management app that has the following Dataverse tables and relationships:

Clone records with Power Fx & custom command bar button - Jukka Niiranen blog (1)

Let’s say that we often provide our employees the same phone model and we’d therefore want to make it faster to add these as asset records by being able to clone an existing asset as the starting point.

First we’ll launch the preview version of the Model-driven app designer and choose to edit the command bar for the asset table. This time we’ll target the main grid:

Clone records with Power Fx & custom command bar button - Jukka Niiranen blog (2)

In the command designer, let’s add a new command bar button called “Clone” and start thinking about the required configuration for it.

Clone records with Power Fx & custom command bar button - Jukka Niiranen blog (3)

We’re going to want to do a similar limitation of scope as in the previous article, by supporting only a single record at a time. Multi-record selection is beyond what we can currently process, so a visibility rule is needed for us to ensure that the button is clickable only when a single record from the table main grid has been selected. The way to achieve this is:

CountRows(Self.Selected.AllItems) = 1

Then for the button’s action part. The end result of what we need to create looks like this:

Clone records with Power Fx & custom command bar button - Jukka Niiranen blog (4)

Unlike in the previous article, we are now not updating the currently selected record but rather creating a brand new one. We’re able to use the exact same kind of Patch formula that any Canvas app would leverage when adding Dataverse records and not using the built-in create forms.

Our assets table has fields than can be directly copied from the earlier asset record. These include text fields like description and lookup fields like vendor and product. The syntax for the formula is very straightforward for all these: just use “Self.Selected.Item.[YourFieldNameHere]” to populate the field on the new record to be patched.

We can of course manipulate the field values for our cloned record via simple tweaks in the Power Fx formula. Let’s set the asset’s purchased date value to Today() and add a “Copy of” prefix to the asset name field. This ease of modifying the field values based on evolving business requirements is one of the key benefits from having the logic defined with a low-code programming language that a citizen developer can use with confidence.

After the patch, we can call up the notification bar in the Model-driven app to display a confirmation message from the clone operation. That’s pretty much all there’s to it, so here’s our formula:

Patch(Assets, Defaults(Assets),{Name: "Copy of " & Self.Selected.Item.Name, Vendor:Self.Selected.Item.Vendor, Product:Self.Selected.Item.Product, 'Purchase Date':Today(), 'Purchased From':Self.Selected.Item.'Purchased From', 'Purchase Cost':Self.Selected.Item.'Purchase Cost', 'Warranty End Date':Self.Selected.Item.'Warranty End Date', Description:Self.Selected.Item.Description});Notify("A copy of the selected asset, " & Self.Selected.Item.Name & ", has been created. Please open the record and update the necessary fields.")

Here’s the resulting end user experience for cloning a record from the main grid:

Clone records with Power Fx & custom command bar button - Jukka Niiranen blog (5)

Not too bad at all for a relatively simple formula, added as a command bar button via a visual editor available in the platform.

Limitations of Power Fx commands today

To be honest, the first cloning scenario that I actually set out to build was targeting the main form. Adding the custom button here is just as simple as the main grid. Actually, since you don’t even have to think about the visibility rules and multiple selection, it would be the logical place for any new Power Fx user to start customizing their Model-driven app command bar. However, I didn’t get exactly the result I hoped for there.

The general cloning logic works on the form, too, but there is an unfortunate user experience issue. Can you spot it?

Clone records with Power Fx & custom command bar button - Jukka Niiranen blog (6)

In the above image, we have already clicked on the Clone button. We have a notification bar as a confirmation. However, we are still sitting on the original record form, not the cloned version of it. If a user would for a moment think that “ok here’s the new cloned record, let’s modify a few fields” the business data would be messed up.

Could we take the user to the form of the newly created record instead? In theory, yes. In practice, nothing I’ve tried works as of today. The Navigate function that should be supported for custom command buttons doesn’t want to co-exist with any other function in the same action of a custom button. It doesn’t want to accept the newly patched record as the target to be navigated to. Even if we’d navigate to the view for all active assets after creating a new record, the UI will still return to the original record after everything in our button’s action formula has been run.

With this current limitation in mind, I started to explore if we’d have any other method to take the user away from the original record form. Navigating to a dedicated “Cloned Assets” view was out of the question, due to the aforementioned feature/bug. Using the Model-driven app notification bar to display and action button with a link seems to not be an option since the documentation says the Notify function available in the command bar is actually that from the Canvas world. This doesn’t have the same action property as addGlobalNotification on the XRM side that can show a clickable link.

One thing I really hoped would work is the new in-app notifications for Model-driven apps. This is such an excellent scenario for leveraging Power Fx to construct a rich message aimed at the specific user with targeted actions and helpful information. It could work the way shown below:

Clone records with Power Fx & custom command bar button - Jukka Niiranen blog (7)

But in the case of the command bar, it doesn’t work today. You see, the above examples of in-app notifications have been triggered from a dummy Canvas app I used for testing the notification feature in general. I took the Power Fx code from the awesome blog post by Diana Birkelbach that describes how to send in-app notifications from Custom Pages.

The reason why you can’t make this work (at least to my knowledge) inside the command bar is the lack of support for collections. You see, in order to construct the JSON data needed for adding an action button into the notification, you would in practice need to feed a collection into the JSON function in your Power Fx. This will result in a “formula within canvas component manipulates collection” warning inside the command designer and this part of your formula will silently fail upon clicking the button.

Update 2021-10-10: Scott Durow gave me a great tip over on LinkedIn, reminding that the in-app notification body text actually supports Markdown syntax. This way you could include a dynamic link that points to the newly created record clone, without having to insert the more complex strings needed for displaying proper action buttons in the notification:

Clone records with Power Fx & custom command bar button - Jukka Niiranen blog (8)

Presumably all of these things will one day work, which will give you plenty of options to design a great user experience for your custom commands. Just be cautious when building on top of the preview feature of modern commanding.

Cloning parent + child records

The above example was a very simple case of record cloning, as we only needed to replicate data from one table. In real world scenarios we often run into a requirement where also the child records in related tables would need to be cloned together with the parent record. Can we achieve this with Power Fx?

Let’s use the same IT asset management data model but move one level higher and build a custom command button to clone a product record and its related asset records. Meaning, both A + B in this picture:

Clone records with Power Fx & custom command bar button - Jukka Niiranen blog (9)

So, we’d like to first create a new product record, which is the exact same Power Fx formula pattern as before. After that’s done, we’d then need to have one or more record create events, depending on how many assets (if any) there are under the original product.

Let’s use the ForAll function to do a patch for each of the existing assets. To identify these records, we can reference them from under the current record with the familiar “dot notation” to travel down the Dataverse table relationships: Self.Selected.Item.Assets.

Then what we need to do is to ensure we link these newly patched assets to the new product record we created in the first step. To achieve this, I’ve added a ClonedProduct variable that is set to the result of the first patch. We can then use this ClonedProduct object when setting values for the N asset records we create inside the ForAll loop.

Clone records with Power Fx & custom command bar button - Jukka Niiranen blog (10)

Our formula for the “Clone With Assets” button on the product main grid is as follows:

Set(ClonedProduct, Patch(Products, Defaults(Products),{Name: "Copy of " & Self.Selected.Item.Name, 'Product Category': Self.Selected.Item.'Product Category', Vendor: Self.Selected.Item.Vendor, Description: Self.Selected.Item.Description}));ForAll(Self.Selected.Item.Assets, Patch(Assets, Defaults(Assets),{Name: "Copy of " & Name, Vendor:ClonedProduct.Vendor, Product:ClonedProduct, 'Purchase Date':Today(), 'Purchased From':'Purchased From', 'Purchase Cost':'Purchase Cost', 'Warranty End Date':'Warranty End Date', Description:Description}));Notify("Product " & Self.Selected.Item.Name & " and its assets have been cloned. Please open the records and update the necessary fields.")

When selecting a product from the grid and clicking on the button, here’s what the end user will see:

Clone records with Power Fx & custom command bar button - Jukka Niiranen blog (11)

This is already a much more powerful example of a low-code feature that can save time when users aren’t required to re-create similar sets of records over & over again. You will have the flexibility of easily adjusting the specific fields, field values and defaults used in your record cloning action.

Sure, you need to understand the concepts used in the Power Fx formula first. I’d still say the barrier for app makers with no software development background is still lower here compared to building the same thing with client-side JavaScript or server-side C# plugins.

Clone records with Power Fx & custom command bar button - Jukka Niiranen blog (2024)

FAQs

In which two places can you use Power Fx when you customize a command bar command? ›

You can use Power Fx for both actions (what happens when the command button is selected) as well as visibility (logic to control when the button is visible). Power Fx isn't supported in classic commands. Dataverse is currently the only data source supported with commands in model-driven apps.

Can you use Power Fx in a model-driven app? ›

Add OnSelect Expression to create a task

Now we can add the Power Fx expression to create the new task related to the account record. Open the command bar editor again using Edit command bar (preview) from inside the model-driven app editor.

What is clone record? ›

Create a record by making a copy of a similar record. When you clone certain records, you can also clone related records.

What is the clone method in recordset? ›

Use the Clone method to create multiple, duplicate Recordset objects. Each Recordset can have its own current record. Using Clone by itself doesn't change the data in the objects or in their underlying structures.

What is Power Fx similar to? ›

Microsoft Power Fx language borrows heavily from the Excel formula language. We seek to take advantage of Excel knowledge and experience from the many makers who also use Excel. Types, operators, and function semantics are as close to Excel as possible.

How to use Power Fx in Excel? ›

To define visibility logic, select the command. Then select Visibility on the right command properties pane and choose Show on condition from formula. You can select Visible on the left of the formula bar then write a Power Fx expression using the formula bar.

What language is Power Apps FX? ›

Power Fx is a low-code programming language available across the Power Platform, including in Power Apps cards. Cards can calculate values, perform tasks, and respond to user input using formulas expressed in Power Fx. Power Fx expressions can also update variables and data sources.

How do you merge records in model-driven app? ›

Select the duplicate rows from the view page in your model-driven app, and then select Merge in the command bar at the top of the page. In the Merge Contacts dialog box, select the primary row (the one you want to keep) and then select any columns in the new row that you want to merge into the primary row.

References

Top Articles
11 casas colombianas que usan el ladrillo a la vista
Getting Paid by Prop Firms Isn’t Easy
Stayton Craigslist
Rentals for rent in Maastricht
Hk Jockey Club Result
The Fappening Blgo
Fone Tech Cleveland Ms
Why are you the best candidate for financial advisor position?
Unveiling the Charm of Rio Vista, California
Santa Maria Cars Craigslist
Walmart Tire Service Hours
Craigslist Com Humboldt
Finger Lakes 1 Police Beat
888-490-1703
Thompson Center Thunderhawk Parts
73 87 Chevy Truck Air Conditioning Wiring Diagram
Browse | Obituaries | Enid News and Eagle
Fisher-Cheney Funeral Home Obituaries
Lovindabooty
Sky Park Stl Coupon
Clarksville.craigslist
Movies123.Pick
Best Fantime Accounts
Ohio Road Construction Map
Alloyed Trident Spear
Violetken 5E
Affordable Prom Dresses for Women | Fashion Nova
My Les Paul Forum
2621 Lord Baltimore Drive
A Closer Look at Ot Megan Age: From TikTok Star to Media Sensation
Www.playgd.mobi Wallet
Boise Craigslist Cars And Trucks - By Owner
Mesmerized Nyt Crossword
Cvs Newr.me
General Kearny Inn Motel & Event Center
Mastering the basics: A comprehensive guide to cybersecurity 101 for the digital age
Depths Charm Calamity
Mcoc Black Panther
Gtl Visit Me Alameda
Viaggio Apostolico a Singapore: Santa Messa nello Stadio Nazionale presso il “Singapore Sports Hub” (12 settembre 2024)
Sam's Auto Arena
Hyundai Elantra - modele, dane, silniki, testy
Ev Gallery
Erica Mena Net Worth Forbes
Aces Fmcna Login
Eliza Hay, MBA on LinkedIn: I’m happy to share that I’ve started a new position as Regional Director… | 36 comments
Lucio Volleyball Code
Best Asian Bb Cream For Oily Skin
"Wordle" #1,176 answer, clues and hints for Saturday, September 7 game
Live TV | Halifax | CBC Gem
Katia Uriarte Husband
Ravenna Greatsword Arcane Odyssey
Latest Posts
Article information

Author: Ouida Strosin DO

Last Updated:

Views: 6766

Rating: 4.6 / 5 (56 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Ouida Strosin DO

Birthday: 1995-04-27

Address: Suite 927 930 Kilback Radial, Candidaville, TN 87795

Phone: +8561498978366

Job: Legacy Manufacturing Specialist

Hobby: Singing, Mountain biking, Water sports, Water sports, Taxidermy, Polo, Pet

Introduction: My name is Ouida Strosin DO, I am a precious, combative, spotless, modern, spotless, beautiful, precious person who loves writing and wants to share my knowledge and understanding with you.