Calling Function and Action in OData V4
In OData V2, there is only on concept called 'Function' or 'Function Import'. But in V4, this is divided into two categories by different purposes for which they are expected to serve. According to the standard OData specification, functions MUST NOT have side-effects, which means it should not make any changes to current data. On the contrary, actions can be used to modify data. This note tries to document ways of calling them. And both are called operation.
Unbound Function
Unbound function can be used to do something based on all data of the connected entity, such as CalculateAveragePrice, CalculateStockAvailability etc..
Sample URL for calling it:
https://abc.com/sap/opu/odata4/sap/zui_v4_test/srvd/sap/zsd_xc_test/0001/FunctionDemo/com.sap.gateway.srvd.zsd_xc_test.v0001.calculateTotal()
The trick point here is to add the namespace before the actual function name.
Unbound Action
A typical example of an unbound action is creating a new instance of the connected entity. Normally we don't need such action since new object creating is covered by POST calling. But it is useful for a scenario, which we want to create a new object by providing a dialog for inputting some data before creating a new object.
https://abc.com/sap/opu/odata4/sap/zui_v4_test/srvd/sap/zsd_xc_test/0001/FunctionDemo/com.sap.gateway.srvd.zsd_xc_test.v0001.createWithParameters(guid=a201faef-19d2-cedd-1234-55d87b3345f1, val1=1, val2='2')
Bound Function
A bound function is more common that an unbound. A example like CalculateDiscount for a sales order. No changes would be made, but just calculate discount info per the current sales data.
https://abc.com/sap/opu/odata4/sap/zui_v4_test/srvd/sap/zsd_xc_test/0001/FunctionDemo(PurchaseDocument='123')/com.sap.gateway.srvd.zsd_xc_test.v0001.calculateDiscount()
For a bound function, the key name and its value are required to identify an instance.
Bound Action
A bound action is also very easy to understand. Do some changes against SELECTED record, such as Copy, Relase etc..
https://abc.com/sap/opu/odata4/sap/zui_v4_test/srvd/sap/zsd_xc_test/0001/FunctionDemo(PurchaseDocument='123')/com.sap.gateway.srvd.zsd_xc_test.v0001.copy(onlyRoot=true)
For a bound action, the key name and its value are required to identify an instance.
In case you are creating SAPUI5 application, I think you don't need to care about how OData operations are called since ODataModel does the dirty work.
Comments
Post a Comment