You need some table types that you can access in several global classes. You decide to define table types in the ABAP Dictionary.
Hint
Before you define table types in the ABAP Dictionary, you should investigate the option to define public table types in one of the global classes or in a global interface.Task 1: Define a Table Type
Create a copy of global class /LRN/CL_S4D430_TYT_TABLE_TYPE (suggested name: ZCL_##_TABLE_TYPE, where ## is your group number). Replace public table type tt_addresses with a dictionary table type (suggested name: Z##T_ADDRESSES).
Steps
Copy class /LRN/CL_S4D430_TYT_TABLE_TYPE to a class in your own package (suggested name: ZCL_##_TABLE_TYPE, where ## stands for your group number).
In the Project Explorer view, right-click class /LRN/CL_S4D430_TYT_TABLE_TYPE to open the context menu.
From the context menu, choose Duplicate ....
Enter the name of your package in the Package field. In the Name field, enter the name ZCL_##_TABLE_TYPE, where ## stands for your group number.
Adjust the description and choose Next.
Confirm the transport request and choose Finish.
In the PUBLIC SECTION of the class definition, analyze the definition of type tt_addresses.
Perform this step as before.
Create a new dictionary object of type Table Type (suggested name: Z##T_ADDRESSES).
In the Project Explorer view, expand your package and open the context menu on subnode Dictionary.
Choose New → Table Type.
Confirm the package, enter the name Z##T_ADDRESSES and the description Addresses, then choose Next.
Assign the new object to a transport request and choose Finish.
Specify your structure type Z##S_ADDRESS as row type.
Note
If you have not finished the previous exercise, you can use the structure type /LRN/S_ADDRESS, instead.In the Row Type section, change the Category from Predefined Type to Dictionary.
In the Type Name field, enter /LRN/S_ADDRESS or Z##S_ADDRESS.
Specify the same table kind and key definition as in table type tt_addresses.
In the Initialization and Access section, change the table kind from Standard Table to Sorted Table.
In the Key Overview section, select <Primary Key>.
In the Primary Key Details section, change the Key Definition from Standard Key to Key Components.
In the Key Components section, use code completion to enter COUNTRY and CITY as key fields.
Activate the table type.
Press Ctrl + F3 to activate the development object.
Return to the source ode of your global class. In the implementation of the IF_OO_ADT_CLASSRUN~MAIN method, replace tt_addresses with your new dictionary structure type. Make sure there are no syntax errors.
Adjust the code as follows:
12345678910111213141516171819
* Task 1
**********************************************************************
* DATA addresses TYPE tt_addresses.
DATA addresses TYPE z##t_addresses.
addresses =
VALUE #(
( street = 'Dietmar-Hopp-Allee 16'
postal_code = '69190'
city = 'Walldorf'
country = 'DE'
)
( street = '3999 West Chester Pike'
postal_code = '19073'
city = 'Newtown Square, PA'
country = 'US'
)
).
Task 2: Define a Deep Structure
In your global class, remove or comment public structure type st_person_deep and replace it with a dictionary structure (suggested name: Z##S_PERSON_DEEP). For the addresses, re-use the table type you already created.
Steps
Create a new dictionary object of type Structure.
In the Project Explorer view, expand your package and open the context menu on subnode Dictionary.
Choose New → Structure.
Confirm the package, enter the name Z##S_PERSON_DEEP and the description Person (Name and Several Addresses), then choose Next.
Assign the new object to a transport request and choose Finish.
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), the last name (suggested name: last_name).
Adjust the code as follows:
1234567
define structure z##s_person_deep {
first_name : /dmo/first_name;
last_name : /dmo/last_name;
}
Add another component for the addresses of the person (suggested name: addresses). As type, use the table type you created in the previous task.
Adjust the code as follows:
12345678
define structure z##s_person_deep {
first_name : /dmo/first_name;
last_name : /dmo/last_name;
addresses : z##t_addresses;
}
Activate the structure type.
Press Ctrl + F3 to activate the development object.
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_deep.
Perform this step as before.
In the implementation of the IF_OO_ADT_CLASSRUN~MAIN method, replace st_person_deep with your new dictionary structure type. Make sure there are no syntax errors.
Adjust the code as follows:
12345678910
* Task 2
**********************************************************************
* DATA person TYPE st_person_deep.
DATA person TYPE /lrn/s_person_deep.
person-first_name = 'Dictionary'.
person-last_name = 'ABAP'.
person-addresses = addresses.
Task 3: Define a Nested Table Type
In your global class, remove or comment public table type tt_persons and replace it with a dictionary table type (suggested name: Z##T_PERSONS). As its row type, use the deep structure type you created in the previous task.
Steps
Create a new dictionary object of type Table Type.
In the Project Explorer view, expand your package and open the context menu on subnode Dictionary.
Choose New → Table Type.
Confirm the package, enter the name Z##T_PERSONS and the description Persons (With Name and Addresses), then choose Next.
Assign the new object to a transport request and choose Finish.
Specify your structure type Z##S_PERSON_DEEP as the row type.
In the Row Type section, change the Category from Predefined Type to Dictionary.
In the Type Name field, enter Z##S_PERSON_DEEP.
Specify the same table kind and key definition as in table type tt_persons.
In the Initialization and Access section, change the table kind from Standard table to Hashed Table.
In the Key Overview section, select <Primary Key>.
In the Primary Key Details section, change the Key Definition from Standard Key to Key Components and the Key Category from Non-Unique to Unique.
In the Key Components section, specify the key fields LAST_NAME and FIRST_NAME.
Activate the table type.
Press Ctrl + F3 to activate the development object.
Return to the source ode of your global class. In the PUBLIC SECTION of the class definition, remove or comment the definition of type tt_persons .
Perform this step as before.
In the implementation of the IF_OO_ADT_CLASSRUN~MAIN method, replace tt_persons with your new dictionary table type. Make sure there are no syntax errors.
Adjust the code as follows:
123456789101112131415161718192021222324252627282930313233
* Task 3
**********************************************************************
* DATA persons TYPE tt_persons.
DATA persons TYPE /lrn/t_persons.
persons =
VALUE #(
( person )
(
first_name = 'CDS'
last_name = 'ABAP'
addresses =
VALUE #(
( street = 'SAP-Allee 29'
postal_code = '68789'
city = 'St.Leon-Rot'
country = 'DE'
)
( street = '35 rue d''Alsace'
postal_code = '92300'
city = 'Levallois-Perret'
country = 'FR'
)
( street = 'Bedfont Road'
postal_code = 'TW14 8HD'
city = 'Feltham'
country = 'GB'
)
)
)
).
Activate and debug the class as a console app. Analyze the content of the data objects addresses, person, and persons.
Press Ctrl + F3 to activate the development object.
Press F9 to execute the class as console app.