cancel
Showing results for 
Search instead for 
Did you mean: 

i need nested for loop for deep structure

aravindcsebe
Explorer
0 Kudos
method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_EXPANDED_ENTITYSET.

    DATA :
      IT_DEEP TYPE STANDARD TABLE OF ZDEEP_S,
           WA_deep TYPE ZDEEP_S,
      IT_HEAD TYPE STANDARD TABLE OF ZHEADER,
           WA_HEAD TYPE ZHEADER,
      IT_ITEM TYPE STANDARD TABLE OF ZITEM1,

           WA_ITEM TYPE ZITEM1.


    CASE iv_entity_set_name.

      WHEN 'headerSet'.

        SELECT * FROM ZHEADER INTO TABLE IT_HEAD.
        IF SY-SUBRC EQ 0.
          SELECT * FROM ZITEM1 INTO TABLE IT_ITEM FOR ALL ENTRIES IN IT_HEAD WHERE ID EQ IT_HEAD-ID.

        ENDIF.


    ENDCASE.


    LOOP AT IT_HEAD INTO WA_HEAD.

      MOVE-CORRESPONDING wa_head to wa_deep.

      loop at IT_ITEM into WA_ITEM.

        if WA_HEAD-ID eq WA_ITEM-ID.

          APPEND WA_ITEM to WA_DEEP-HEADERTOITEMNAV.
          CLEAR WA_ITEM.

        ENDIF.






      ENDLOOP.
      APPEND WA_DEEP to IT_DEEP.
      clear WA_DEEP.
    ENDLOOP.







    COPY_DATA_TO_REF(
      exporting
        IS_DATA = IT_DEEP
      changing
        CR_DATA = ER_ENTITYSET
    ).

  endmethod.





in this code i have used nested loop in that place i need nested for loop
FredericGirod
Active Contributor

Could you use [CODE] button to format your code, and make some sentences to explain what do you want to do and what is your problem ?

Accepted Solutions (1)

Accepted Solutions (1)

Sandra_Rossi
Active Contributor

I hope that you will ask some questions about the code below, so that you understand, and next time you will be able to find by yourself.

Old code:

    LOOP AT IT_HEAD INTO WA_HEAD.
      MOVE-CORRESPONDING wa_head to wa_deep.
      loop at IT_ITEM into WA_ITEM.
        if WA_HEAD-ID eq WA_ITEM-ID.
          APPEND WA_ITEM to WA_DEEP-HEADERTOITEMNAV.
          CLEAR WA_ITEM.
        ENDIF.
      ENDLOOP.
      APPEND WA_DEEP to IT_DEEP.
      clear WA_DEEP.
    ENDLOOP.

New code (I couldn't check the syntax because you didn't provide a reproducible example):

    it_deep = VALUE #( FOR <wa_head> IN it_head
                       ( VALUE #( BASE CORRESPONDING #( <wa_head> )
                         HeaderToItemNav = VALUE #( FOR <wa_item> IN it_item
                                                    WHERE ( id = <wa_head>-id )
                                                    ( <wa_item> ) ) ) ) ).
aravindcsebe
Explorer
0 Kudos

this is working fine, as I expected.

how did you do that you are a gem. 🙂

aravindcsebe
Explorer
0 Kudos

if you have time please give us some explanation about the new syntax, because i did not understand that.

FredericGirod
Active Contributor

You have old and new code, you could understand.

VALUE type( field1 = .... field 2 = .. ). <-- set data into a structure with one statement

FOR my_field IN my_data <-- more or less like a LOOP where my_field get value of the field of my_data one by one

my_result = CORRESPONDING output_structure( input_structure ) <-- little bit like move-corresponding with additionnal tool

aravindcsebe
Explorer
0 Kudos

VALUE #( BASE CORRESPONDING #( wa_head1 )

this is what I didn't understand

Sandra_Rossi
Active Contributor

These are two different parts that you can analyze separately and in order:

  1. CORRESPONDING #( wa_head1 ) will be like a move-corresponding wa_head1 to a temporary "anonymous" structure (no name indicated)
  2. VALUE #( BASE base_structure comp1 = ... comp2 = ... ) says that the structure after BASE will have some of its components initialized with other values, and the components which are not mentioned keep their values coming from the base structure

Answers (2)

Answers (2)

roberto_forti
Contributor
0 Kudos

Hi, in case that "headerSet" is an entity then try reading related entities instead of implementing (Select *) and after that work with the results from Header and Item.

Header:

READ ENTITY headerSet\\alias FROM VALUE #( FOR <header_key> IN keys ( %key-ID = <header_key>-ID ) )
RESULT DATA(lt_headerSet).

Item:

READ ENTITY itemSet\\alias FROM VALUE #( FOR <item_key> IN keys ( %key-ID = <item_key>-ID ) )
RESULT DATA(lt_itemSet).
aravindcsebe
Explorer
0 Kudos

thanks for your input.

aravindcsebe
Explorer
0 Kudos

nitish2027