Technical Articles
How to implement Bound functions and Unbound functions in SAP CAPM
Introduction
- In this blog, we will look at how to implement functions in Cloud Application Programming Model and invoke the functions in local host.
Custom Functions
- Within service definitions, you can additionally specify actions and functions.
- In SAP CAP, We have two types of functions (Bound Functions and Unbound Functions)
Bound Functions
Actions and functions can also be bound to individual entities of a service, enclosed in an additional actions block.
Unbound Functions
The notion of actions and functions in CDS adopt actions and functions on service-level.
Let’s implement Bound functions in our application.
Providing Service Implementations
In Node.js, the easiest way to provide implementations of functions for services is thru equally named .js files placed next to a service definition’s .cds file.
Bound Function in .cds file
In the above example, we have defined the Bound function in .cds file which is bounded to individual entity employeeMaster.
The next most important step is function definition in .js file.
Function definition in .js file
In the above example, we will not cover the complex scenario however, Let’s implement the basic logic now.
req.query is a basic Query with where clause.
{ SELECT: {
from: {
ref: [
{
id: 'CatalogService.employeeMaster',
where: [ {ref:['ID']}, '=', {val:1} ]
}
]
}
}}
Output of Bound function
How to invoke Bound Functions
/employeeMaster/1/CatalogService.boundedRead() // employeeMaster is Entity and CatalogService.boundedRead() is the function invoking.
Let’s implement Unbound functions in our application.
Unbound Function in .cds file
In the above example, we have defined the Unbound function in .cds file which is defined on service-level.
Function Definition for Unbound function.
Unbound Function Definition in .js file
Output of Unbound function
How to invoke Unbound Functions
/unboundedRead() // You can directly invoke Unbound functions
I hope everyone enjoyed this blog, Please don’t forget to put comment and stay safe at home.
Regards
Nikhil Puri
Hi Nikhil Puri
nice summary of the actions topic on CAP! I just struggled with figuring out how to call bound actions via HTTP last week and it took some time to figure out that you need to include the namespace of the action.
Kind regards,
Sebastian
Thanks fo rthe blog. Quick question. How to call this bound action programatically from the node js service?
Hi Nikhil,
It's a nice blog. I am also doing similar to that.
My service file has following content :
When i call this function from ui5 controller file. It is not calling anything, no error is coming.
fYI I changed the manifest file datasource as V2.
I think it comes around unbound entity scenario.
If you can help with either v2 / v4 service to make it work.
Regards,