In the expansive world of SAP, efficient data modeling and consumption are paramount. SAP Core Data Services (CDS) views have revolutionized how we define and consume data in S/4HANA and beyond. Among the various types of CDS views, the Projection View stands out as a powerful tool for simplifying, refining, and exposing data for various consumption layers, including Fiori applications and analytical tools.
A CDS Projection View acts as a wrapper around an existing CDS view (or even a database table), allowing you to project a subset of fields, rename them, create calculated fields, apply filters, and expose associations selectively. It's akin to creating a tailored 'view' of your underlying data model without modifying the original. This capability is crucial for adhering to 'separation of concerns' – your foundational CDS views can remain complex and comprehensive, while projection views offer simplified, purpose-built interfaces for specific applications or APIs.
Let's illustrate this with an example. Imagine we have a base CDS view that joins sales order headers and items, providing a comprehensive dataset. We'll call it I_SalesOrderAnalysis
.
@AbapCatalog.sqlViewName: 'ZSOSRANLBASE'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Sales Order Analysis Base'
define view ZI_SalesOrderAnalysis as select from I_SalesOrder as SalesOrder
association [0..*] to I_SalesOrderItem as _Item on _Item.SalesOrder = SalesOrder.SalesOrder
{
key SalesOrder.SalesOrder,
SalesOrder.CreationDate,
SalesOrder.OverallSDProcessStatus,
SalesOrder.TotalNetAmount,
SalesOrder.TransactionCurrency,
SalesOrder.SoldToParty,
SalesOrder.SoldToPartyName,
_Item.SalesOrderItem,
_Item.Material,
_Item.MaterialText,
_Item.NetPriceAmount,
_Item.NetPriceQuantity,
_Item.NetPriceQuantityUnit
}
Now, let's create a Projection View specifically for a Fiori application that needs to display a simplified list of sales orders with only key header information and a calculated status description. We'll call this ZC_SalesOrderHeader_PRJ
.
@AbapCatalog.sqlViewName: 'ZSOHDRPRJ'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Sales Order Header Projection'
@OData.publish: true
define view ZC_SalesOrderHeader_PRJ as projection on ZI_SalesOrderAnalysis
{
key SalesOrder,
CreationDate,
TotalNetAmount,
TransactionCurrency,
SoldToParty,
SoldToPartyName,
// Calculate a readable status description
case OverallSDProcessStatus
when 'C' then 'Completed'
when 'B' then 'Partially Processed'
when 'A' then 'Open'
else 'Unknown'
end as OverallStatusDescription,
/* Associations can be exposed directly or renamed */
_Item
}
Let's break down the key aspects of this projection view:
projection on ZI_SalesOrderAnalysis
: This explicitly states thatZC_SalesOrderHeader_PRJ
is a projection on our base viewZI_SalesOrderAnalysis
.- Field Selection and Aliasing: We've selected
SalesOrder
,CreationDate
,TotalNetAmount
, etc. If we wanted to rename a field, we could doCreationDate as OrderDate
. - Calculated Fields: The
case ... end as OverallStatusDescription
demonstrates how you can derive new fields based on existing ones. This logic is executed at the database level, enhancing performance. - Exposing Associations: The
_Item
association from the base view is directly exposed. This allows consumers ofZC_SalesOrderHeader_PRJ
to navigate to related sales order item data if needed, using standard OData navigation properties. - Annotations: The
@OData.publish: true
annotation is crucial. It automatically generates an OData service for this view, making it consumable by Fiori Elements, analytical clients, or any RESTful API consumer. Other annotations like@UI.lineItem
or@UI.selectionField
could be added for Fiori UI specific behavior.
Projection views are fundamental for building robust and flexible data models in SAP. They enforce a clean separation between core data definitions and application-specific consumption models, simplifying development, improving maintainability, and providing tailored data exposure without data duplication. Whether you're building Fiori apps, exposing data via OData, or creating analytical queries, mastering CDS projection views is an indispensable skill.
No comments:
Post a Comment