Loading...

Error Handling Update/Patch in CommandBar Power Fx buttons

Error Handling Update/Patch in CommandBar Power Fx buttons

As you know, I'm 'super excited'* about the new Power Fx low-code Command Bar buttons (First Look Video) (Ribbon Workbench compared to Power Fx) - especially the ease at which you can update multiple records in a grid. To allow the user to select a number of records on a grid, and then perform an operation on each, in turn, would have taken plenty of pro-code TypeScript/JavaScript before but now can be turned into a simple ForAll expression. 

* That one's for you @laskewitz 😊

The one thing that always gets left out - Error Handling!

It's easy to assume that every operation will always succeed - but my motto is always "if it can fail, it will fail"!

Just because we are writing low-code, doesn't mean we can ignore 'alternative flows'. With this in mind, let's add some error handling to our updates. 

Step 1 - Add your Grid Button

Using the Command Bar editor, add a button to an entity grid and use the formula similar to :

With({updated:ForAll(Self.Selected.AllItems As ThisRecord, 
    If(Text(ThisRecord.'Credit Hold')="No", 
        Patch(Accounts, ThisRecord, { 'Credit Hold':'Credit Hold (Accounts)'.Yes });"Updated"),
        "Skipped"
    )
)},
With({
    updatedCount: CountRows(Filter(updated,Value="Updated")),
    skippedCount: CountRows(Filter(updated,Value="Skipped"))
    },
    Notify("Updated " & Text(updatedCount) & " record(s) [" & Text(skippedCount) & " skipped ]");
));

In this example code, we are updating the selected Account records and marking them as on 'Credit Hold' - but only if they are not already on hold. 

Imagine if we had some logic that ran inside a plugin when updating accounts that performed checks and threw an error if the checks failed. This code would silently fail and the user would not know what had happened. To work around this we can use the IsError function and update the code accordingly:

With({updated:ForAll(Self.Selected.AllItems As ThisRecord, 
    If(Text(ThisRecord.'Credit Hold')="No", 
        If(IsError(Patch(Accounts, ThisRecord, { 'Credit Hold':'Credit Hold (Accounts)'.Yes }))=true,"Error","Updated"),
        "Skipped"
    )
)},
With({
    updatedCount: CountRows(Filter(updated,Value="Updated")),
    errorCount: CountRows(Filter(updated,Value="Error")),
    skippedCount: CountRows(Filter(updated,Value="Skipped"))
    },
    Notify("Updated " & Text(updatedCount) & " record(s) [ " & Text(errorCount) & " error(s) "  & Text(skippedCount) & " skipped ]");
));

Save and Publish your Command Button. This creates/updates a component library stored in the solution that contains your model-driven app.

Step 2- Open the component library and enable 'Formula-level error management'

Since we are using the IsError function we need to enable this feature inside the component library. This along with the IfError function can be used to check for errors when performing Patch operations.

Inside your solution, edit the command bar Component Library (it will end with _DefaultCommandLibrary), then select Settings and toggle on the Formula-level error management feature:

 

 Make sure you save, then publish your component library.

Step 3 - Re-open the Command Bar Editor and publish

After editing the Component library, it seems to be necessary to always re-publish inside the command bar editor (You will need to make a small change to make the editor enable the Save and publish button). You will also need to refresh/reload your model-driven app to ensure the new command bar button is picked up.

Done! You should now have error handling in your command bar buttons 😊

Hope this helps,

@ScottDurow

P.S. SUMMIT NA 2021 is next week! I can't believe it! I'll be speaking about custom pages and Power FX command buttons - if you are able, come and check out my session on Next Gen Commanding.

 

Published on:

Learn more
Develop 1 - Dynamics 365 Architecture Services
Develop 1 - Dynamics 365 Architecture Services

Share post:

Related posts

Show/Hide Buttons Based on Entity Permissions Using Power FX

Recently, we received a client requirement to show a button to users who have the Create permission for a specific entity. In the traditional ...

7 days ago

[Beginner’s Guide] PowerFx | Lookup Function with ‘ReductionFormula’ Property

Did you know that when you use the Lookup function, it retrieves the matching record along with All Columns? In the screenshot below, I used t...

21 days ago
Stay up to date with latest Microsoft Dynamics 365 and Power Platform news!
* Yes, I agree to the privacy policy