Skip to main content
SAP BTP

Creating an SAP CAPM Demo Project and Working with Custom Functions Concepts in SAP CAPM – Part 3

Ravi Krishna Thota Ravi Krishna Thota, Ravi Krishna Thota is a Developer based out of Mindset’s Hyderabad office 4 min read

In the previous two parts of this blog series, we discussed the different languages and notations in the Core Data Services (CDS) (An Introduction to CDS in the SAP CAPM; and Decoding CDS Query Language). In part three, we will look at how to create a simple CAPM project, implement custom functions in Cloud Application Programming Model and invoke the functions in the local host.

The basic flow of an SAP CAPM Project: The CAPM Project is a Full-Stack application

Diagram: Fiori UX or other UI front end, CAPM back-end framework, Node JS middleware, and HANA DB data store

A CAPM project is a Multi-Target application because it runs at different run times. For example, the persistence layer runs in the HANA DB run time, the service layer runs in the Node.js run time and The UX/UI layer runs in the Browser run time.

Diagram mapping a sample app's HTML5, Node JS and HANA DB layers to matching Cloud Foundry container applications

Step 1: Prerequisites for a CAPM project

Table of six setup steps, such as installing Node.js and @sap/cds-dk, needed in local VS Code versus SAP BAS

Step 2: Creating a small demo CAPM Project.

Go to your project folder location in VS code/BAS terminal and execute the command in terminal “cds init <ProjectName>” (cds init customer)

(or)

In SAP BAS we can create the CAPM project through the Template option.

After executing the above command the project is created in your selected location like the below structure.

Folder structure of the Customer project: app, db, srv, .cdsrc.json, package.json and README.md

  • Create a DB file with “.cds” extension in the db folder for DB model (entities), like below.

Ex: data-models.cds in db folder

CDS code defining a customer entity with id, name, type, emailId, contactNo, address and companyName fields

  • Create a Service file with “.cds” extension in the srv folder for service calls, like below.

Ex: custservice.cds in srv folder.

CDS code for CustService at path /api/customer/v1/CustSRV exposing customer as a projection

  • Execute npm install command in the project terminal.
  • Run the application using “cds run (or) cds watch” in the project terminal.

You should now be able to see the below screen

Welcome to @sap/cds Server page listing the /api/customer/v1/CustSRV endpoint and a Fiori preview link

Metadata result:

OData V4 metadata XML for CustService showing the customer entity type and its properties

V4 Results:

OData JSON response for customer with an empty value array

  • Create a .csv file for the temp data: Add the .csv file with dummy data like below;

CSV file location db/csv/<namespace>-<filename>.csv, example com.customer.info-customer.csv, with a header row and one sample customer record

  • Run again will get result like below

V4 Results:

OData JSON response for customer returning one record with id 100 and the sample field values

The Function implementation in a SAP CAPM Project

Now we will be implementing custom functions in the Cloud Application Programming Model and invoking the functions in localhost.

Note: The custom functions are used to do additional activities in CRUD operations.

  1. Within service definitions, you can additionally specify actions and functions.
  2. In SAP CAP, we have two types of functions (Bound Functions and Unbound Functions)
  3. We Need to Create One “.JS” file with the same name as the “.cds” in the srv folder in order to implement the custom logic.

Bound Functions

The Actions and functions can also be bound to individual entities of a service, enclosed in an additional actions block.

Implementation:

CDS service code with an actions block on customer defining boundedFunction with and without a msg parameter

Note: Multi params Example

function boundedFunction(msg:String, data:String) returns String;

In the above example, we have defined the Bound function in the .cds file which is bound to the individual entity customer.

Now, implementing custom logic to Bounded functions. (The next most important step is creating a function definition in the .js file.)

The file location is: srv→ custservice.js

JavaScript handler _customFunction returning a message, registered with srv.on for boundedFunction and boundedFunction1

Run URL:

//Without params

https://port4004-workspaces-ws-t4fcs.us10.applicationstudio.cloud.sap/api/customer/v1/CustSRV/customer/100/CustService.boundedFunction()

//With params

https://port4004-workspaces-ws-t4fcs.us10.applicationstudio.cloud.sap/api/customer/v1/CustSRV/customer/100/CustService.boundedFunction(mag=”)

Result Data:

OData JSON result of type Edm.String with the value "The function call..!"

Unbound Functions

The notion of actions and functions in CDS is adopted at the service level. Here no involvement of entities.

Implementation:

CDS service code adding unbound functions myfoobar and myfoobar1 at service level, highlighted

Let’s implement custom logic in custservice.js

JavaScript module.exports registering srv.on handlers, with myfoobar and myfoobar1 highlighted

Run URL:

// Without params

https://port4004-workspaces-ws-t4fcs.us10.applicationstudio.cloud.sap/api/customer/v1/CustSRV/myfoobar()

// With params

https://port4004-workspaces-ws-t4fcs.us10.applicationstudio.cloud.sap/api/customer/v1/CustSRV/myfoobar(mag=”)

Result:

OData JSON result of type Edm.String with the value "The function call..!"

Note: From the CDS perspective, I recommend preferring unbound actions/functions. This is because these are far more straightforward to implement and invoke. Because you now know how to create custom functions and implement the custom functions in SAP CAPM, you will have no problem using the function concept and implementing custom logic in your code.

Share