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:
CatalogServiceexposes Products and Orders.- An action
shipOrderupdates order status. - A function
totalStockValuecalculates inventory worth. - 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.

WhatsApp us