Developing Data Model Extensions

Objectives

After completing this lesson, you will be able to:
  • Extend database tables.
  • Extend CDS views.

Append Structures

Steps to Create an Append Structure

To create an append structure, proceed as follows:

  1. In the project explorer, locate the dictionary object that you want to extend (structure type or database table definition).
  2. Right-click the object and choose New Append Structure.
  3. Adjust the package, enter a name and a description for the append structure, and choose Next.

Note

Usually, an append structure 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 chooseFinish.

Navigation to the Append Structure

When you want to know if there are any direct or indirect appends for a given database table or structure type, proceed as follows:

  1. Open the database table definition or structure type definition in the editor.
  2. Place the cursor on the table name or structure name and press F2 to open the tooltip description.
  3. Look for an Extended with section. It lists all direct or indirect appends to the object.
  4. Choose an append structure name to navigate to its definition.

CDS View Extensions

Definition of a CDS view extension

Like CDS view entities, 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.

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

Caution

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.

Example of a CDS view extension

The CDS view extensions extend the element list of their respective target view with more elements. If the target view contains the allowNewDatasources annotation with value false, they are restricted to the associations, views, and tables listed in the dataSources annotation.

The example shows how to add the zzcategoryzag append field from database table /DMO/Agency to the extension include view /DMO/E_Agency, from there to the data model view /DMO/R_AgencyTP, and finally to the projection view /DMO/C_AgencyTP.

Note

The example also illustrates, that the view extension on data model level can be used to add exposed associations and compositions to the data model. It makes it possible to extend the node hierarchy of a business object with extra nodes.

Develop Data Model Extensions

In this exercise, you develop an extension of the business object for flight travels and its projection. The goal of this extension is to have an additional booking class field for the flight travel items. To achieve this, you first extend the extension include structure Z##_S_EXT_TRITEM, which indirectly extends the database table for flight travel items and the related draft table.

After that, you extend the business object and its projection. You begin by extending the extension include view Z##_E_TravelItem because, in our case, the extension include view is the only allowed data source when extending the view entity on data model level Z##_R_TravelItem. Then you extend the view entity on data model level Z##_R_TravelItem and - based on that extension - the projection view entity Z##_C_TravelItem. Finally, you create a new metadata extension to add the booking class field to the UI metadata of the OData service.

Note

In this exercise, you extend your own data model objects. This is done for simplicity. To motivate the use of extensions, during this exercise, pretend that the database tables Z##_TRITEM and Z##_TRITEM_D, the extension include structure Z##_S_EXT_TRITEM, the CDS view entities Z##_R_TravelItem and Z##_C_TravelItem, and the extension include view Z##_E_TravelItem lie in a different namespace and that you are not authorized to make direct changes to these objects.

Note

In this exercise, replace ## with your group number.

Solution

Repository Object TypeRepository Object ID
Structure Type (Append Structure)/LRN/ZZX_S_TRITEM_CLASS
CDS Data Definition (Extension Include Extension)/LRN/ZZX_E_TRAVELITEM_CLASS
CDS Data Definition (Model Extension)/LRN/ZZX_R_TRAVELITEM_CLASS
CDS Data Definition (Projection Extension)/LRN/ZZX_C_TRAVELITEM_CLASS
CDS Metadata Extension (Projection)/LRN/ZZX_C_TRAVELITEM_CLASS

Task 1: Extend the Database Tables

