Custom Command Buttons Using Power Fx in Power Apps

Ernest Cat and his thumbs love Power Fx
Ernest Cat does a lot of coding since he has thumbs. Here he is pondering the issue with a custom command button.

This was a challenge and searching the Internet didn’t give much information or information that was out of date.

Then I found Rajeev Pentayla’s blog post titled “Command Bar | Power Fx | Clone a record along with child records” from July 2023. YAY THANK YOU! This helped me get over my issue.

With the new command button creator using Power Fx, MOST of the functions that you’d use when creating buttons or actions in a Canvas app are available, but not all of them. Microsoft lists it all here:

Using Power Fx with commands

To paraphrase, you can’t use Collect(), ClearCollect() or Set() functions (among others). So, if you are using Patch() to create a new record, how the hell do you get the GUID to use it somewhere else?

READ ON!

The Need

I am creating a custom Power Apps model-driven app to track sales opportunities and quotes in a very simple way. When the user is looking at the Opportunity, the command bar has a button called “Create Quote.”

Custom command button called Create Quote using Power Fx.

Here’s what it looks like in the entire screen:

Model-driven app with custom Opportunity screen and command bar

The line items in the Opportunity drive lots of additional actions — estimated revenue calculations, sales pipelines and the same items become the line items when creating the Quote. This is basically replicating the way Dynamics 365 takes the data from the opportunity when creating a new quote.

The Problem

Using Patch() to create a new record is easy:

Patch( Quotes, Defaults(Quotes),
{
Topic: Self.Selected.Item.Name,
Opportunity: Self.Selected.Item,
Contact: Self.Selected.Item.Contact,
'Quote Date': Now(),
'Expiration Date': DateAdd(Now(), 30)
}
)

HOWEVER, you can’t use Set() to set a variable to the GUID of the newly created record. You receive an error.

The Solution

It’s the With() function to the rescue! Now the Patch() function sits inside a With() function:

With(
{
newQuote: Patch( Quotes, Defaults(Quotes),
{
Topic: Self.Selected.Item.Name,
Opportunity: Self.Selected.Item,
Contact: Self.Selected.Item.Contact,
'Quote Date': Now(),
'Expiration Date': DateAdd(Now(), 30)
}
)
},
ForAll(Filter('Opportunity Products', Opportunity.Opportunity = Self.Selected.Item.Opportunity), Patch('Quote Products', Defaults('Quote Products'), { Quote: newQuote, Product: Product, 'Write-In Product': 'Write-In Product', Quantity: Quantity, 'Unit Price': 'Unit Price' } )
)

Using the With() function and putting “newQuote:” in front of the Patch() function will save the record GUID that is created.

Still in the With() function as the second argument, I just have to use a ForAll() and Filter() IN THE WITH() to grab all of the products on the Opportunity showing on the screen and create them as products on the Quote.

Wow. WOW. WOWWWWWWWWWWWWWWWWWWWWWW. NEAT, RIGHT?!

A Bonus for the User

User experience is so incredibly important. The Notify() function puts a nice notification bar on the top of the screen. Let’s have it notify the user when the Quote is created, so they just aren’t sitting there waiting around forever wondering “What the hell is this doing?”

Notify("Your quote has been created.", NotificationType.Success);

I used the Success notification option so it showed in green.

Power Apps notification quote created successfully.

The command buttons in Power Fx are really awesome. I guess you just need to think a bit out of the box to accomplish certain ways of creating and manipulating records in Dataverse.

Thank you Rajeev for the guidance! You’re awesome!

2 thoughts on “Custom Command Buttons Using Power Fx in Power Apps”

    1. It was just what the intellisense was prompting me to do. The table is called “Opportunity” and the ID field in the table is also just called “Opportunity.”

Leave a Comment

Your email address will not be published. Required fields are marked *