Extending Dictionary Objects

Objective

After completing this lesson, you will be able to Extend dictionary objects using append structures.

Append Structures

You already learned that a database table definition in the ABAP dictionary corresponds to a physical table on the database. Changes to the dictionary object are conveyed to the database object during activation (ALTER TABLE) or through the database adjustment procedure.

Append Structures are a technique that make it possible to add fields to a database table without actually changing the table definition in the ABAP dictionary.

Let us consider a database table that is defined by a software provider, for example, SAP. A customer or partner wants to add fields to this database table.

Instead of adding the fields to the database table definition in the dictionary, they create an append structure. The append structure is a separate dictionary object, owned and maintained by the customer or partner. Technically, it is a structure type in the ABAP dictionary. However, it is linked to database table definition and defines additional fields for the related database table. When the append structure is activated, its components are added as fields to the table on the database.

You can say that the table on the database is defined by two dictionary objects: The database table definition and the append structure.

Note

One append structure belongs to exactly one database table . One database table, however, can have several append structures.

In ABAP Cloud, you can only create an append if the software provider released the table definition for extension.

But what happens if the software provider ships a new version of the database table definition?

Let us consider that in a future release, the database table will contain an additional field. The solution is simple: during the upgrade, the new field is simply added to the field catalog of the database table, using the ALTER TABLE statement. The different field sequence on the database and in the dictionary is not a problem for the ABAP environment.

Here is an example for an append structure.

The code on the left shows the definition of database table /DMO/AGENCY. The code on the right shows the definition of append structure Z00_EXT_AGENCY. The link between the two dictionary objects is established in the append structure by mentioning the database table after keyword EXTEND TYPE.

The append structure has one component zzcategory. It is added to the field list of database table /DMO/AGENCY.

There is a reason why the component of the append structure starts with "ZZ". Table fields and structure components starting with "ZZ" or "YY" lie in the customer namespace. SAP guarantees that tables shipped by SAP will never contain fields in the customer namespace. By choosing field names in the customer namespace, customers can avoid field name conflicts if SAP adds new fields to the target table.

The append technique not only works for database table definitions. You can also use append structures to add components to a structure type.

In this example, the append structure on the right extends dictionary structure /DMO/S_EXT_INCL_AGENCY with a field ZZCATEGORYZAG.

Extensibility Control for Dictionary Objects

To avoid later conflicts, the owner of a database table definition or structure type can restrict extensibility or even disallow it.

The software provider controls extensibility using the following subannotations of annotation @ABAPCatalog.enhancement:

category

Defines the enhancement category. The following values can be specified:

  • #NOT_CLASSIFIED - Not classified
  • #NOT_EXTENSIBLE - Cannot be enhanced
  • #EXTENSIBLE_CHARACTER - Can be enhanced (character-like)
  • #EXTENSIBLE_CHARACTER_NUMERIC - Can be enhanced (character-like or numeric)
  • #EXTENSIBLE_ANY - Can be enhanced (deep - only for structures)
fieldSuffix

Defines a 3-character element suffix. This suffix is to be used for all fields of an extension created by customers or partners. The purpose is to avoid field naming clashes.

quotaMaximumFields

Defines the maximum number of fields that can be appended to the corresponding object by customers and partners. In other words, it reserves a field count for customer and partner extensions.

quotaMaximumBytes

Defines the maximum byte capacity that can be appended to the corresponding object by customers and partners. In other words, it reserves a byte capacity for customer and partner extensions.

Note

For the complete documentation of extensibility annotations, refer to the ABAP keyword documentation.

SAP often uses extension control to enforce the use of extension includes.

Extension includes are specific structure types that are included in one or several database table definitions. If customers or partners want to add fields to the table, they have to create an append structure for this extension include rather than a direct append for the database table.

The main benefits are as follows:

  • The software provider can use the same extension include in several database table definitions and structure types. This ensures that additional fields are added to several tables in a consistent way.
  • The software provider can decide to release only the extension include and not the database table definition itself.

Let us have a look at an example for an indirect extension:

The append structure on the right does not extend data base table /DMO/AGENCY, directly. Instead, it extends structure type /DMO/S_EXT_INCL_AGENCY. As the structure is used as include structure in database table /DMO/AGENCY, any field added to the structure us also added to the database table.

Append Structure Creation

Creating an Append Structure

Play the video to see how to create an append structure.

How to Extend a Database Table Definition

Play the video to see how to extend a database table definition.

Extend a Database Table Definition

You want to extend a database table with additional fields. Instead of editing the database table definition directly, you create an APPEND structure.

Note

