Skip to Content
Technical Articles
Author's profile photo Andrea Schlotthauer

ABAP Core Data Services: New syntax for extending CDS entities

This blog post provides an overview of all available types of CDS extensions.

Overview of CDS entity extensions

Statement Can be used for Available since
EXTEND VIEW ENTITY
  • CDS view entity
  • CDS projection view
7.87 | 1911 | 7.55
EXTEND CUSTOM ENTITY CDS custom entity 7.89 | 2208 | 7.57
EXTEND ABSTRACT ENTITY CDS abstract entity 7.84 | 2105 | 7.56
EXTEND VIEW CDS DDIC-based view 7.40, SP08

EXTEND VIEW ENTITY

The statement EXTEND VIEW ENTITY is a replacement of EXTEND VIEW, designed for the extension of CDS view entities and CDS projection views.

Differences Between View Extensions and View Entity Extensions

  • A view entity extension does not have a DDIC append view.
  • CDS view extends with EXTEND VIEW require you to define at least one view field. This requirement has been removed. CDS view entity extensions also work without any new field. You can, for example, define only a new association.
  • For a view entity extension, no name is specified after WITH. A view entity extension has only one name, which is the name of the repository object. This name is assigned in the wizard for creating a new repository object.
  • In view entity extensions, no header annotations are allowed. That means that no annotations are allowed in front of the statement EXTEND VIEW ENTITY.
  • View entity extensions were developed for CDS view entities and they have the same advantages, for example, much better activation performance and new features, such as typed literals and improved currency and quantity handling.

An existing CDS view can have one or more CDS view entity extensions. A CDS entity extension can not be further extended.

Both kinds of view extensions, EXTEND VIEW and EXTEND VIEW ENTITY, can be used for all three kinds of CDS views (CDS view entities, CDS projection views, CDS DDIC-based views). This is supported for migration and compatibility reasons, but it is not recommended.

How to extend a CDS view entity

Prerequisites

The header annotation @AbapCatalog.viewEnhancementCategory[ ] specifies how a CDS view entity can be extended. The possible values are:

  • #PROJECTION_LIST: Extensions of the SELECT list and additional CDS associations are allowed
  • #GROUP_BY: can only be specified together with #PROJECTION_LIST. Allows extensions to views that have aggregate expressions in the SELECT list.
  • #UNION: Can only be specified together with #PROJECTION_LIST. Allows extensions to views that use a clause with a set operator (UNION, EXCEPT, INTERSECT).
  • #NONE: Extensions are not allowed.

What can be extended?

  • New fields can be added.
  • New associations can be defined and exposed.
  • All kinds of elements that are allowed in the SELECT list of the respective entity (CDS view entity or CDS projection view) can be added in the extension. That means that a CDS view entity extension to a CDS view entity can define elements such as path expressions, typed literals, expressions, and functions. A CDS view entity extension to a CDS projection view can define elements such as virtual elements, localized elements, and redefined associations.

Restrictions

  • New input parameters are not possible.
  • A regular view entity cannot be transformed into a root view entity.
  • An appended field cannot be defined as key field.
  • No new header annotations can be defined.

Example

The following CDS view entity allows extensions of the SELECT list.

