Skip to Content
Technical Articles
Author's profile photo Nikhil Puri

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%20Function%20in%20cds%20file

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%20definition%20in%20.js%20file

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%20of%20Bound%20function

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%20Function%20in%20.cds%20file

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.

Definition%20of%20Unbound%20Function

Unbound Function Definition in .js file

 

Let’s check the output of Unbound function.
Output%20of%20Unbound%20function

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

 

Assigned Tags

      3 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Sebastian Esch
      Sebastian Esch

      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

      Author's profile photo Phani Vasabattula
      Phani Vasabattula

      Thanks fo rthe blog. Quick question. How to call this bound action programatically from the node js service?

      Author's profile photo Arun D
      Arun D

      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