2

I have a field symbol of a component of a structure:

ASSIGN COMPONENT lv_field_name OF STRUCTURE ls_structure TO <lv_data>.
IF sy-subrc = 0.
  WRITE <lv_data> TO lv_field_value.
ENDIF.

Problem: if <lv_data> is of type CURR the result of WRITE... might be wrong.

<lv_data> references to to a field that holds the currency symbol (like 'EUR'). In my case we can make the assumption that the referenced currency field is in the same structure.

Is there an abstract way to get the referenced currency value of <lv_data> so that I can write something like

WRITE <lv_data> TO lv_field_value CURRENCY <lv_currency>.

I looked into class cl_abap_typedescr and subclasses, but I found nothing that I can use to assign <lv_currency>.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Gerd Castan
  • 6,275
  • 3
  • 44
  • 89
  • I added the tag sap-data-dictionary (DDIC) because you are probably talking about DDIC structures, not ABAP internally-defined structures. – Sandra Rossi Jul 30 '21 at 11:22

1 Answers1

3

The class cl_abap_structdescr has a method get_ddic_field_list which returns a table of structure DFIES. The fields REFTABLE and REFFIELD contain the name of the reference field for the currency or unit for the corresponding field.

DATA(lo_structdescr) = CAST cl_abap_structdescr( cl_abap_typedescr=>describe_by_data( ls_structure ) ).
DATA(lt_ddic_fields) = lo_structdescr->get_ddic_field_list( ).

DATA(ls_ddic_info) = lt_ddic_fields[ fieldname = lv_field_name ].
ASSIGN COMPONENT lv_field_name OF STRUCTURE ls_structure TO FIELD-SYMBOL(<lv_data>).
ASSIGN COMPONENT ls_ddic_info-reffield OF STRUCTURE ls_structure TO FIELD-SYMBOL(<lv_currency>).

WRITE <lv_data> CURRENCY <lv_currency>.

Caveat: This code assumes that the currency field is in the same structure as the value field. This is not always the case! It is possible that ls_ddic_info-reftable mentions a different structure. In that case it gets a lot more complicated. You need to find the entry of that table which corresponds to your structure (probably from the database) and retrieve the currency field from there.

Philipp
  • 67,764
  • 9
  • 118
  • 153
  • of course, this code needs to be completed with exception handling stuff like adding word `EXCEPTIONS`, checking `sy-subrc`, checking line is found in table expression, etc. Note that the answer is valid if a structure is defined in the DDIC (otherwise `get_ddic_field_list` fails), and probably the OP meant a DDIC structure, not a structure internally defined, because a currency code column can be linked to a currency amount column only in DDIC. – Sandra Rossi Jul 30 '21 at 11:20