How to Call Dataverse Custom APIs Directly in Power Fx: A Step-by-Step Guide

How to Call Dataverse Custom APIs Directly in Power Fx: A Step-by-Step Guide

Introduction

The Power Platform’s Dataverse is a powerful database that allows developers to store, manage, and analyze data for their apps. With the latest updates to Power Fx, developers can now call Dataverse Custom API directly in their formulas, streamlining their app development process and making it easier to manipulate data.

In this blog post, we’ll explore how to call Dataverse actions directly in Power Fx, including the syntax and functions you need to know. We’ll also discuss the benefits of this feature, such as improved efficiency, reduced complexity, and enhanced data processing capabilities. Whether you’re a beginner or an experienced developer, this post will provide valuable insights and tips for optimizing your app development process with Power Fx and Dataverse.

Estimated reading time: 6 minutes

Table of contents

Enable access to Microsoft Dataverse action

It’s important to note that the behavior described in this blog post is only available when the Access to Microsoft Dataverse actions feature is turned on. To enable this feature, you need to navigate to Settings -> Upcoming features -> Experimental in your Power Apps environment, and then turn on the Enable access to Microsoft Dataverse actions toggle.

Once you have enabled access to Microsoft Dataverse Actions, you can start using the pre-built actions in your Power Apps applications. These tools can help you streamline your app development process and create more robust and efficient solutions on the Power Platform.

Call Dataverse Custom APIs

Examples of using Dataverse Custom APIs in Power Fx

Custom APIs in Dataverse allow users to extend the platform’s functionality by creating their own endpoints that can be called from external systems or other parts of the platform. Power Fx is a low-code language that can be used to build custom logic and workflows within Dataverse. In this section, we will provide examples of how to use Dataverse Custom APIs in Power Fx, demonstrating how these two tools can be combined to create powerful solutions that meet specific business needs. We will start with a simple example that shows how to call a custom API using Power Fx.

We have created a Custom API that calculate the age of an individual based on their birthdate and a specified date (more details see the following article: Building Custom APIs in Dataverse).

Call Dataverse Custom APIs

Custom API Request Parameters

One of these Custom API request parameters is BirthDay and AgeAtDate. These parameters can be used to retrieve data related to a person’s birthdate and their age at a specific date:

Call Dataverse Custom APIs
Call Dataverse Custom APIs

Custom API Response Property

In addition to Custom API request parameters, Dataverse also offers Custom API response properties that can be used to retrieve specific data from the platform.

Call Dataverse Custom APIs

The Age Calculator App

Call Dataverse Custom APIs

Create three pairings of labels and Date Pickers and Text Inputs as shown in the image below and place a button at the bottom:

  • Birtdate
  • Age at Date
  • Result

We are using the Power Apps Modern Controls instead of standards. More information see this article: Matthew Devaney: The Complete Power Apps Modern Controls Guide

Call Dataverse Custom APIs

Add controls

After completing the basic design of our app, our next step is to incorporate code that calculates the user’s age.

Use this code in the OnSelect property of the pickerBirthDate control:

Set(
    varBirthDate,
    pickerBirthDate.Value
);

Use this code in the OnSelect property of the pickerAgeAtDate control:

Set(
    varAgeAtDate,
    pickerAgeAtDate.Value
);

Add Environment table

In order to trigger a Custom API, we must establish a connection to the Environment table of Dataverse. When the Power Fx Environment object is added to your application, you can access Dataverse actions by adding Environment to your formula and then dotting into the actions.

Call Dataverse Custom APIs

When the Power Fx Environment object is added to your application, you can access Dataverse actions by adding Environment to your formula and then dotting into the actions.

Once this connection has been established, the subsequent step involves calling Dataverse Custom APIs. Below is the code snippet to call a custom API/action when the user clicks the Calculate Age button (use this code in the OnSelect property of the Calculate Age button):

If(
    And(
        Not(IsBlank(varBirthDate)),
        Not(IsBlank(varAgeAtDate))
    ),
    Set(
        varResult,
        Environment.discover_AgeCalculation(
            {
                AgeAtDate: varAgeAtDate,
                Birthday: varBirthDate
            }
        )
    );
    UpdateContext({txtResult: varResult.Age});
    
);

Use this code in Value property of txtResult control:

varResult.Age

Finally the Age Calculator App completed.

Limitations

While calling Dataverse Custom APIs directly in Power Fx offers a lot of flexibility and power, there are some limitations that you should be aware of. Here are some of the most significant ones:

  • Limited error handling: When calling Custom APIs directly in Power Fx, error handling is primarily handled through the use of the OnError property on API function calls. This limits the ability to handle specific types of errors, and may require additional error handling logic in your code. See more Matthew Devaney: Power Apps Error Handling Guidelines
  • Limited debugging capabilities: Debugging Custom API calls in Power Fx can be challenging, as there is no built-in debugger or step-through functionality. This can make it difficult to identify and resolve issues in your code.
  • Limited data type support: Power Fx has a limited set of data types that can be used with Custom APIs, which may make it difficult to work with certain types of data, such as complex object.

Conclusion

Integrating your Custom APIs with Power Fx allows you to take full advantage of the power of Dataverse. By following the steps outlined in this guide and adopting best practices, you can streamline your data management and increase your productivity.

Resource Links

These links below gave me inspiration and use cases in this post: