$npx -y skills add likweitan/abap-skills --skill cds-view-entitiesHelp with CDS (Core Data Services) view entity development including data modeling, annotations, associations, compositions, access controls, aggregate expressions, built-in functions, and input parameters. Use when users ask about CDS views, CDS view entities, CDS annotations, C
| 1 | # CDS View Entities |
| 2 | |
| 3 | Guide for building semantic data models using ABAP CDS (Core Data Services) view entities in ABAP Cloud. |
| 4 | |
| 5 | ## Workflow |
| 6 | |
| 7 | 1. **Determine the user's goal**: |
| 8 | - Creating a new data model (standalone or for RAP) |
| 9 | - Defining relationships between entities (associations, compositions) |
| 10 | - Adding annotations for UI, semantics, or analytics |
| 11 | - Implementing access controls |
| 12 | - Using expressions, functions, or parameters in CDS |
| 13 | |
| 14 | 2. **Identify the context**: |
| 15 | - Standalone CDS view vs. RAP BO data model |
| 16 | - Root entity vs. child entity vs. projection view |
| 17 | - Transactional (RAP) vs. analytical vs. read-only consumption |
| 18 | |
| 19 | 3. **Apply best practices**: |
| 20 | - Use CDS view entities (v2 syntax `define view entity`) — not legacy CDS views (`define view`) |
| 21 | - Follow naming conventions (e.g., `ZR_*` for interface/BO views, `ZC_*` for consumption/projection views, `ZI_*` for reuse views) |
| 22 | - Add appropriate annotations for metadata consumers (UI, OData, analytics) |
| 23 | |
| 24 | ## CDS View Entity Syntax |
| 25 | |
| 26 | ### Basic View Entity |
| 27 | |
| 28 | ```cds |
| 29 | @AccessControl.authorizationCheck: #CHECK |
| 30 | @EndUserText.label: 'Sales Order' |
| 31 | define view entity ZI_SalesOrder |
| 32 | as select from zsalesorder |
| 33 | { |
| 34 | key order_id as OrderId, |
| 35 | customer_id as CustomerId, |
| 36 | order_date as OrderDate, |
| 37 | net_amount as NetAmount, |
| 38 | currency_code as CurrencyCode, |
| 39 | status as Status, |
| 40 | created_by as CreatedBy, |
| 41 | created_at as CreatedAt, |
| 42 | last_changed_at as LastChangedAt |
| 43 | } |
| 44 | ``` |
| 45 | |
| 46 | ### Root View Entity (for RAP) |
| 47 | |
| 48 | ```cds |
| 49 | @AccessControl.authorizationCheck: #CHECK |
| 50 | @EndUserText.label: 'Sales Order Root' |
| 51 | define root view entity ZR_SalesOrder |
| 52 | as select from zsalesorder |
| 53 | composition [0..*] of ZR_SalesOrderItem as _Item |
| 54 | { |
| 55 | key order_uuid as OrderUUID, |
| 56 | order_id as OrderId, |
| 57 | customer_id as CustomerId, |
| 58 | order_date as OrderDate, |
| 59 | @Semantics.amount.currencyCode: 'CurrencyCode' |
| 60 | net_amount as NetAmount, |
| 61 | currency_code as CurrencyCode, |
| 62 | status as Status, |
| 63 | |
| 64 | @Semantics.user.createdBy: true |
| 65 | created_by as CreatedBy, |
| 66 | @Semantics.systemDateTime.createdAt: true |
| 67 | created_at as CreatedAt, |
| 68 | @Semantics.user.localInstanceLastChangedBy: true |
| 69 | last_changed_by as LastChangedBy, |
| 70 | @Semantics.systemDateTime.localInstanceLastChangedAt: true |
| 71 | last_changed_at as LastChangedAt, |
| 72 | @Semantics.systemDateTime.lastChangedAt: true |
| 73 | last_changed_at as LastChangedAt, |
| 74 | |
| 75 | _Item |
| 76 | } |
| 77 | ``` |
| 78 | |
| 79 | ### Child View Entity |
| 80 | |
| 81 | ```cds |
| 82 | @AccessControl.authorizationCheck: #CHECK |
| 83 | @EndUserText.label: 'Sales Order Item' |
| 84 | define view entity ZR_SalesOrderItem |
| 85 | as select from zsalesorder_item |
| 86 | association to parent ZR_SalesOrder as _Order |
| 87 | on $projection.OrderUUID = _Order.OrderUUID |
| 88 | { |
| 89 | key item_uuid as ItemUUID, |
| 90 | order_uuid as OrderUUID, |
| 91 | product_id as ProductId, |
| 92 | quantity as Quantity, |
| 93 | @Semantics.amount.currencyCode: 'CurrencyCode' |
| 94 | unit_price as UnitPrice, |
| 95 | currency_code as CurrencyCode, |
| 96 | |
| 97 | @Semantics.user.createdBy: true |
| 98 | created_by as CreatedBy, |
| 99 | @Semantics.systemDateTime.localInstanceLastChangedAt: true |
| 100 | last_changed_at as LastChangedAt, |
| 101 | |
| 102 | _Order |
| 103 | } |
| 104 | ``` |
| 105 | |
| 106 | ### Projection View (Consumption Layer) |
| 107 | |
| 108 | ```cds |
| 109 | @AccessControl.authorizationCheck: #CHECK |
| 110 | @EndUserText.label: 'Sales Order Projection' |
| 111 | @Metadata.allowExtensions: true |
| 112 | define view entity ZC_SalesOrder |
| 113 | as projection on ZR_SalesOrder |
| 114 | { |
| 115 | key OrderUUID, |
| 116 | OrderId, |
| 117 | CustomerId, |
| 118 | OrderDate, |
| 119 | NetAmount, |
| 120 | CurrencyCode, |
| 121 | Status, |
| 122 | CreatedBy, |
| 123 | CreatedAt, |
| 124 | LastChangedBy, |
| 125 | LastChangedAt, |
| 126 | |
| 127 | _Item : redirected to composition child ZC_SalesOrderItem |
| 128 | } |
| 129 | ``` |
| 130 | |
| 131 | ## Associations & Compositions |
| 132 | |
| 133 | ### Association Types |
| 134 | |
| 135 | | Type | Syntax | Use Case | |
| 136 | | ------------------------- | ------------------------------------------------------- | ----------------------------------------------------- | |
| 137 | | **Regular association** | `association [0..1] to ZI_Customer |