ODATA actions in Data Entities provide a way to inject behaviors into the data model, or expose custom business logic from Dynamics 365 Finance & Operations.
You can add actions by adding a method to the data entity and then decorating the method with specific attributes [SysODataActionAttribute]
I use this Odata actions mostly in automation job like after refreshing data from PROD to UAT, we need to enable users, assign company to users, enable batches …
Or simply consume it in Power Automate.
1. Create an action to OData entity
You can create a new entity following this standard docs
or you can duplicate any standard entity. I created AutomationDataEntity. Right-click the enitity, select View code and add the code
publicclassAutomationDataEntityextendscommon{//1st example [SysODataActionAttribute("assignUserToCompany", false)]publicstaticvoidassignUserToCompany(NetworkAlias_networkAlias,DataAreaName_company){UserInfouserInfo;ttsbegin;selectfirstonlyforupdateuserInfowhereuserInfo.networkAlias==_networkAlias;userInfo.company=_company;userInfo.update();ttscommit;}//2nd example [SysODataActionAttribute("ReturnRental", true)]publicstrReturnRental(){return"Rental was successfully returned. Thanks for your business";}//following 3rd example of an OData action takes in a parameter and returns a list [SysODataActionAttribute("GetColors", true),
SysODataCollectionAttribute("return", Types::Record, "CarColor")]publicListGetColorsByAvailability(booleanonlyAvailableVehicles){ListreturnList=newList(Types::Record);// do somethingreturnreturnList;}}
In this example, the SysODataActionAttribute class decorates the assginUserToCompany method that is exposed as an action. The first argument of the attribute is the publicly exposed name of the action, and the second argument indicates whether this action need an entity instance or not. If you set the second argument to false, the method has to be static.
You might need reset IIS service to update Odata endpoint.
2. Test Entity Odata actions with Postman and Power Automate
2.1. With Postman
Please follow this document for basic configurations in Dynamics 365 Finance & Operation, Azure to work with Postman.
2.1.1. Let’s use the first example
Specify Odata endpoint request with POST method into Postman application
{"Message":"No HTTP resource was found that matches the request URI 'https://[devaos].cloudax.dynamics.com/data/AutomationDatas/Microsoft.Dynamics.DataEntities.ReturnRental'. No route data was found for this request."}
The reason is that you set the second argument to true, that means you need an instance for AutomationDatas entity before you can use ReturnRental method.
My entity created based on CustGroup table, so to get an instance I need DataAreaId and CustGroupID. The correct endpoint should be
{"@odata.context":"https://[devaos].cloudax.dynamics.com/data/$metadata#Edm.String","value":"Rental was successfully returned. Thanks for your business"}
2.2. With Power Automate
2.2.1. With the first example
Create a simple Power Automate with Dynamics 365 Finance & Operations connector, to consume Odata actions we use “Execute action” action following
2.2.2. With the second example
when specify action in Execute action, Dynamics 365 Finance & Operations connector understand that this needs an instance
3. More
In Odata actions, you can return a list
1
2
3
4
5
6
7
8
[SysODataActionAttribute("GetColors", true),
SysODataCollectionAttribute("return", Types::Record, "CarColor")]publicListGetColorsByAvailability(booleanonlyAvailableVehicles){ListreturnList=newList(Types::Record);// do somethingreturnreturnList;}
The following example of an OData action takes in list a parameter.
In those examples,the SysODataCollectionAttribute class enables OData to expose strongly typed collections from X++. This class takes in three parameters:
The name of the parameter that is a list (Use return for the return value of the method.).
The X++ type of the members of this list.
The public name of the OData resource that is contained in the collection.
You can find actions that are defined on data entities by searching for the SysODataActionAttribute attribute in metadatasearch.
If you want to check how many Odata actions available for an entity, you can go here and search for an entity.
At the time I’m writing this post, Odata actions does not support COC extension (A 18, P 42). So if you write some thing like this, it won’t work.
1
2
3
4
5
6
7
8
9
10
[ExtensionOf(dataentityviewstr(CustCustomerGroupEntity))]finalclassCustCustomerGroupEntity_KA_Extension{ [SysODataActionAttribute("ReturnRental", false)]publicstaticstrReturnRental(){return"Rental was successfully returned. Thanks for your business";}}