How to find out the type of a variable when it gets declared with TABLE-FIELD

3k Views Asked by At

Is it possible to find out the TABLE-FIELD phrase from a variable? In the example below, how do I get jkak-vbeln as a type from dref as a string? I tried some RTTI but didn't find anything useful.

DATA: p_dat TYPE jkak-vbeln.
DATA: dref TYPE REF TO data.

GET REFERENCE OF p_dat INTO dref.
2

There are 2 best solutions below

0
Sandra Rossi On BEST ANSWER

This answer is for getting at runtime the text JKAK-VBELN from a variable declared with DATA p_dat TYPE jkak-vbeln, i.e. for getting its type name in full text. This answer works well for types based on ABAP Dictionary (DDIC) "TABLE-FIELD" combination, I don't know for anything else.

RTTI is only interested by the technical type, not by the exact path of its origin, so you can't do it fully with RTTI.

If the type of the variable comes from the DDIC, then you can use DESCRIBE FIELD ... HELP-ID .... Assuming that your starting point is your DREF reference to that variable, here is how to use it:

  DATA: p_dat TYPE jkak-vbeln.
  DATA: dref TYPE REF TO data.

  GET REFERENCE OF p_dat INTO dref.

  DATA: help_id TYPE string.
  FIELD-SYMBOLS: <any> TYPE any.

  ASSIGN dref->* TO <any>.
  DESCRIBE FIELD <any> HELP-ID help_id.

  ASSERT help_id = 'JKAK-VBELN'.

If your goal is to scan your ABAP source code, then you can use READ REPORT, but you have many more things to think about, like using a lexer, a parser, reading the parent or child ABAP source code units, etc.

3
Hendrik On

Assuming you want to know "AVNR" type of the field VBELN in table JKAK? Then try this:

DATA: p_dat TYPE jkak-vbeln.
DATA: dref TYPE REF TO data.

GET REFERENCE OF p_dat INTO dref.

DATA: o_type TYPE REF TO cl_abap_typedescr,
      o_elem TYPE REF TO cl_abap_elemdescr.

o_type = cl_abap_typedescr=>describe_by_data_ref( p_data_ref = dref ).
o_elem ?= o_type.
WRITE o_elem->get_ddic_field( )-tabname.

For other values regarding the ddic field you can check the other components in the last line ;)