Extending CDS Views

Objective

After completing this lesson, you will be able to Extend CDS entities using CDS view extensions.

CDS View Extensions

Technically, the extension of CDS entities is very similar to the extension of dictionary objects. Customers or partners create new development objects that refer to the development object that they want to extend. In the ABAP dictionary they create APPEND structures, in CDS they define CDS entity extensions. CDS view entities are extended with CDS view extensions. A CDS view extensions is defined in a dedicated data definition using statement EXTEND VIEW ENTITY. An existing CDS view can have one or more CDS view extensions.

In this example, CDS view entity /DMO/E_Agency on the left contains one element in the element list. The view extension on the right, adds an element, ZZCategoryZAG, to this list. The link between the two data definitions is established through the CDS view entity name after key word EXTEND VIEW ENTITY.

Note

The CDS view entity extension has to specify the data source name when addressing fields or elements. In the example, it uses Agency which is defined in CDS view entity /DMO/E_Agency as an alias name for database table /DMO/AGENCY.

CDS view extensions can also define additional associations. Depending on the extensibility settings (see below), they can expose these associations or use them in path expressions in the element list.

In this example, the CDS view extension defines association _ZZTextZAG and adds it to the element list of CDS view entity /DMO/E_Agency.

Extensibility Control for CDS Entities

Extensibility control for CDS entities is very similar to the extensibility control for dictionary objects. Most of the annotations have a similar syntax and meaning. For a complete documentation, refer to the ABAP keyword documentation.

There are some minor differences though, as shown here:

  • The annotations start with @ABAPCatalog.extensibility. In CDS, they start with @ABAPCatalog.enhancement.
  • There is a simple true/false annotation to allow or disallow extensibility.

    Note

    The annotation AbapCatalog.viewEnhancementCategory specifies how a CDS view can be extended.

    AbapCatalog.viewEnhancementCategory and AbapCatalog.extensibility.extensible can optionally be specified together in the same CDS view. In this case, the annotation values must fit to each other. They must both either allow or forbid extensions.

  • It is not possible to distribute the quota share between customers and partners.
  • You can define an elementSuffix and not a fieldSuffix, because the element list of a CDS entity can consist of more than just fields. Exposed associations are an example.

The most important difference are subannotations allowNewDatasources and dataSources, which have no equivalent in the ABAP dictionary. With these annotations, the owner of a CDS view entity can control from where the extensions can read their data. If allowNewDatasources is set to true, the developer of an extension can read from any data source the original view reads from. If it is set to false, the developer of the extension can only read from the data sources that are listed under dataSources. This can be the direct data source(s) of the original view or the targets of its associations.

SAP often uses this to enforce the use of extension includes, which are a best practice for CDS view extensions.

Extension includes are a best practice for CDS view extensions. The name is a bit misleading because ABAP CDS does not support an include technique like the ABAP dictionary. What SAP uses instead are special CDS view entities, usually identifiable by the prefix "E_". The standard CDS view entity, for example, a view with prefix "R_", is defined with an association to such an E-view.

On the figure, the CDS view definition on the top left contains an association to the extension include view on the bottom left. Both CDS views are extendable, but to enforce the use of the E-view, the main view restricts extensibility to elements of the association to the E-view. The E-view on the other hand, allows extension with the elements of the main data source. As a consequence, extension developers, have to add new elements to the E-view first, before they can add them to the main view.

Let's have a look at an example.

This example illustrates how to extend CDS view entity /DMO/R_AgencyTP (on the top left) with the ZZCATEGORYZAG field from the database table /DMO/AGENCY.

It is not possible to do the extension directly, because CDS view entity /DMO/R_AgencyTP restricts the extension to association _Extension, which has CDS view entity /DMO/E_Agency as target (bottom left).

Therefore, in a first step, the developer extends /DMO/E_Agency (bottom right) and only then extends /DMO/R_AgencyTP addressing element ZZCategoryZAG through a path expression.

CDS View Extension Creation

Like CDS views, CDS view extensions are defined in CDS data definitions.

To define a CDS view extension, proceed as follows:

  1. In the project explorer, locate the data definition of the CDS entity that you want to extend.
  2. Right-click the data definition and choose New Data Definition.
  3. Adjust the package, enter a name and a description for the new object. Then choose Next.

    Note

    Usually, the data definition with the extension does not lie in the same package as the base object. Therefore, it is important to adjust the suggested package.

  4. Enter a transport request and choose Next.
  5. From the list of templates, choose Extend View Entity and choose Finish.

    There is also an Extend View template. It belongs to an older generation of CDS views. Only use this template when you have to extend a view that is defined with define view and not with define view entity.

