In SAP CAP, building applications is not only about modeling data with CDS but also about implementing custom logic to meet business requirements. Understanding CAP custom logic through event handlers, hooks, and extensions allows developers to create dynamic, maintainable, and scalable applications.
For structured hands-on learning, consider enrolling in a practical SAP course. Knowledge of SAP HANA enhances the performance of custom logic in production environments.
Understanding Event Handlers
Event handlers allow developers to intercept and process operations on entities or services. Events can be triggered for actions like CREATE, READ, UPDATE, DELETE (CRUD) or for custom actions and functions.
Example: Handling a CREATE event for Orders
this.on('CREATE', 'Orders', async (req) => {
const { productID, quantity } = req.data;
// Custom logic: validate stock before creation
const product = await cds.run(SELECT.from('Products').where({ ID: productID }));
if (product.stock < quantity) throw new Error('Insufficient stock');
});
This ensures that business rules are enforced before data is persisted.
Using Hooks
Hooks are functions triggered before or after standard operations, making it easy to implement reusable business logic. Common hooks include:
before CREATE/UPDATE/DELETEafter READ/UPDATE
Example: Updating inventory after an order is created
this.before('CREATE', 'Orders', async (req) => {
await cds.run(UPDATE('Products')
.set({ stock: { '-=': req.data.quantity } })
.where({ ID: req.data.productID }));
});
Hooks make it easier to maintain centralized business rules rather than scattering logic across multiple places.
Leveraging Extensions
Extensions allow you to enhance existing services or entities without modifying the original definitions. This is particularly useful for multi-team projects or when extending SAP standard services.
Example: Extending a service to include a custom action
extend service CatalogService {
action cancelOrder(orderID : UUID) returns Boolean;
}
Then implement the logic in your service handler:
this.on('cancelOrder', async (req) => {
const { orderID } = req.data;
await cds.run(UPDATE('Orders').set({ status: 'Cancelled' }).where({ ID: orderID }));
return true;
});
Real-World Use Case
- Orders are created and validated with event handlers.
- Inventory is updated automatically using before hooks.
- New business actions like
cancelOrderorshipOrderare added via service extensions. - All custom logic remains modular, maintainable, and easily testable.
Best Practices
- Keep event handlers focused and reusable
- Use before/after hooks to centralize business rules
- Apply extensions for service scalability without modifying base code
- Document all custom logic to aid team collaboration
- Combine with CDS annotations for validation and UI enhancements
Conclusion
Mastering CAP custom logic through event handlers, hooks, and extensions empowers developers to implement complex business rules while maintaining clean, scalable code. Combine this practical knowledge with an expert-led SAP course and deepen your backend understanding with SAP HANA to build production-ready CAP applications.
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

WhatsApp us