A few weeks ago, preparing the session for the Global Power Platform Bootcamp 2021, I realized that the series of posts that I had written about Power Automate & MSDyn365FO was incomplete…, I hadn’t analyzed one of the options availables on the Dynamics 365 Finance and Operations apps connector: Execute action
Execute action
From execute action option we have the possibility of executing X++ code that we expose through the Dynamics 365 F&O data entities. Exposing OData actions through these is pretty straightforward, let’s see an example.
I’ve created a test data entity that contains the CustGroup table as data source. Now, we are going to create a method to get the description of the customer group given its code. As you can imagine, it is a fairly simple code.
1 2 3 4 5 |
[SysODataAction("CustGroupName", false)] public static Description getDescription(CustGroupId _custGroupId) { return CustGroup::find(_custGroupId).Name; } |
Once the method is created, all we need to expose it as an OData endpoint is to decorate the method with the SysODataAction attribute, indicating the name we want to give to the action, and if this action is an instance method or not.
Now, we just have to consume the action from a Power Automate flow, which is quite simple.
As always, we start by indicating the Dynamics 365 Finance and Operations instance on which we want to execute the action, which will allow us to later choose the specific action we want to execute.
Once we have selected both the instance and the action (Entity-Action), Power Automate obtains the parameters we need to be able to execute it using the MetadataService service. As you can see in the screenshot, the parameter appears with the exact name that we have written in the method (_custGroupId).
Finally, we just have to indicate the Customer Group and run the flow to see if it works correctly.
After being executed, we can see how the flow obtains the name or description of the customer group that we have indicated in the parameter.
We are going to consult the IIS log to verify that, as we can imagine, what it is doing is a POST call on the CustGroupName action (name that we indicate in the SysODataAction attribute) of the AXZTests entity.
1 |
21-04-11 17:33:40 POST /data/AXZTests/Microsoft.Dynamics.DataEntities.CustGroupName ja.tomas@axazure.com - 200 |
Limitations
As is has happened in other actions, the code is also executed on the company associated to the user in the user options, but remember that this time we are executing X++ code directly, so we have greater flexibility . If we need to execute these actions in different companies, we could, for example, add the company code to the parameters of the action, and play with the changeCompany within it. Problem solved! 😎