Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
NikhilPuri
Explorer

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} ]
}
]
}
}}

If server listening is stopped, Execute cds watch again from the Terminal to see the output in new tab. Otherwise, refresh the browser and check the output.



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


 

Let's check the output of Unbound function.


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

 
4 Comments
Labels in this area