$npx -y skills add likweitan/abap-skills --skill odataHelp with OData service development in ABAP including OData V2 and V4 services via RAP service bindings, SEGW-based services, service definitions, service bindings, OData annotations, consumption of external OData services, and troubleshooting common OData errors. Use when users
| 1 | # OData Service Development |
| 2 | |
| 3 | Guide for creating and consuming OData services in ABAP, covering both RAP-based (V4/V2) and SEGW-based (V2) approaches. |
| 4 | |
| 5 | ## Workflow |
| 6 | |
| 7 | 1. **Determine the user's goal**: |
| 8 | - Exposing a RAP BO as an OData service (recommended approach) |
| 9 | - Creating a classic SEGW-based OData V2 service |
| 10 | - Consuming an external OData service from ABAP |
| 11 | - Adding OData annotations for Fiori UIs |
| 12 | - Troubleshooting OData errors |
| 13 | |
| 14 | 2. **Identify the approach**: |
| 15 | - RAP-based service (preferred for ABAP Cloud) |
| 16 | - SEGW-based service (classic, Standard ABAP only) |
| 17 | - OData consumption (client proxy) |
| 18 | |
| 19 | 3. **Guide implementation** following SAP best practices |
| 20 | |
| 21 | ## RAP-Based OData Services (Recommended) |
| 22 | |
| 23 | ### Architecture Flow |
| 24 | |
| 25 | ``` |
| 26 | CDS View Entity → Behavior Definition → Service Definition → Service Binding |
| 27 | ↓ |
| 28 | OData V4/V2 Endpoint |
| 29 | ``` |
| 30 | |
| 31 | ### Service Definition |
| 32 | |
| 33 | Exposes CDS entities and their behaviors as a named service: |
| 34 | |
| 35 | ```cds |
| 36 | @EndUserText.label: 'Travel Service' |
| 37 | define service ZUI_TRAVEL_O4 { |
| 38 | expose ZC_Travel as Travel; |
| 39 | expose ZC_Booking as Booking; |
| 40 | expose I_Currency as Currency; |
| 41 | expose I_Country as Country; |
| 42 | } |
| 43 | ``` |
| 44 | |
| 45 | #### Key Rules |
| 46 | |
| 47 | - Expose projection CDS views (C\_ prefix by convention), not root views |
| 48 | - Include value help CDS views (I\_\* views) the UI needs |
| 49 | - Alias names become OData entity set names |
| 50 | - One service definition can be bound to multiple protocols |
| 51 | |
| 52 | ### Service Binding |
| 53 | |
| 54 | Binds a service definition to a specific OData protocol and provides a URL: |
| 55 | |
| 56 | | Binding Type | Protocol | Use Case | |
| 57 | | -------------------- | -------- | ---------------------------------- | |
| 58 | | `OData V4 - UI` | V4 | SAP Fiori Elements apps | |
| 59 | | `OData V2 - UI` | V2 | Legacy Fiori apps, older frontends | |
| 60 | | `OData V4 - Web API` | V4 | API consumption (A2X scenarios) | |
| 61 | | `OData V2 - Web API` | V2 | API consumption (legacy) | |
| 62 | | `InA - UI` | InA | Analytical scenarios | |
| 63 | |
| 64 | ### Creating a Service Binding |
| 65 | |
| 66 | 1. In ADT: **New → Other ABAP Repository Object → Business Services → Service Binding** |
| 67 | 2. Select the service definition |
| 68 | 3. Choose binding type (e.g., OData V4 - UI) |
| 69 | 4. Activate |
| 70 | 5. Click **Publish** to register the service |
| 71 | 6. Click **Preview** to open the Fiori Elements preview |
| 72 | |
| 73 | ## OData V4 vs V2 |
| 74 | |
| 75 | | Feature | OData V4 | OData V2 | |
| 76 | | --------------------- | ----------------------------- | ------------------------------- | |
| 77 | | **Protocol** | JSON by default | XML (Atom) default, JSON option | |
| 78 | | **Batch** | `$batch` with JSON | `$batch` with multipart | |
| 79 | | **Deep operations** | Deep create/update supported | Limited | |
| 80 | | **Actions/Functions** | Bound and unbound | Function imports | |
| 81 | | **Filtering** | `$filter` with `lambda` | `$filter` basic | |
| 82 | | **Draft** | Full support | Supported via extensions | |
| 83 | | **Aggregation** | `$apply` transformation | Not natively supported | |
| 84 | | **Recommendation** | Preferred for new development | Maintain existing only | |
| 85 | |
| 86 | ## SEGW-Based OData V2 Services (Classic) |
| 87 | |
| 88 | For Standard ABAP only (not available in ABAP Cloud): |
| 89 | |
| 90 | ### Architecture |
| 91 | |
| 92 | ``` |
| 93 | SEGW Project → Data Model (Entity Types, Sets, Associations) |
| 94 | → Service Implementation (MPC/DPC classes) |
| 95 | → Register & Activate in /IWFND/MAINT_SERVICE |
| 96 | ``` |
| 97 | |
| 98 | ### Steps |
| 99 | |
| 100 | 1. **Create project** in `SEGW` transaction |
| 101 | 2. **Define data model**: Entity types, properties, navigation properties |
| 102 | 3. **Generate runtime artifacts** (MPC/DPC classes) |
| 103 | 4. **Implement DPC extension methods**: `GET_ENTITYSET`, `GET_ENTITY`, `CREATE_ENTITY`, etc. |
| 104 | 5. **Register service** in `/IWFND/MAINT_SERVICE` |
| 105 | 6. **Test** via `/IWFND/GW_CLIENT` or browser |
| 106 | |
| 107 | ### DPC Method Implementation Example |
| 108 | |
| 109 | ```abap |
| 110 | METHOD travelset_get_entityset. |
| 111 | SELECT * FROM ztravel_tab |
| 112 | INTO TABLE @DATA(lt_travel) |
| 113 | UP TO 100 ROWS. |
| 114 | |
| 115 | et_entityset = CORRESPONDING #( lt_travel |