
In earlier ABAP days, it was never very complicated to examine the data in use in a program. The data was all in variables, field symbols, or internal tables.
In today's ABAP, however, it can be time-consuming to check the data that a program is using. Rather than lying relatively exposed in an internal table, the data you wish to see may be encapsulated in instances of ABAP objects. Objects may further be nested within each other, as in a customer object with an embedded address object. But In such cases, displaying data requires opening an object and displaying attributes.
The Data Explorer in the debugger does make it easy to look at the data in one or a few such encapsulated containers, but even with the help of the Data Explorer, there is no longer a fast and easy way to review data in use, at least, not if mass data is involved.
Here again, scripts in the new ABAP debugger offer fast help.
The sample script below assumes a customer object with an embedded account object. The script generates an ALV list of the data held in the customer and account objects in a program.
*---------------------------------------------------------------------*
* CLASS lcl_debugger_script DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_debugger_script DEFINITION INHERITING FROM cl_tpda_script_class_super .
PUBLIC SECTION.
METHODS: prologue REDEFINITION,
init REDEFINITION,
script REDEFINITION,
end REDEFINITION.
PRIVATE SECTION.
* Example types for the data to be extracted and displayed
TYPES ty_amount(16) TYPE p DECIMALS 2.
TYPES:
BEGIN OF ty_customer,
name TYPE string,
street TYPE string,
city TYPE string,
accountno TYPE string,
accountbalance TYPE ty_amount,
END OF ty_customer.
DATA it_customer_info TYPE STANDARD TABLE OF ty_customer.
DATA tablename TYPE string.
ENDCLASS. "lcl_debugger_script DEFINITION
*---------------------------------------------------------------------*
* CLASS lcl_debugger_script IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_debugger_script IMPLEMENTATION.
METHOD prologue.
* generate abap_source (source handler for ABAP)
super->prologue( ).
ENDMETHOD. "prolog
METHOD init.
* Name of a table containing customer representations in ABAP objects
tablename = 'IT_CUSTOMERS'.
ENDMETHOD. "init
METHOD script.
* Variables for metadata on the table from which data is to be
* extracted in the program that is being debugged.
DATA: table_descr TYPE REF TO cl_tpda_script_tabledescr,
content TYPE tpda_scr_table_content_it ,
table_clone TYPE REF TO data,
l_cols_it TYPE tpda_scr_table_comp_it,
l_col_alv_it TYPE tpda_script_service_source_tab,
l_col_alv LIKE LINE OF l_col_alv_it,
l_off TYPE i.
FIELD-SYMBOLS: <table_clone> TYPE table.
FIELD-SYMBOLS: <l_col> LIKE LINE OF l_cols_it.
FIELD-SYMBOLS: <l_comp> TYPE any.
FIELD-SYMBOLS: <l_clone> TYPE any.
DATA l_comp_long TYPE string.
DATA wa_customer TYPE ty_customer.
* Obtain the table metadata via the script reflection interface, available
* in Script Wizard functions.
* First get a handle for accessing the table from the script.
****************************************************************
*Interface (CLASS = CL_TPDA_SCRIPT_DATA_DESCR / METHOD = FACTORY )
*Importing
* REFERENCE( P_VAR_NAME ) TYPE TPDA_VAR_NAME
*Returning
* VALUE( P_DATADESCR ) TYPE REF TO CL_TPDA_SCRIPT_DATA_DESCR
****************************************************************
table_descr = cl_tpda_script_data_descr=>factory(
p_var_name = tablename ).
* Clone the customer object table from the debugged program into the script session
****************************************************************
*Interface (CLASS = CL_TPDA_SCRIPT_TABLEDESCR / METHOD = ELEM_CLONE )
*Importing
* REFERENCE( P_KEYS ) TYPE TPDA_TABLE_VIEW_KEY_COLS_IT OPTIONAL
* REFERENCE( P_UNIQUE ) TYPE FLAG OPTIONAL
* REFERENCE( P_TABLE_KIND ) TYPE I OPTIONAL
* REFERENCE( P_STRING_CLONE ) TYPE FLAG OPTIONAL
*Returning
* VALUE( P_REF_CLONE ) TYPE REF TO DATA OPTIONAL
****************************************************************
table_clone = table_descr->elem_clone( ).
ASSIGN table_clone->* TO <table_clone>.
* Loop through the customer objects and construct the attribute names needed by
* the get_simple_value method of cl_tpda_script_data_descr.
LOOP AT <table_clone> ASSIGNING <l_clone>.
CLEAR wa_customer.
ASSIGN COMPONENT 2 OF STRUCTURE <l_clone> TO <l_comp>.
IF sy-subrc = 0.
FIND '{' IN <l_comp> MATCH OFFSET l_off.
IF sy-subrc = 0.
<l_comp> = <l_comp>+l_off(*).
ELSE.
RAISE EXCEPTION TYPE cx_tpda_varname.
ENDIF.
* Get values from object references using a short form of the
* script wizard function 'Variable value (for simple variables)
CONCATENATE <l_comp> '-ADDRESS-NAME' INTO l_comp_long.
wa_customer-name = cl_tpda_script_data_descr=>get_simple_value(
p_var_name = l_comp_long ).
CONCATENATE <l_comp> '-ADDRESS-STREET' INTO l_comp_long.
wa_customer-street = cl_tpda_script_data_descr=>get_simple_value(
p_var_name = l_comp_long ).
CONCATENATE <l_comp> '-ADDRESS-CITY' INTO l_comp_long.
wa_customer-city = cl_tpda_script_data_descr=>get_simple_value(
p_var_name = l_comp_long ).
CONCATENATE <l_comp> '-ACCOUNT->ACCOUNTNO' INTO l_comp_long.
wa_customer-accountno = cl_tpda_script_data_descr=>get_simple_value(
p_var_name = l_comp_long ).
CONCATENATE <l_comp> '-ACCOUNT->ACCOUNTBALANCE' INTO l_comp_long.
wa_customer-accountbalance = cl_tpda_script_data_descr=>get_simple_value(
p_var_name = l_comp_long ).
APPEND wa_customer TO it_customer_info.
ENDIF.
ENDLOOP.
* Set up the ALV table for displaying the data.
CLEAR l_col_alv_it.
l_col_alv-fieldname = l_col_alv-content = 'NAME'.
APPEND l_col_alv TO l_col_alv_it.
l_col_alv-fieldname = l_col_alv-content = 'STREET'.
APPEND l_col_alv TO l_col_alv_it.
l_col_alv-fieldname = l_col_alv-content = 'CITY'.
APPEND l_col_alv TO l_col_alv_it.
l_col_alv-fieldname = l_col_alv-content = 'ACCOUNTNO'.
APPEND l_col_alv TO l_col_alv_it.
l_col_alv-fieldname = l_col_alv-content = 'ACCOUNTBALANCE'.
APPEND l_col_alv TO l_col_alv_it.
* Give the data to the script wizard ALV function for display
****************************************************************
*Interface (CLASS = CL_TPDA_SCRIPT_DATA_DISPLAY / METHOD = DATA_DISPLAY )
*Importing
* REFERENCE( P_SET_POPUP ) TYPE TPDA_SCRIPT_SET_SCREEN_POPUP OPTIONAL
* REFERENCE( P_LIST_HEADER ) TYPE LVC_TITLE OPTIONAL
* REFERENCE( P_COLUMN_IT ) TYPE TPDA_SCRIPT_SERVICE_SOURCE_TAB OPTIONAL
* REFERENCE( P_POPUP ) TYPE FLAG OPTIONAL
*Changing
* REFERENCE( P_DATA_IT ) TYPE ANY TABLE OPTIONAL
****************************************************************
cl_tpda_script_data_display=>data_display(
p_column_it = l_col_alv_it
p_data_it = it_customer_info ).
ENDMETHOD. "script
ENDCLASS. "lcl_debugger_script IMPLEMENTATION