Once you’ve defined a data model and the underlying database tables of an application, you can put a semantic and reusable basic interface CDS layer on top.
The basic interface layer is the foundation layer of CDS views and is directly deployed on top of the database tables. This layer isn’t supposed to do any complex calculations or join large sets of data; instead, this interface layer simply projects the fields provided by the underlying database tables to foster the granular reuse of these views.
To create a CDS view using the ABAP development tools in Eclipse, follow these steps:
- Open the ABAP perspective in the ABAP development tools in Eclipse.
- In the ABAP project, select the package node into which you want to create the table via the Project Explorer
- Open the context menu by right-clicking on the package and select New > Other ABAP Repository > Core Data Services > Data Definition.
- In the CDS creation wizard that appears, enter a Name and Description. In our case, we’ll call the purchase documents view
Z_I_PurchaseDocumentand the purchase document items view
- Select one of the provided templates to create a skeleton CDS view, for instance, Define View, which will create a simple projection view skeleton with one single data source and without any associations or joins.
- Click the Finish button in the wizard, and the source code editor will open so you can add fields or associations to the view.
Note: You can use the shortcut (Ctrl)+(Space) inside the field selection section of the CDS view source code editor to trigger the Eclipse content assist popup window to easily select fields of the underlying database table or CDS view, as shown below.
The code listing below shows the basic interface view for purchase documents, which creates simple projection on the purchase document table (table zpurchdocument) to expose all table fields and provide camel case aliases for fields that are relevant to end users. To follow the naming conventions for interface layer views according to the SAP S/4HANA VDM development guidelines, we named the views using the following pattern:
Z_I_<CDSViewName>. The Z_ indicates that a custom developed view in the customer namespace, and the I_ indicates that this view is an interface layer view. The view is also annotated with the
@VDM.viewType: #BASIC annotation, which assigns the view to the basic interface view layer of the VDM.
In addition to the persistent database fields, the view also contains several associations that are propagated to potential consumers of this view at the end of the field selection list. By convention, associations start with an underscore, for instance,
_ PurchaseDocumentItem. Usually, most of the (key) fields contained in a view will have foreign key associations to other views because data is supposed to be stored in a normalized and redundancy-free form. In some cases, we omit this for the sake of simplicity, but, as an example, we’ve added associations to the
PurchasingOrganization views. In the purchase document table, only the
PurchasingOrganization keys will be stored; however, additional data, like texts, can be read via the foreign key associations to these views.
Another important annotation is the authorization check
@AccessControl.authorizationCheck: #CHECK, which enables restricted access to a CDS view using a data control language (DCL) access control.
@ObjectModel.representativeKey annotation defines the field of the primary key that is specific for this view, and the
@ObjectModel.semanticKey defines the primary key of this view. For the purchase document basic interface view, this field is the
PurchaseDocument field in both cases. Fields containing descriptions or text should be annotated with @Semantics.text: true. The main description field of the entity can be linked to the entity key with the
@ObjectModel.text.element: ['Description'] annotation.
@EndUserText.label: 'Purchase Document'
define view Z_I_PurchaseDocument
as select from zpurchdocument
association [0..*] to Z_I_PurchaseDocumentItem as _PurchaseDocumentItem
on $projection.PurchaseDocument = _PurchaseDocumentItem.PurchaseDocument
association [0..1] to Z_I_PurchaseDocumentPriority as _Priority
on $projection.Priority = _Priority.Priority
association [0..1] to Z_I_PurchaseDocumentStatus as _Status
on $projection.Status = _Status.Status
association [0..1] to Z_I_PurchasingOrganization as _PurchasingOrganization
on $projection.PurchasingOrganization = _
key purchasedocument as PurchaseDocument,
description as Description,
status as Status,
priority as Priority,
purchasingorganization as PurchasingOrganization,
purchasedocumentimageurl as PurchaseDocumentImageURL,
The purchase document item basic interface view is shown in the next listing. Its representative key is the PurchaseDocumentItem field, which is the most specific part of its primary key and defines this entity. The semantic key consists of the PurchaseDocument and PurchaseDocumentItem fields and reflects the primary key of the entity. The purchase document item basic interface view contains a foreign key association to the purchase document view as well as to reuse views for the currency (I_Currency) and the unit of measure (I_UnitOfMeasure) provided by the standard SAP S/4HANA basic interface view layer.
As soon as views are explicitly released by SAP, you can use them in your own developments without the risk of introducing changes that break the view in future releases. The semantic currency and unit of measure annotations should be known from the table definition. We added the @DefaultAggregation:#NONE annotation to the Price and Quantity fields because all amount fields will be handled, by default, as measures and aggregated by SADL or the Analytical Engine. However, in our case, we explicitly want to suppress this default behavior.
@EndUserText.label: 'Purchase Document Item'
define view Z_I_PurchaseDocumentItem
as select from zpurchdocitem
association [1..1] to Z_I_PurchaseDocument as _PurchaseDocument on
$projection.PurchaseDocument = _PurchaseDocument.PurchaseDocument
association [0..1] to I_UnitOfMeasure as _QuantityUnitOfMeasure on
$projection.QuantityUnit = _QuantityUnitOfMeasure.UnitOfMeasure
association [0..1] to I_Currency as _Currency on
$projection.Currency = _Currency.Currency
key purchasedocumentitem as PurchaseDocumentItem,
key purchasedocument as PurchaseDocument,
description as Description,
vendor as Vendor,
vendortype as VendorType,
price as Price,
currency as Currency,
quantity as Quantity,
quantityunit as QuantityUnit,
purchasedocumentitemimageurl as PurchaseDocumentItemImageURL,
This post showed you how to create a basic interface CDS view using the ABAP programming language.
Editor’s note: This post has been adapted from a section of the book ABAP RESTful Programming Model: ABAP Development for SAP S/4HANA by Stefan Haas and Bince Mathew.