2
votes

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

1
I added the tag sap-data-dictionary (DDIC) because you are probably talking about DDIC structures, not ABAP internally-defined structures.Sandra Rossi

1 Answers

3
votes

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.