Defining Dictionary Structures

Objectives

After completing this lesson, you will be able to:

  • Define dictionary structures

Dictionary Structures

Dictionary structures can be used as data types in the ABAP dictionary.

The structure on the left-hand side of our example consists of five components. The first four components reference various data elements. The last component is typed with predefined dictionary type CHAR, directly.

The ABAP code on the right-hand side defines exactly the same structure type using the TYPES statement. This code could be located in a method, in the definition part of a class, or in an interface. The first four components use the same data elements as the components in the dictionary structure. The last component uses built-in ABAP type C.

Apart from the extensibility and the label, the two structures are fully compatible.

Nested Structures

Nested structure types in ABAP dictionary

Play the video to learn about nested structure types in ABAP dictionary.

Note

When you type a data object with a nested structure type, you have to access its components as follows:

Code snippet
 
  gs_struct-address-post_code = '69190'.
  gs_struct-address-city      = 'Walldorf'.
Expand

It is not possible to access the sub-components of the address directly.

How to Define a Nested Structure

Play the video to see how to define a nested structure.

Include Structures

Nested structures must not be confused with structures that include another structure type.

Earlier in this course, you learned how to include a structure into the field list of a database table. The same technique is available when defining a structure type.

In the example, structure zs4d430s_person_include has a total of seven components: Components first_name and last_name are defined in the structure directly, while the other five components are added by including structure type zs4d430s_address.

The ABAP code on the right-hand side illustrates how you can define the same type using the TYPES statement in an ABAP class or interface.

Note
To understand this code, you have to be aware, that INCLUDE TYPES is a statement of its own and not an addition to statement TYPES. The TYPES chain statement is finished before the INCLUDE statement with a period sign (.) and a new TYPES chain statement begins after the INCLUDE statement.
Note

When you use such a structure type in ABAP, the include is not visible. You access the additional components as usual:

Code snippet

  gs_struct-first_name = 'John'.
  gs_struct-post_code  = '69190'.
  gs_struct-city       = 'Walldorf'.
Expand

Named Includes

When you include a structure into another structure, you can give the include a name. This include is then called a named include.

In the ABAP dictionary, you add the name before the keyword INCLUDE and separate the two by a colon sign (:). When you define your structure type in ABAP, you place the name after the name of the include structure, with keyword AS in front of it.

The named include establishes a kind of a field group that allows you to access the fields not only directly but also through the name of the include.

Let us consider a data object struct that is typed with structure type zs4d430s_person_include. You can access field city in two ways: Either as struct-city as you would with a normal include, or as struct-address-city.

Note
Named includes and field groups are used a lot in the derived data types of the ABAP RESTful application programming model.

How to Include Structures

Play the video to see how to include structures.

Define and Use Dictionary Structures

You need some structure types that you can access in several global classes. You decide to define structure types in the ABAP Dictionary.

Hint
Before you define structure types in the ABAP Dictionary, you should investigate the option to define public structure types in one of the global classes or in a global interface.

Template:

  • /LRN/CL_S4D430_TYT_STRUCTURE (Global Class)

Solution:

  • /LRN/CL_S4D430_TYS_STRUCTURE (Global Class)
  • /LRN/S_ADDRESS (Structure Type)
  • /LRN/S_NAME (Structure Type)
  • /LRN/S_PERSON (Structure Type)

Task 1: Define a Simple Structure

