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

      1 Comment
      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