Building CAP Services Using CDS: Actions, Functions & Queries

Creating powerful and reusable services is the heart of SAP CAP development. With CAP services using CDS, you can define business logic, expose OData endpoints, and implement complex operations with actions, functions, and queries. This guide will help beginners and corporate developers understand how to model, implement, and query services efficiently.

For hands-on CAP development, enroll in a structured SAP course. Combining this with SAP HANA knowledge ensures your services perform optimally in production environments.

Understanding CAP Services

In CAP, services are layers that expose your CDS entities to applications. Services handle operations such as:

  • Fetching data with queries
  • Performing actions that modify data
  • Executing functions that return calculated results

All these are defined declaratively in CDS, reducing the need for boilerplate code.

Defining a Basic Service

Services are created in the srv/ folder using CDS syntax. Here’s a simple example exposing Products and Orders:

using my.bookshop from '../db/schema';

service CatalogService {
  entity Products as projection on my.bookshop.Products;
  entity Orders as projection on my.bookshop.Orders;
}

With this service, CAP automatically generates OData endpoints for Products and Orders.

Implementing Actions

Actions are operations that perform changes or execute business logic. For example, marking an order as shipped:

service CatalogService {
  action shipOrder(orderID : UUID) returns Boolean;
}

You implement the logic in a Node.js handler:

this.on('shipOrder', async (req) => {
  const { orderID } = req.data;
  await cds.run(UPDATE(Orders).set({ status: 'Shipped' }).where({ ID: orderID }));
  return true;
});

Actions allow encapsulation of custom logic while remaining part of the service interface.

Implementing Functions

Functions are read-only operations that return data or calculated values. For example, getting total stock value:

service CatalogService {
  function totalStockValue() returns Decimal;
}

Handler implementation:

this.on('totalStockValue', async () => {
  const result = await cds.run(SELECT.from(Products).columns(['sum(price*stock) as total']));
  return result[0].total;
});

Functions ensure read-only operations are separated from mutating actions.

Querying Services

CAP services support advanced queries directly using OData and CDS syntax:

SELECT from Products { ID, name, price }
WHERE stock > 0
ORDER BY price desc;

Projections, filters, and expansions allow clients to consume exactly the data they need.

Best Practices for CAP Services

  • Keep actions and functions specific and reusable
  • Use projections to control exposed fields
  • Validate inputs in handlers to prevent inconsistent data
  • Use associations in queries to reduce joins manually
  • Document service endpoints for team clarity and integration

Real-World Use Case

Consider an e-commerce CAP application:

  1. CatalogService exposes Products and Orders.
  2. An action shipOrder updates order status.
  3. A function totalStockValue calculates inventory worth.
  4. Queries filter products by category, availability, or price.

This approach ensures services are modular, maintainable, and ready for frontend apps.

Conclusion

Mastering CAP services using CDS with actions, functions, and queries enables you to build flexible and scalable SAP applications. By clearly defining service interfaces and handlers, you can provide consistent and robust endpoints for all client applications.

Combine practical experience with a structured SAP course and strengthen backend knowledge using SAP HANA for optimal service performance.

you may be interested in this blog here:-

Don’t Fear the Update: Navigating the Challenges of how to implement sap note

Five Top Technology Investment Drivers for 2024

How many dollars worth of RSU does Salesforce typically offer an MTS (experienced hire) on joining?

Integration cloud system to HANA Cloud Platform using Cloud Connector

₹25,000.00

SAP SD S4 HANA

SAP SD (Sales and Distribution) is a module in the SAP ERP (Enterprise Resource Planning) system that handles all aspects of sales and distribution processes. S4 HANA is the latest version of SAP’s ERP suite, built on the SAP HANA in-memory database platform. It provides real-time data processing capabilities, improved…
₹25,000.00

SAP HR HCM

SAP Human Capital Management (SAP HCM)  is an important module in SAP. It is also known as SAP Human Resource Management System (SAP HRMS) or SAP Human Resource (HR). SAP HR software allows you to automate record-keeping processes. It is an ideal framework for the HR department to take advantage…
₹25,000.00

Salesforce Administrator Training

I am text block. Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
₹25,000.00

Salesforce Developer Training

Salesforce Developer Training Overview Salesforce Developer training advances your skills and knowledge in building custom applications on the Salesforce platform using the programming capabilities of Apex code and the Visualforce UI framework. It covers all the fundamentals of application development through real-time projects and utilizes cases to help you clear…
₹25,000.00

SAP EWM

SAP EWM stands for Extended Warehouse Management. It is a best-of-breed WMS Warehouse Management System product offered by SAP. It was first released in 2007 as a part of SAP SCM meaning Supply Chain Management suite, but in subsequent releases, it was offered as a stand-alone product. The latest version…
₹25,000.00

Oracle PL-SQL Training Program

Oracle PL-SQL is actually the number one database. The demand in market is growing equally with the value of the database. It has become necessary for the Oracle PL-SQL certification to get the right job. eLearning Solutions is one of the renowned institutes for Oracle PL-SQL in Pune. We believe…
₹25,000.00

Pega Training Courses in Pune- Get Certified Now

Course details for Pega Training in Pune Elearning solution is the best PEGA training institute in Pune. PEGA is one of the Business Process Management tool (BPM), its development is based on Java and OOP concepts. The PAGA technology is mainly used to improve business purposes and cost reduction. PEGA…
₹27,000.00

SAP PP (Production Planning) Training Institute

SAP PP Training Institute in Pune SAP PP training (Production Planning) is one of the largest functional modules in SAP. This module mainly deals with the production process like capacity planning, Master production scheduling, Material requirement planning shop floor, etc. The PP module of SAP takes care of the Master…
X
WhatsApp WhatsApp us
Call Now Button