Application Development 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: 

loop into loop with dynamic field name

0 Kudos

hi all,

i'm Software developer, and now i learn abap for do task in my work.

TL:DR - problem with loop table in another table with dynamic fields.

THE PROBLEM:

i have 2 tables like this:

TABLE 1: HR table with many colums and row..

TABLE 2 :
table with list of columns that i need to read from table 1

see this image:


in table 1,

i need loop all row,

and i need to check all the fields from table 2.

.if have "X" in the field, i need print the field description from table 2.


i try do it with nested loop (loop table 1, and each loop iteration loop table 2 and check all the fields of the row).

and i can't use variable on table-<FIelds> (i need variable for the loop in <Fields> ).

MY CODE:

    LOOP AT lt_hrp9229  INTO ls_hrp9229 .<br>       LOOP AT lt_otype_fields INTO ls_otype_fields .
       <br>       IF ls_hrp9229-ls_otype_fields-FIELD_NAME = 'X'.
        ev_descript_data = ev_descript_data && ','  &&  ls_otype_fields-Field_description.<br>
       ENDIF.
     ENDLOOP.
   ENDLOOP.

I have problem in this row:

IF ls_hrp9229-ls_otype_fields-FIELD_NAME = 'X'

i can't use dynamic field name from the loop.

thanks.

1 ACCEPTED SOLUTION

matt
Active Contributor

You need ASSIGN COMPONENT. I've removed the prefixes and replaced your variables with slightly more meaningful names.

LOOP AT hrp9229 INTO hrp9229_record.
  LOOP AT otype_fields INTO field .
    ASSIGN COMPONENT field-fieldname OF STRUCTURE hrp9229_record
      TO FIELD-SYMBOL(<field_value>).
    IF <field_value> = 'X'.
        ev_descript_data = ev_descript_data && ','  && 
                        field-Field_description.
    ENDIF.
  ENDLOOP.
ENDLOOP
2 REPLIES 2

former_member30
Community Manager
Community Manager
0 Kudos

Hi and welcome to the SAP Community!

Thank you for visiting SAP Community to get answers to your questions. Since you're asking a question here for the first time, I recommend that you familiarize yourself with https://community.sap.com/resources/questions-and-answers (if you haven't already), as it provides tips for preparing questions that draw responses from our members. For example, you can outline what steps you took to find answers (and why they weren't helpful) and share screenshots of what you've seen/done. The more details you provide, the more likely it is that members will be able to assist you.

Should you wish, you can revise your question by selecting Actions, then Edit (although once someone answers your question, you'll lose the ability to edit the question -- but if that happens, you can leave more details in a comment).

Finally, if you're hoping to connect with readers, please consider adding a picture to your profile. Here's how you do it: https://www.youtube.com/watch?v=F5JdUbyjfMA&list=PLpQebylHrdh5s3gwy-h6RtymfDpoz3vDS. By personalizing your profile with a photo of you, you encourage readers to respond.

Cheers,

Julia SAP Community Moderato

matt
Active Contributor

You need ASSIGN COMPONENT. I've removed the prefixes and replaced your variables with slightly more meaningful names.

LOOP AT hrp9229 INTO hrp9229_record.
  LOOP AT otype_fields INTO field .
    ASSIGN COMPONENT field-fieldname OF STRUCTURE hrp9229_record
      TO FIELD-SYMBOL(<field_value>).
    IF <field_value> = 'X'.
        ev_descript_data = ev_descript_data && ','  && 
                        field-Field_description.
    ENDIF.
  ENDLOOP.
ENDLOOP