Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Get technical field names and their attributes (type, length, short/med/long text...)

sas70
Explorer
0 Kudos
1,253

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.
2 REPLIES 2

Sandra_Rossi
Active Contributor
753

You are using the old ALV technology. You can use CL_SALV_TABLE instead (your program will be shortened to 10 lines of code). With CL_SALV_TABLE, you will never need to initialize the field catalog as there's one defined by default based on your internal table, and you can adapt it through methods if needed.

Few remarks:

  1. CL_ABAP_TYPEDESCR is part of Runtime Type Services / Identification (RTTS / RTTI).
  2. You should not use cl_alv_table_create=>create_dynamic_table, because it's slow and may short dump after a given number of calls. Instead use Alternative to CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE | SAP Blogs.

0 Kudos
753

Thank you so much for guiding me here. I am a beginner here in the ABAP world, so I could not run your code without the error of GREF_TABLE unknown field. Thanks again