In this exercise, you extend your own database table for employee data (suggested name was: Z##EMPLOY, where ## is your group number). This is done for simplicity. To motivate the use of the append technique, you have to pretend that the database table lies in a different name space and that you are not authorized to make direct changes to it.

Template:

  • n.a.

Solution:

  • /LRN/S_EXT_EMPLOYEE ( Structure Type)

Prerequisites

For this exercise, you need the database table definition for employee data that you created in a previous exercise (suggested name was: Z##EMPLOY, where ## is your group number). If you have not created that database table, create it now as a copy of database table definition /LRN/EMPLOY_REL.

Task 1: Enable Extensibility

Adjust the extensibility settings of your database table for employee data (suggested name was: Z##EMPLOY, where ## is your group number). Allow the extension with fields of flat data types, that is char-like and numeric fields. Enforce that the names of additional fields end with the same letters (suggested value: ZEM).

Steps

  1. Open the definition of your database table for employee data.

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

  2. Adjust the extension category to allow the extension with char-like and numeric fields.

    1. Change the value of annotation @ABAPCatalog.enhancement.category from #NOT_EXTENSIBLE to #EXTENSIBLE_CHARACTER_NUMERIC.

  3. Define a mandatory suffix for the additional fields (suggested value: ZEM).

    1. Add annotation @ABAPCatalog.enhancement.fieldSuffix.

    2. Adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      12
      @AbapCatalog.enhancement.category :
  4. Activate the database table.

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

Task 2: Create Append Structure

Create an append structure (suggested name: Z##_S_EXT_EMPLOYEE, where ## is your group number) to extend your database table for employee data (suggested name was: Z##EMPLOY). Add a field for the employee's title (suggested name: ZZTITLE_ZEM) and a field for the country where the employee is based (suggested name: ZZCOUNTRY_ZEM). Type the new fields with data elements /DMO/TITLE and LAND1.

Steps

  1. Use a quick fix to create an append structure for this table (suggested name: Z##_S_EXT_EMPLOYEE, where ## is your group number) .

    1. Place the cursor on the name of the database table and press Ctrl + 1 to invoke the quick fix.

    2. From the list of available quick fixes, choose Create append structure for ....

    3. Under Name, enter Z##_S_EXT_EMPLOYEE and under Description, enter Employee (Append Structure), then choose Next.

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

  2. In the component list of the append structure, define a new component (suggested name: zztitle) with data element /DMO/TITLE as component type.

    1. Adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      1234
      extend type z##employ with z##_s_ext_employee { }
  3. Define a second component (suggested name: zzcountry_zem) with data element LAND1 as its component type.

    1. Adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      12345
      extend type z##employ with z##_s_ext_employee { zztitle_zem : /dmo/title; }
  4. Activate the append structure.

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

  5. Display the Tooltip Description for the extended database table to confirm that the table contains the new fields.

    1. In the code row that begins with extend type, place the cursor on z##employ and press F2.

Task 3: Fill Extension Fields

In your employee table, fill the extension fields. To do so, create a copy of the ABAP class /LRN/CL_S4D430_CHECK_AND_FILL and adjust the values of constants c_version and c_employ_table. Then activate and execute the class as a console app.

Steps

  1. Create a copy of ABAP class /LRN/CL_S4D430_CHECK_AND_FILL, name it Z##_S4D430_CHECK_AND_FILL and place it in your package ZS4D430_##.

    Note

    Skip this step if you created a copy of /LRN/CL_S4D430_CHECK_AND_FILL in an earlier exercise.
    1. In the Project Explorer view, locate the ABAP class in the /LRN/S4D430_EXERCISE package and right-click it to open the context menu.

    2. From the context menu, choose Duplicate.

    3. Enter the name of your package, the name for the copy and a description. Then choose Next.

    4. Assign the new object to a transport request and choose Finish.

  2. Change the value of constant c_version to lcl_generator=>with_extensions.

    Hint

    You can add a comment sign in front of the current definition and remove the comment sign from the second definition alternative.
    1. Adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      12345
      * CONSTANTS c_version TYPE t_version VALUE lcl_generator=>employee_table_only. * CONSTANTS c_version TYPE t_version VALUE lcl_generator=>with_relationships. CONSTANTS c_version TYPE t_version VALUE lcl_generator=>with_extensions.
  3. If necessary, change the value of the c_employ_table constant to the name of your database table.

    Note

    The c_depment_table constant is not needed in this version of the code.
    1. Scroll down to the code row starting with CONSTANTS c_employ_table.

    2. If the literal after VALUE still contains ##, replace it with your group number.

  4. Activate the ABAP class and execute it as a console app. If the console output contains errors, adjust the definition of your database table for employees until it is compatible with the database table definition /LRN/EMPLOY_EXT.

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

    2. Press F9 to execute the ABAP class as console app.

    3. Make sure the console output contains the text Filled table ... for the database table.

Log in to track your progress & complete quizzes