Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
AndreaUS
Product and Topic Expert
Product and Topic Expert
26,976

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 ENTITYCDS custom entity7.89 | 2208 | 7.57
EXTEND ABSTRACT ENTITYCDS abstract entity7.84 | 2105 | 7.56
EXTEND VIEWCDS DDIC-based view7.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.

22 Comments
Labels in this area