Extend CDS View Entities

You want to extend CDS view entities with additional elements and associations. Instead of editing the data definitions directly, you create CDS view extensions.

Note

In this exercise, you extend your own CDS view entities Z##_R_Employee and Z##_C_EmployeeQueryP, where ## is your group number. This is done for simplicity. To motivate the use of extensions, you have to pretend that the view entities lie in a different name space and that you are not authorized to make direct changes to them.

Template:

  • n.a.

Solution:

  • /LRN/EXT_R_EMPLOYEE ( Data Definition)
  • /LRN/EXT_C_EMPLOYEE ( Data Definition)

Prerequisites

For this exercise, you need the base view entity for employee data (suggested name was: Z##_R_Employee) that developed in previous exercises. If you have not finished those exercises, create a copy of data definition /LRN/R_EMPLOYEE_AUT.

In addition, you need the view entity with parameters for employee data (suggested name was: Z##_C_EmployeeQueryP) . If you have not finished the related exercises, create a copy of data definition /LRN/C_EMPLOYEE_AUT and let it read from your base view entity.

Task 1: Enable Extensibility

Adjust the extensibility settings of your base view entity for employee data (suggested name was: Z##_R_Employee , where ## is your group number). Allow the extension of the element list with fields that come from your database table for employee data (suggested name was Z##_EMPLOY).

In the same way, adjust the extensibility settings of your view entity with parameters for employee data (suggested name was: Z##_C_EmployeeQueryP , where ## is your group number). Allow the extension of the element list with elements that come from your base view entity (suggested name was Z##_R_Employee).

Steps

  1. Open the definition of your base view entity for employee data.

    1. Press Ctrl + Shift + A, enter the name of the data definition and choose OK.

  2. Adjust the view extension category to allow the extension of the element list.

    1. Change the value of annotation @ABAPCatalog.viewEnhancementCategory from #NONE to #PROJECTION_LIST.

  3. Maintain the list of allowed data sources for the extension. Add the database table from which the view entity reads to this list.

    Note

    To add the database table to the list, you need to define an alias name for it (suggested alias name: Employee).
    1. Add the @ABAPCatalog.extensibility.dataSources annotation as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      123456
      @AbapCatalog: { dataMaintenance: #RESTRICTED, viewEnhancementCategory: [] }
    2. Define the alias for the data source as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      123
      define view entity Z##_R_Employee as select from z##employ
  4. Define a mandatory suffix for the additional fields (suggested value: ZEM).

    1. Add annotation @ABAPCatalog.extensibility.elementSuffix as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      1234567
      @AbapCatalog: { dataMaintenance: #RESTRICTED, viewEnhancementCategory: [#PROJECTION_LIST], extensibility.dataSources: [ 'Employee' ] }
  5. Activate the data definition.

    1. Press Ctrl + F3 to activate the development object.

  6. Open the definition of your view entity with parameters for employee data.

    1. Press Ctrl + Shift + A, enter the name of the data definition and choose OK.

  7. As before, adjust the view extension category, maintain the allowed data sources, and define the element suffix.

    Note

    Use the same data source alias and element suffix as before.
    1. Adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      123456
      @AbapCatalog: { dataMaintenance: #RESTRICTED, viewEnhancementCategory: [] }
    2. Define the alias for the data source as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      12345678
      define view entity Z##_C_EmployeeQueryP with parameters p_target_curr : /dmo/currency_code, @EndUserText.label: 'Date of evaluation' @Environment.systemField: #SYSTEM_DATE p_date : abap.dats as select from Z##_R_Employee
  8. Activate the data definition.

    1. Press Ctrl + F3 to activate the development object.

Task 2: Extend the Base View Entity

For your base view entity, create a CDS view extension (suggested name: Z##EXT_R_EMPLOYEE) to add the fields for the employee's title and the country to the element list (suggested names were: ZZTITLE_ZEM and ZZCOUNTRY_ZEM).

Steps

  1. Create a CDS view extension for your CDS view entity Z##_R_Employee.

    1. In the Project Explorer, locate data definition Z##_R_EMPLOYEE, right-click it and choose New Data Definition.

    2. Ensure that the Package field contains the name of your own package.

    3. Enter Z##EXT_R_Employee in Name and Extension of Z##_R_Employee in Description. Then choose Next.

    4. Confirm the transport request and choose Next.

      Note

      Do not choose Finish yet or you will not be able to choose the correct template.
    5. From the list of Templates, choose Extend View Entity and choose Finish.

  2. Edit the element list of the view extension. Remove the placeholder and use code-completion to add the employee's title and country.

    1. In the source code, replace base_data_source_code.element_name. with zz and press Ctrl + Space to invoke code-completion.

    2. From the suggestion list, choose zztitle_zem- z##employ as employee (column) to add the append field to the element list of the CDS view extension.

    3. Repeat to adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      12345
      extend view entity Z##_R_Employee with { }
  3. Define alias names for the new view elements (suggested names: ZZTitleZem and ZZCoutryZem).

    1. Adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      1234567
      extend view entity Z##_R_Employee with { employ.zztitle_zem , employ.zzcountry_zem }
  4. Activate the view extension.

    1. Press Ctrl + F3 to activate the development object.

  5. Display the Tooltip Description for the extended view entity to confirm that it contains the new elements.

    1. In the code row that begins with extend view entity, place the cursor on z##_R_Employee and press F2.

Task 3: Extend the Query View Entity

For your query view entity with parameters, create a CDS view extension (suggested name: Z##EXT_C_EMPLOYEE) to add the elements that you added to the base view (suggested elements names were: ZZTitleZem and ZZCountryZem). Also add an SQL function to concatenate the employee's first name and last name, separated by a single blank (suggested element name: ZZFullNameZem). Finally, extend the view with a flag that tells you if the employee is based in the European Union or not (suggested element name: ZZEUBasedZem).

Hint

The country master data contain a flag IsEuropeanUnionMember. To access this element, you first have to extend the view with an association to CDS view entity I_Country (suggested association name: _ZZCountryZem). Then add a path expression to the element list of the view extension.

Steps

  1. Create a CDS view extension for your CDS view entity Z##_C_EmployeeQueryP.

    1. In the Project Explorer, locate data definition Z##_C_EMPLOYEEQUERYP, right-click it and choose New Data Definition.

    2. Ensure that the Package field contains the name of your own package.

    3. Enter Z##EXT_C_Employee in Name and Extension of Z##_C_EmployeeQueryP in Description. Then choose Next.

    4. Confirm the transport request and choose Next.

      Note

      Do not choose Finish yet or you will not be able to choose the correct template.
    5. From the list of Templates, choose Extend View Entity and choose Finish.

  2. Edit the element list of the view extension. Remove the placeholder and use code-completion to add the employee's title and country.

    1. In the source code, replace base_data_source_code.element_name. with zz and press Ctrl + Space to invoke code-completion.

    2. From the suggestion list, choose ZZTitleZem - Z##_R_Employ as Employee (column) to add the extension field to the element list.

    3. Repeat to adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      12345
      extend view entity Z##_C_EmployeeQueryP with { }
  3. Use the concat_with_space function to concatenate the employee's first name and last name, separated by a single blank.

    1. Adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      12345678
      extend view entity Z##_C_EmployeeQueryP with { Employee.ZZTitleZem, Employee.ZZContryZem, }
  4. Add a name for this new element (suggested name: ZZFullNameZem) and use the relevant annotation to provide a label.

    1. Adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      12345678910
      extend view entity Z##_C_EmployeeQueryP with { Employee.ZZTitleZem, Employee.ZZContryZem, @ concat_with_space( Employee.FirstName, Employee.LastName, 1 ) }
  5. Extend the view with an association to CDS view entity I_Country (suggested name: _ZZCountryZem).

    Hint

    In the ON condition, use the country code from the element list and the key element of view entity I_Country.
    1. Adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      1234567891011
      extend view entity Z##_C_EmployeeQueryP with { Employee.ZZTitleZem, Employee.ZZContryZem, concat_with_space( Employee.FirstName, Employee.LastName, 1 ) as ZZFullNameZem }
  6. In the element list, add the IsEuropeanUnionMember element of the association target (suggested name: ZZEUBasedZem).

    1. Adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      12345678910111213
      extend view entity Z##_C_EmployeeQueryP with association [1..1] to I_Country as _ZZCountryZem on $projection.ZZCountryZem = _ZZCountryZem.Country { Employee.ZZTitleZem, Employee.ZZContryZem, concat_with_space( Employee.FirstName, Employee.LastName, 1 ) as ZZFullNameZem }
  7. Activate the view extension and display the Tooltip Description for the extended view entity to confirm that it contains the new elements.

    1. Press Ctrl + F3 to activate the development object.

    2. In the code row that begins with extend view entity, place the cursor on z##_C_EmployeeQueryP and press F2.

Log in to track your progress & complete quizzes