Create an append structure (suggested name: Z##X_S_TRITEM_CLASS) for your extension include structure Z##_S_EXT_TRITEM. Add a field for the booking class (suggested name: ZZCLASSZ## ) and type the new field with data element /LRN/CLASS_ID.

Steps

  1. Locate your extension include structure in the Project Explorer.

    1. In the Project Explorer, expand the Favorite Packages node.

    2. Expand your own package ZS4D437_##.

    3. Under your package, expand DictionaryStructures.

  2. For the extension include structure, create an append structure (suggested name: Z##X_S_TRITEM_CLASS).

    1. In the Project Explorer, right-click Z##_S_EXT_TRITEM and select New Append Structure.

    2. On the New Append Structure dialog that opens, enter Z##X_S_TRITEM_CLASS in the Name field and Extend Travel Item with Booking Class in the Description field. Then choose Next.

    3. Assign the append structure to a transport request and choose Finish.

  3. In the component list of the append structure, define a new component (suggested name: zzclassz##) with data element /LRN/CLASS_ID as component type.

    1. Adjust the code as follows:

      Code Snippet
      123
      extend type z##_s_ext_tritem with z##x_s_tritem_class { zzclassz## : /lrn/class_id; }
  4. Activate the append structure.

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

  5. Open the definition of the database table for active flight travel items. Open the Tooltip Description to confirm that the table contains the new field.

    1. In the Project Explorer, under your own package, expand DictionaryDatabase Tables.

    2. Double-click Z##_TRITEM to open the definition of the database table.

    1. In the code row that begins with define table, place the cursor on z##_tritem and press F2.

    Result

    You see the new field at the end of the field list. You also see the name of your append structure in the Extended with section.
  6. Repeat this for the corresponding draft table to confirm that this table also contains the new field.

    1. In the Project Explorer, double-click Z##_TRITEM_D to open the definition of the draft table.

    1. In the code row that begins with define table, place the cursor on z##_tritem_d and press F2.

    Result

    You see the new field at the end of the field list. You also see the name of your append structure in the Extended with section.

Task 2: Extend the Data Model Definition

Create a CDS view extension to add the booking class field to the element list of the extension include view (suggested name: Z##X_E_TravelItem_Class). Then create a similar CDS view extension for the view entity on data model level (suggested name: Z##X_R_TravelItem_Class).

Steps

  1. In the Project Explorer, locate the CDS data definition that contains the definition of your extension include view.

    1. In the Project Explorer, expand the Favorite Packages node.

    2. Expand your own package ZS4D437_##.

    3. Under your package, expand Core Data ServicesData Definitions.

  2. For the extension include view, create a CDS view extension (suggested name: Z##X_E_TravelItem_Class).

    1. In the Project Explorer, right-click Z##_E_TRAVELITEM and select New Data Definition.

    2. In the Name field, enter Z##X_E_TravelItem_Class and in the Description field, enter Extend Travel Item with Booking Class. Then choose Next.

    3. Confirm the transport request and choose Next.

      Note

      Do not choose Finish yet or you will not be able to choose the correct template.
    4. From the list of templates, choose View Extend (creation)extendViewEntity and choose Finish.

  3. Edit the element list of the view extension. Remove the placeholder and use code completion to add the booking class field.

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

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

  4. To increase readability, define an alias name for the new view element (suggested name: ZZClassZ##) and activate the data definition.

    Caution

    Make sure that the alias name and the field name only differ in uppercase or lowercase!
    1. Adjust the code as follows:

      Code Snippet
      1234
      extend view entity Z##_E_TravelItem with { Item.zzclassz## as ZZClassZ## }
    2. Press Ctrl + F3 to activate the development object.

  5. For the flight travel item view on data model level Z##_R_TravelItem, create a CDS view extension (suggested name: Z##X_R_TravelItem_Class).

    1. In the Project Explorer, right-click Z##_R_TRAVELITEM and select New Data Definition.

    2. Under Name, enter Z##X_R_TravelItem_Class and under Description, enter Extend Travel Item with Booking Class. Then choose Next.

    3. Confirm the transport request and choose Next.

      Note

      Do not choose Finish yet or you will not be able to choose the correct template.
    4. From the list of templates, choose View Extend (creation)extendViewEntity and choose Finish.

  6. Edit the element list of the view extension. Remove the placeholder and use code completion to add the booking class field.

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

    2. From the suggestion list, choose ZZClassZ## - Z##_E_TravelItem as _Extension (column) to add the extension field to the element list of the CDS view extension.

    3. Make sure that the code looks like this:

      Code Snippet
      1234
      extend view entity Z##_R_TravelItem with { _Extension.ZZClassZ## }
  7. Activate the data definition.

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

Task 3: Extend the Data Model Projection

Create a CDS view extension to add the booking class field to the CDS view entity on projection level (suggested name: Z##X_C_TravelItem_Class). Then create a new metadata extension for the projection view (suggested name: Z##X_C_TravelItem_Class). Set the metadata layer to #CUSTOMER and add UI metadata for the booking class field.

Steps

  1. For the travel item view on projection level Z##_C_TravelItem, create a CDS view extension (suggested name: Z##X_C_TravelItem_Class).

    1. In the Project Explorer, right-click Z##_C_TRAVELITEM and select New Data Definition.

    2. Under Name, enter Z##X_C_TravelItem_Class and under Description, enter Extend Travel Item with Booking Class. Then choose Next.

    3. Confirm the transport request and choose Next.

      Note

      Do not choose Finish yet or you will not be able to choose the correct template.
    4. From the list of templates, choose View Extend (creation)extendViewEntity and choose Finish.

  2. Edit the element list of the view extension. Remove the placeholder and use code completion to add the booking class field.

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

    2. From the suggestion list, choose ZZClassZ## - Z##_R_TravelItem as Item (column) to add the extension field to the element list of the CDS view extension.

    3. Make sure that the code looks like this:

      Code Snippet
      1234
      extend view entity Z##_C_TravelItem with { Item.ZZClassZ## }
  3. Assign a value help to the new field, using /LRN/437_I_ClassStdVH as the data source.

    1. Adjust the code as follows:

      Code Snippet
      1234567
      extend view entity Z##_C_TravelItem with { @Consumption.valueHelpDefinition: [ { entity: { name: '/LRN/437_I_ClassStdVH', element: 'ClassID' } } ] Item.ZZClassZ## }
  4. Activate the data definition.

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

  5. For the flight travel item view on projection level Z##_C_TravelItem, create a CDS metadata extension (suggested name: Z##X_C_TRAVELITEM_CLASS).

    1. In the Project Explorer, right-click Z##_C_TRAVELITEM and select New Metadata Extension.

    2. Under Name, enter Z##X_C_TRAVELITEM_CLASS and under Description, enter Extend Travel Item with Booking Class. Then choose Next.

    3. Confirm the transport request and choose Next.

      Note

      Do not choose Finish yet or you will not be able to choose the correct template.
    4. From the list of templates, choose Annotate View (creation)annotateView and choose Finish.

  6. In the newly created metadata extension, set the metadata layer to #CUSTOMER.

    1. In the @Metadata.layer annotation, replace the placeholder layer with #CUSTOMER.

  7. Add UI metadata for the booking class field. Make sure, the new field appears on both, the list page and the object page, immediately after the BookingId field.

    Hint

    View element BookingId is annotated with @UI.lineItem.position: 60 and @UI.identification.position: 60.
    1. Adjust the code as follows:

      Code Snippet
      12345678
      @Metadata.layer: #CUSTOMER annotate view Z##_C_TravelItem with { @UI: { lineItem: [ { position: 65, importance: #HIGH } ], identification: [ { position: 65, importance: #HIGH } ] } ZZClassZ##; }
  8. Activate the metadata extension.

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

  9. Refresh the preview of the OData UI service and confirm that the booking class extension field is displayed.