Create a copy of global class /LRN/CL_S4D430_TYT_STRUCTURE (suggested name: ZCL_##_STRUCTURE, where ## is your group number). Replace structure type st_address with a dictionary structure (suggested name: Z##S_ADDRESS).

Steps

  1. Copy class /LRN/CL_S4D430_TYT_STRUCTURE to a class in your own package (suggested name: ZCL_##_STRUCTURE, where ## is your group number).

    1. In the Project Explorer view, right-click class /LRN/CL_S4D430_TYT_STRUCTURE to open the context menu.

    2. From the context menu, choose Duplicate ....

    3. Enter the name of your package in the Package field. In the Name field, enter the name ZCL_##_STRUCTURE, where ## stands for your group number.

    4. Adjust the description and choose Next.

    5. Confirm the transport request and choose Finish.

  2. In the PUBLIC SECTION of the class definition, analyze the definition of type st_address.

    1. Perform this step as you have done earlier.

  3. Create a new dictionary object of type Structure (suggested name: Z##S_ADDRESS).

    1. In the Project Explorer view, expand your package and open the context menu on subnode Dictionary.

    2. Choose NewStructure.

    3. Confirm the package, enter the name Z##S_ADDRESS and the description Address, then choose Next.

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

  4. Remove the placeholder component_to_be_Changed : abap.string(0);from the component list of the structure. Then replace it with a component street. Use the same data element as in the st_address type in your global class.

    1. Adjust the code as follows:

      Code snippet
      
      define structure z##s_address {
      
      }
      
      Expand
  5. Add components for the postal code, the city, and the country. Use the same component names and data elements as in the st_address type in your global class.

    1. Adjust the code as follows:

      Code snippet
      
      define structure z##s_address {
        street      : /dmo/street;
      
      }
      
      Expand
  6. Activate the structure type.

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

  7. Return to the source code of your global class. In the implementation of the IF_OO_ADT_CLASSRUN~MAIN method, replace st_address with your new dictionary structure type. Make sure there are no syntax errors.

    1. Adjust the code as follows:

      Code snippet
      
      * Task 1
      *****************************************************************
      *    DATA address TYPE st_address.
      
          address-street      = 'Dietmar-Hopp-Allee 16'.
          address-postal_code = '69190'.
          address-city        = 'Walldorf'.
          address-country     = 'DE'.
      
      Expand

Task 2: Define a Nested Structure

In your global class, remove or comment the public structure type st_person and replace it with a dictionary structure (suggested name: Z##S_PERSON). For the address, re-use the structure type you already created. For the name, use a quick fix to create a second simple structure with two components (suggested name: Z##S_NAME).

Steps

  1. Create a new dictionary object of type structure.

    1. In the Project Explorer view, expand your package and open the context menu on the Dictionary subnode .

    2. Choose NewStructure.

    3. Confirm the package, enter the name Z##S_PERSON and the description Person (Name and Address), then choose Next.

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

  2. Remove the placeholder component_to_be_Changed : abap.string(0); from the component list of the structure and replace it with a component for the address (suggested name: address). Type the new component with the structure that you created in the previous task (Z##S_ADDRESS).

    1. Adjust the code as follows:

      Code snippet
      
      define structure z##s_person {
      
      }
      
      Expand
  3. Add another component for the name of the person (suggested name: Name). Enter Z##S_NAME as the name of the component type.

    Note
    This structure type does not yet exist. You create it in the next step.
    1. Adjust the code as follows:

      Code snippet
      
      define structure z##s_person {
      
      } 
      
      Expand
  4. Save the structure type. Then use a quick fix to create structure type Z##S_NAME.

    1. Press Ctrl + S to save the development object.

    2. Left-click on the error icon next to the code line you just added and choose Create Dictionary Structure ....

    3. Enter the description Name and choose Next.

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

  5. Remove the placeholder component_to_be_Changed : abap.string(0);from the component list of the structure and replace it with a component for the first name (suggested name: first_name) and a component for the last name (suggested name: last_name). Use suitable data elements from the /DMO/ name space as components types.

    1. Adjust the code as follows:

      Code snippet
      
      define structure z##s_name {
      
      }
      
      Expand
  6. Save the structure type. Then activate all inactive development objects.

    1. Press Ctrl + S to save the development object.

    2. From the eclipse toolbar, choose Activate inactive ABAP development objects. Alternatively, press Ctrl + Shift + F3.

    3. Choose Select All and Activate.

  7. Return to the source code of your global class. In the PUBLIC SECTION of the class definition, remove or comment the definition of the types st_name and st_person.

    1. Perform this step as before.

  8. In the implementation of the IF_OO_ADT_CLASSRUN~MAIN method, replace st_person with your new dictionary structure type. Make sure there are no syntax errors.

    1. Adjust the code as follows:

      Code snippet
      
      * Task 2
      ***********************************************************
      *    DATA person TYPE st_person.
          person-name-first_name     = 'Dictionary'.
          person-name-last_name      = 'ABAP'.
          person-address-street      = 'Dietmar-Hopp-Allee 16'.
          person-address-postal_code = '69190'.
          person-address-city        = 'Walldorf'.
          person-address-country     = 'DE'.
      
      Expand

Task 3: Define Named Includes

In your global class, remove or comment public structure type st_person_inc and replace it with a dictionary structure (suggested name: Z##S_PERSON_INC). Instead of structured components NAME and ADDRESS define named includes of the same name.

Steps

  1. Create a copy of your nested dictionary structure (suggested name: Z##S_PERSON_INC, where ## is your group number).

    1. In the Project Explorer view, expand your package and subnode DictionaryStructure.

    2. Open the context menu on structure type Z##S_PERSON and choose Duplicate.

    3. Enter the name Z##S_PERSON_INC and choose Next.

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

  2. Turn structured component name into a named include.

    1. After the colon that separates the component name and the component type, insert keyword INCLUDE.

  3. Repeat this for structured component address.

    1. Make sure the structure type now looks like this:

      Code snippet
      
      define structure z##s_person_inc {
        name    :  z##s_name;
        address :  z##s_address;
      }
      
      Expand
  4. Activate the structure type.

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

  5. Return to the source code of your global class. In the PUBLIC SECTION of the class definition, remove or comment the definition of type st_person_inc.

    1. Perform this step as before.

  6. In the implementation of the IF_OO_ADT_CLASSRUN~MAIN method, replace st_person_inc with your new dictionary structure type. Make sure both alternatives for accessing the components work.

    1. Adjust the code as follows:

      Code snippet
      
      
      * Task 3
      **********************************************************************
      * DATA person2 TYPE st_person_inc.
      
      person2-name-first_name = 'Dictionary'.
      person2-name-last_name = 'ABAP'.
      person2-address-street = 'Dietmar-Hopp-Allee 16'.
      person2-address-postal_code = '69190'.
      person2-address-city = 'Walldorf'.
      person2-address-country = 'DE'.
      * or -------------------------------------------------------
      person2-first_name = 'Dictionary'.
      person2-last_name = 'ABAP'.
      person2-street = 'Dietmar-Hopp-Allee 16'.
      person2-postal_code = '69190'.
      person2-city = 'Walldorf'.
      person2-country = 'DE'.
      
      Expand
  7. Activate and debug the class as a console app. Analyze the content of the data objects address, person, and person2.

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

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

Log in to track your progress & complete quizzes