Application Development and Automation 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: 
Read only

How to use variable as fieldname?

Former Member
0 Likes
1,221

Hi forums,

i need to loop the columns and rows of a table, so i created a fieldcatalog and loop the rows of the table in the outer-loop and the rows of the catalog in the inner loop.

See the code below, wa_fieldcat-fieldname contains the fieldname of the field i want assign to item-text.


LOOP AT stueckliste INTO wastueckliste.
      LOOP AT fieldcat INTO wa_fieldcat.
        item-item_name = wa_fieldcat-fieldname.
        item-text = wastueckliste-{i need content of wa_fieldcat-fieldname here}
      endloop.
ENDLOOP.

eg. if wa_fieldcat-fieldname contains the string 'matnr', item-text shall become the content of wastueckliste-matnr, i hope you understand what i mean... i've already tried to assign wa_fieldcat-fieldname to a a fieldsymbol and access the field by wastueckliste-<fs> but that doesn't work, too...

btw: i know that snippet doesn't do nothing, its just an example...

Message was edited by:

Adam Pyschny

1 ACCEPTED SOLUTION
Read only

marcelo_ramos1
SAP Mentor
SAP Mentor
0 Likes
777

Hi,

Try this way,


DATA: stueckliste   ...
DATA: wastueckliste ...

DATA: fieldcat ...
DATA: wa_fieldcat ...

FIELD-SYMBOLS <field> TYPE ANY.

LOOP AT stueckliste INTO wastueckliste.
  LOOP AT fieldcat INTO wa_fieldcat.

    item-item_name = wa_fieldcat-fieldname.

" Here you are moving the field 'matnr'  of  wastueckliste to a field-symbol
    ASSIGN COMPONENT wa_fieldcat-fieldname
                      OF STRUCTURE wastueckliste TO <field>.

" IF ok, just use <field> as wastueckliste-matnr
    IF sy-subrc IS INITIAL.
      item-text = <field>.
    ELSE.
      "   error
    ENDIF.
  ENDLOOP.
ENDLOOP.

<b>Don't forget to close this Thread and reward points when your question be answered.</b>

Regards.

Marcelo Ramos

Hi forums,

i need to loop the columns and rows of a table, so i created a fieldcatalog and loop the rows of the table in the outer-loop and the rows of the catalog in the inner loop.

See the code below, wa_fieldcat-fieldname contains the fieldname of the field i want assign to item-text.


LOOP AT stueckliste INTO wastueckliste.
      LOOP AT fieldcat INTO wa_fieldcat.
        item-item_name = wa_fieldcat-fieldname.
        item-text = wastueckliste-{i need content of wa_fieldcat-fieldname here}
      endloop.
ENDLOOP.

eg. if wa_fieldcat-fieldname contains the string 'matnr', item-text shall become the content of wastueckliste-matnr, i hope you understand what i mean... i've already tried to assign wa_fieldcat-fieldname to a a fieldsymbol and access the field by wastueckliste-<fs> but that doesn't work, too...

btw: i know that snippet doesn't do nothing, its just an example...

Message was edited by:

Adam Pyschny

2 REPLIES 2
Read only

marcelo_ramos1
SAP Mentor
SAP Mentor
0 Likes
778

Hi,

Try this way,


DATA: stueckliste   ...
DATA: wastueckliste ...

DATA: fieldcat ...
DATA: wa_fieldcat ...

FIELD-SYMBOLS <field> TYPE ANY.

LOOP AT stueckliste INTO wastueckliste.
  LOOP AT fieldcat INTO wa_fieldcat.

    item-item_name = wa_fieldcat-fieldname.

" Here you are moving the field 'matnr'  of  wastueckliste to a field-symbol
    ASSIGN COMPONENT wa_fieldcat-fieldname
                      OF STRUCTURE wastueckliste TO <field>.

" IF ok, just use <field> as wastueckliste-matnr
    IF sy-subrc IS INITIAL.
      item-text = <field>.
    ELSE.
      "   error
    ENDIF.
  ENDLOOP.
ENDLOOP.

<b>Don't forget to close this Thread and reward points when your question be answered.</b>

Regards.

Marcelo Ramos

Read only

0 Likes
777

Works fantastic! thx..