Programming

How to Create a Basic Interface CDS View with ABAP

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.

 

This blog post will show you how to do this; we’ll adhere to the standard development guidelines of the SAP S/4HANA VDM based on CDS views.

 

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:

  1. Open the ABAP perspective in the ABAP development tools in Eclipse.
  2. In the ABAP project, select the package node into which you want to create the table via the Project Explorer
  3. Open the context menu by right-clicking on the package and select New > Other ABAP Repository > Core Data Services > Data Definition.
  4. In the CDS creation wizard that appears, enter a Name and Description. In our case, we’ll call the purchase documents view Z_I_PurchaseDocument and the purchase document items view Z_I_PurchaseDocumentItem.
  5. 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.
  6. 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.

 

Triggering the Eclipse Content Assist for CDS Fields Using the Keyboard Shortcut

 

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 Priority, Status, and PurchasingOrganization views. In the purchase document table, only the Priority, Status, and 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.

 

The @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.

 

@AbapCatalog.sqlViewName: 'ZIPURCHDOC'

@EndUserText.label: 'Purchase Document'

@AccessControl.authorizationCheck: #CHECK

@VDM.viewType: #BASIC

@ObjectModel.representativeKey: 'PurchaseDocument'

@ObjectModel.semanticKey: ['PurchaseDocument']

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 = _

PurchasingOrganization.PurchasingOrganization

{

    @ObjectModel.text.element: ['Description']

  key purchasedocument as PurchaseDocument,

    @Semantics.text: true

    description as Description,

    @ObjectModel.foreignKey.association: '_Status'

    status as Status,

    @ObjectModel.foreignKey.association: '_Priority'

    priority as Priority,

    @ObjectModel.foreignKey.association: '_PurchasingOrganization'

    purchasingorganization as PurchasingOrganization,

    @Semantics.imageUrl: true

    purchasedocumentimageurl as PurchaseDocumentImageURL,

    crea_date_time,

    crea_uname,

    lchg_date_time,

    lchg_uname,

    // Associations

    _PurchaseDocumentItem,

    _Priority,

    _Status,

    _PurchasingOrganization

}

 

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.

 

@AbapCatalog.sqlViewName: 'ZIPURCHDOCITEM'

@EndUserText.label: 'Purchase Document Item'

@AccessControl.authorizationCheck: #NOT_REQUIRED

@VDM.viewType: #BASIC

@ObjectModel.representativeKey: 'PurchaseDocumentItem'

@ObjectModel.semanticKey: ['PurchaseDocumentItem','PurchaseDocument']

 

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

{

 

    @ObjectModel.text.element: ['Description']

  key purchasedocumentitem as PurchaseDocumentItem,

 

    @ObjectModel.foreignKey.association: '_PurchaseDocument'

  key purchasedocument as PurchaseDocument,

 

    @Semantics.text: true

    description as Description,

 

    vendor as Vendor,

    vendortype as VendorType,

 

    @Semantics.amount.currencyCode: 'Currency'

    @DefaultAggregation: #NONE

    price as Price,

    @Semantics.currencyCode: true

    @ObjectModel.foreignKey.association: '_Currency'

    currency as Currency,

 

    @Semantics.quantity.unitOfMeasure: 'QuantityUnit'

    @DefaultAggregation: #NONE

    quantity as Quantity,

    @Semantics.unitOfMeasure: true

    @ObjectModel.foreignKey.association: '_QuantityUnitOfMeasure'

    quantityunit as QuantityUnit,

 

    @Semantics.imageUrl: true

    purchasedocumentitemimageurl as PurchaseDocumentItemImageURL,

 

    crea_date_time,

    crea_uname,

    lchg_date_time,

    lchg_uname,

 

    // Associations

    _PurchaseDocument,

    _QuantityUnitOfMeasure,

    _Currency

}

 

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.

Recommendation

ABAP RESTful Programming Model: ABAP Development for SAP S/4HANA
ABAP RESTful Programming Model: ABAP Development for SAP S/4HANA

You’ve worked with ABAP, SAP Fiori, and OData—now see how these technologies and more come together in the ABAP RESTful programming model! Build on your expertise to create cloud-ready applications for SAP S/4HANA and deploy applications to the SAP Fiori launchpad. Manage applications with Git version control, automated testing, and continuous integration. Make the new model work for you! Based on the best-selling book, ABAP Programming Model for SAP Fiori by Stefan Haas and Bince Mathew.

Learn More
SAP PRESS
by SAP PRESS

SAP PRESS is the world's leading SAP publisher, with books on ABAP, SAP S/4HANA, SAP CX, intelligent technologies, SAP Business Technology Platform, and more!

Comments