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
8,055

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
SebastianEsch
Active Participant
0 Kudos

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

cvasaba1
Explorer
0 Kudos
Thanks fo rthe blog. Quick question. How to call this bound action programatically from the node js service?
ramuknura1395
Explorer
Hi Nikhil,

It's a nice blog. I am also doing similar to that.

My service file has following content :

 

service Etlr {

function fgcall(procedure_name: String) returns Boolean;


}



I have done test in localhost of it, it works fine. So i thought of consuming it in ui5 app.
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.



  // oGModel.callFunction("/fgcall",

            // {

            //  method: "POST",

            //  urlParameters: {"procedure_name" :"GG_CALL"},

            //     success: function (oData, response) {

            //         // that.getView().setBusy(false);

            //         debugger

            //         console.log('success');

            //         // resolve(oData.results);

            //     }.bind(this),

            //     error: function (error) {

            //         // that.getView().setBusy(false);

            //         // reject(oResponse);

            //         debugger

            //         console.log('9failure');

            //     }.bind(this)

            // });



I think it comes around unbound entity scenario.
If you can help with either v2 / v4 service to make it work.


Regards,

Arun -D
Aarish
Discoverer
0 Kudos

Hi @ramuknura1395 

I'm also trying this scenario. But facing same issue. Have you ressolved it? 

 

Labels in this area