Hello professionals,
I leveraged an old post and used that code to dynamically get the technical field names of a given table. I had hard time to publish the output into an ALV_GRID report. I added a third routine to do that, but that is throwing syntax error. any help will be appreciated.
Here is the original post : https://answers.sap.com/questions/5136838/how-to-get-the-structure-of-a-table-dynamically.html
Here is my modified code:
REPORT ZMY_ALV_4.
*parameters:
* p_infty type infty.
DATA: l_dbtab TYPE t777d-dbtab.
SELECT SINGLE dbtab
FROM t777d
INTO l_dbtab.
* WHERE infty = p_infty.
TYPE-POOLS : abap.
FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,
<dyn_wa>,
<dyn_field>.
DATA: dy_table TYPE REF TO data,
dy_line TYPE REF TO data,
xfc TYPE lvc_s_fcat,
ifc TYPE lvc_t_fcat.
START-OF-SELECTION.
PERFORM get_structure.
PERFORM create_dynamic_itab.
PERFORM display_fields_list.
*---------------------------------------------------------------------*
* FORM get_structure *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM get_structure.
DATA : idetails TYPE abap_compdescr_tab,
xdetails TYPE abap_compdescr.
DATA : ref_table_des TYPE REF TO cl_abap_structdescr.
* Get the structure of the table.
ref_table_des ?=
cl_abap_typedescr=>describe_by_name( l_dbtab ).
idetails[] = ref_table_des->components[].
LOOP AT idetails INTO xdetails.
CLEAR xfc.
xfc-fieldname = xdetails-name .
xfc-datatype = xdetails-type_kind.
xfc-inttype = xdetails-type_kind.
xfc-intlen = xdetails-length.
xfc-decimals = xdetails-decimals.
APPEND xfc TO ifc.
ENDLOOP.
ENDFORM.
*---------------------------------------------------------------------*
* FORM create_dynamic_itab *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM create_dynamic_itab.
* Create dynamic internal table and assign to FS
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = ifc
IMPORTING
ep_table = dy_table.
ASSIGN dy_table->* TO <dyn_table>.
* Create dynamic work area and assign to FS
CREATE DATA dy_line LIKE LINE OF <dyn_table>.
ASSIGN dy_line->* TO <dyn_wa>.
ENDFORM.
FORM display_fields_list.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
IT_FIELDCAT = ifc
TABLES
T_OUTTAB = dy__table
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.
ENDFORM.