All about Odata actions in Dynamics 365 Finance And Operations
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
public class AutomationDataEntity extends common
{
//1st example
[SysODataActionAttribute("assignUserToCompany", false)]
public static void assignUserToCompany(NetworkAlias _networkAlias, DataAreaName _company)
{
UserInfo userInfo;
ttsbegin;
select firstonly forupdate userInfo
where userInfo.networkAlias == _networkAlias;
userInfo.company = _company;
userInfo.update();
ttscommit;
}
//2nd example
[SysODataActionAttribute("ReturnRental", true)]
public str ReturnRental()
{
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")]
public List GetColorsByAvailability(boolean onlyAvailableVehicles)
{
List returnList = new List(Types::Record);
// do something
return returnList;
}
}
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 or this blog for basic configurations in Dynamics 365 Finance & Operation, Azure to work with Postman.
- Consuming the first example
Specify Odata endpoint request with POST method into Postman application
[finopsURL]/data/AutomationDatas/Microsoft.Dynamics.DataEntities.assignUserToCompany
[finopsURL] = https://yourenvironment.cloudax.dynamics.com
Here is the Json file contains the parameters for assignUserToCompany
method
{
"_networkAlias":"Max.Nguyen@Microsoft.com",
"_company":"USMF"
}
Click Send and you will get your logic executed.
- Consuming the second example
Everything should be remain the same, you just need to change the method to ReturnRental
[finopsURL]/data/AutomationDatas/Microsoft.Dynamics.DataEntities.ReturnRental
Click Send and you will get an error
{
"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
[finopsURL]/data/AutomationDatas(dataAreaId='USMF',CustomerGroupId='BRIDGE')/Microsoft.Dynamics.DataEntities.ReturnRental
The result
{
"@odata.context": "https://[devaos].cloudax.dynamics.com/data/$metadata#Edm.String",
"value": "Rental was successfully returned. Thanks for your business"
}