@AccessControl.authorizationCheck: #NOT_REQUIRED
@AbapCatalog.viewEnhancementCategory: [#PROJECTION_LIST]
@EndUserText.label: 'Further information about the CDS entity'
define view entity DEMO_CDS_ORIGINAL_VIEW_VE 
  as select from
           spfli
      join scarr on
        scarr.carrid = spfli.carrid
    {
      key scarr.carrname     as carrier,
      key spfli.carrid       as carrierId,
      key spfli.connid       as flight,
          spfli.cityfrom     as departure,
          spfli.cityto       as destination
    }; 

The following CDS view entity extension defines and exposes a new association:

extend view entity DEMO_CDS_ORIGINAL_VIEW_VE with
association [1..*] to sflight as _sflight 
  on $projection.carrierId = _sflight.carrid
{
  _sflight
}

EXTEND CUSTOM ENTITY

CDS custom entity extensions can be used to add elements to a custom entity without making any modifications to the original entity.

  • Prerequisite: the extended entity must allow extensions. Extensions are allowed per default or can be explicitly allowed with the annotation @AbapCatalog.extensibility.extensible with the value true.
  • Extension elements: New elements, new associations, and new compositions can be defined in the extension.
  • Restrictions: 
    • New input parameters are not possible.
    • A custom entity cannot be turned into a root entity by an extensions.
    • Defining new header annotations is not supported.
    • Defining new to-parent associations in an extension is not supported.
    • No new key elements can be defined.

Example

The following CDS custom entity extension

extend custom entity DEMO_CDS_ORIGINAL_CUSTOM with 
{ 
  col4    : abap.char(1); 
  _assoc  : association to DEMO_CDS_CUSTOM_ENTITY  
    on $projection.col2 = _assoc.id; 
  _compos : composition [0..1] of DEMO_CDS_CUSTOM_CHILD; 
} 

extends the existing CDS custom entity

@EndUserText.label: 'CDS custom entity, extended entity' 
@AbapCatalog.extensibility.extensible: true 

define root custom entity DEMO_CDS_ORIGINAL_CUSTOM 
  with parameters 
    param1 : abap.int4 
{ 
  key col1 : abap.char( 5 ); 
      col2 : abap.int4; 
      col3 : abap.int4; 
} 

EXTEND ABSTRACT ENTITY

CDS abstract entity extensions were designed specifically to extend CDS abstract entities. Here’s a short overview of the rules:

  • Prerequisite: As a prerequisite, the extended entity must allow extensions. Extensions are allowed per default or can be explicitly allowed with the annotation @AbapCatalog.extensibility.extensible with the value true.
  • Elements of the extension: New elements and new associations can be defined in the extension.
  • Restrictions: New input parameters are not possible. An abstract entity cannot be turned into a root entity by an extensions. Defining new header annotations is not supported.

Example

The following CDS abstract entity allows extensions:

@EndUserText.label: 'CDS abstract entity with extension'
@AbapCatalog.extensibility.extensible: true

define abstract entity DEMO_CDS_ORIGINAL_ABSTRACT
  with parameters
    param1 : abap.int4
{
  col1 : abap.char( 5 );
  col2 : abap.int4;
  col3 : abap.int4;
}

A new field and a new association are added by the following CDS abstract entity extension:

extend abstract entity DEMO_CDS_ORIGINAL_ABSTRACT with
{
  col4   : abap.char( 1 );
  _assoc : association to demo_cds_abstract_entity  
             on $projection.col1 = _assoc.col1;
}

The following program evaluates the structure of the enhanced CDS abstract entity using RTTI methods:

CLASS demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS demo IMPLEMENTATION.
  METHOD main.
    DATA(out) = cl_demo_output=>new( ).

    DATA(components) =
      CAST cl_abap_structdescr( cl_abap_typedescr=>describe_by_name(
                                  'DEMO_CDS_ORIGINAL_ABSTRACT' )
                               )->components.
    out->write( components ).

    cl_dd_ddl_annotation_service=>get_direct_annos_4_entity(
      EXPORTING
        entityname =    'DEMO_CDS_ORIGINAL_ABSTRACT'
      IMPORTING
        annos      =    DATA(annos) ).
    out->write( annos ).
*
    DATA(struct) = VALUE demo_cds_original_abstract(
       col1 = 'hallo'
       col2 = 333
       col3 = 2
       col4 = 'A' ).
    out->write( struct ).

    out->display( ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo=>main( ).

The result set contains the column added via the extension:

Further information on CDS entity extensions is available in the ABAP Keyword Documentation, secion about CDS entity extensions.

Assigned Tags

      6 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Carlin Willams
      Carlin Willams

      Hi Andrea,

      Thanks for nice blog.

      If we want to extend one more condition in where clause of standard Consumption View how can we extend. Please guide me.

      define view C_SlsDocFlfmtSlsDocTypeVH

      as select from I_SalesDocumentType as SalesDocumentType

      association [0..*] to I_SalesDocumentTypeText  as _Text on $projection.SalesDocumentType = _Text.SalesDocumentType

      {

      @ObjectModel.text.association: '_Text'

      @Search.defaultSearchElement: true

      key SalesDocumentType.SalesDocumentType,

       

      @Search.defaultSearchElement: true

      @UI.hidden: true

      SalesDocumentType._SalesDocumentTypeLangDepdnt[1:Language=$session.system_language].SalesDocumentTypeLangDepdnt,

       

      //Association

      _Text

      }

      where ( SalesDocumentType.SDDocumentCategory = 'C' or    --> Order

      SalesDocumentType.SDDocumentCategory = 'I' or    --> Order w/o charge

      SalesDocumentType.SDDocumentCategory = 'L' )

      Author's profile photo Andrea Schlotthauer
      Andrea Schlotthauer
      Blog Post Author

      Hi Carlin,

       

      the WHERE clause cannot be extended using EXTEND VIEW. Here's the documentation which lists all available syntax additions of EXTEND VIEW.

      I recommend you to create another view (wrapper view) that selects from I_SalesDocumentType and that adds one more condition to the WHERE clause.

      Hope this helps.

      Best

      Andrea

      Author's profile photo Carlin Willams
      Carlin Willams

      Thank you for your swift response.

      I can create another custom View (Z), but how can we replace (extend) this ZView in another Consumption stanadard View which is reffred the stanadrd Fiori application/Odata Services.

      Please help.

      Thanks , carlin

       

      Author's profile photo Andrea Schlotthauer
      Andrea Schlotthauer
      Blog Post Author

      Hi Carlin,

      sorry, extensions of the WHERE clause are not possible and I cannot offer any simple solution to this issue.

      Best

      Andrea

      Author's profile photo Carlin Willams
      Carlin Willams

      Hi Andrea,

      I am so sorry , may be i miss communicated to you.

      I will create new Z CDS View , in this view i will modify where condition.

      How i can change reference(C_SlsDocFlfmtSlsDocTypeVH) with ZCDS View which i have created in standard Consumption View: C_SlsDocFlfllmntAnalyzer.

      Below code is stanadard Consumption View: C_SlsDocFlfllmntAnalyzer , need to replace C_SlsDocFlfmtSlsDocTypeVH with Z View

      @Consumption.valueHelpDefinition: [{entity: {name: 'C_SlsDocFlfmtSlsDocTypeVH', element: 'SalesDocumentType'} }]
      SalesDocument.SalesDocumentType,

       

      Please guide.

      I've searched in forums but no luck.

      Thanks, Carlin

       

      Author's profile photo Andrea Schlotthauer
      Andrea Schlotthauer
      Blog Post Author

      Hi Carlin,

      I have checked with the dev team, but I'm so sorry I still cannot offer any solution.

      A wrapper view is useful only for your own application. But if we're talking about a standard SAP Fiori app, it is to my knowledge not possible to replace an existing reference with a ZCDS view.

      To ensure upgrade stability, only certain stable extension points can be extended or modified. The WHERE clause can't be extended or modified in a stable fashion. And the reference to a view also can't be modified, to my knowledge.

      Best

      Andrea