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: 

Field symbol to dynamic variable field naming

deiamolina
Active Participant
0 Kudos
2,355

Hello all,

I am not used to field-symbols and I don't know if it's possible to do.

I need to create a node for a ALV Tree and the hierarchy changes 9 times. So, instead of create 9 loops I want to create a Perform and use the field as parameter.

case 'X'.

     when s1.

       perform tree_nodes_s1 using 'BEZEI' 'NAME1' 'VTEXT'.

     when s2.

       perform tree_nodes_s2 using 'BEZEI' 'VTEXT' ''.


.....


In the Form


loop at it_data into wa_data.

     if l_field1 is not initial.

       on change of field.

         perform add_carrid_line using wa_data   ''

                                 changing l_carrid_key.

       endon.

     endif.

So, the field need to be wa_data-bezei or wa_data-name1 or wa_data-vtext etc.

I guess that I saw it on SAP code but I really don't know how to reproduce it.

Thanks,

Andréa

1 ACCEPTED SOLUTION

roland_spindler
Participant
0 Kudos
772

Hello Andréa,

FIELD-SYMBOLS: <l_field> TYPE ANY.

ASSIGN COMPONENT i_fieldname OF STRUCTURE wa_data TO <l_field>.

CHECK sy-subrc = 0.

Where i_fieldname is an importing parameter containing the field name. After that you can work with <l_field> like it was that field of structure wa_data.

kind regards

Roland

6 REPLIES 6

roland_spindler
Participant
0 Kudos
773

Hello Andréa,

FIELD-SYMBOLS: <l_field> TYPE ANY.

ASSIGN COMPONENT i_fieldname OF STRUCTURE wa_data TO <l_field>.

CHECK sy-subrc = 0.

Where i_fieldname is an importing parameter containing the field name. After that you can work with <l_field> like it was that field of structure wa_data.

kind regards

Roland

0 Kudos
772

Hi Roland Spindler

I did that before and I thought that it was wrong because I got this same error message:

The field "<L_FIELD1>" specified under LIKE either has no type or a  generic type. type.

Code

   field-symbols: <l_field1> type any.

   assign component param1 of structure wa_data to <l_field1>.

   check sy-subrc = 0.

     loop at it_data into wa_data.

     if <l_field1> is not initial.

       on change of <l_field1>.

         perform add_carrid_line using wa_data   ''

                                 changing l_carrid_key.

       endon.

     endif.


Not sure if I can use it on 'on change of'.


do you know the right type to declare?


Thanks,

Andrea

0 Kudos
772

No, you cannot use a typeless field symbol like this, since it refers to a single variable, not to a column as in "on change of".

Try something like this:

if <l_field1> is not initial.

  if l_string <> <l_field1>.

         perform add_carrid_line using wa_data   ''

                                 changing l_carrid_key

  endif.

l_string = <l_field1>.

endif.

Roland

0 Kudos
772

Hi

Thanks for all your help.

It's now ativate but the field-symbol is not being filled.

Any idea what is wrong?

Sorry for all those question but I am really lost here. Not used at all with field-symbol.

Hopefully it will be the last question

Thanks,

Andréa

0 Kudos
772

Before the loop the fields are empty of course, you just created a reference. They should be filled within the loop though. (Provided there are values in the first place.)

0 Kudos
772

Ok.. I thought that it will be like wa_data-bezei and not the value.

Make sense!

Thanks