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

loop orders

Former Member
0 Likes
1,237

Hi all,

I loop over clients and I want to get the orders of that client every time :

loop at lt_client into ls_client.
  read table lt_orders
  into ls_orders
  with key client_id = ls_orders-client_id.
end loop.

the problem is that I get the first order of the client every time.

Ex : I have client1 that has Order1, Order2 and Order3

I want to read into ls_orders :

- Order1 in the first loop

- Order2

- and order 3

Any Ideas?

3 REPLIES 3
Read only

Sandra_Rossi
Active Contributor
1,076

error:

with key client_id = ls_orders-client_id.

correction:

with key client_id = ls_client-client_id.
Read only

michael_piesche
Active Contributor
1,076

You need to do a nested loop. With your current logic, it would look like this:

LOOP AT lt_client INTO ls_client.
  " do client/customer logic based on ls_client
  LOOP AT lt_orders INTO ls_orders WITH KEY client_id = ls_client-client_id.
    " do orders of customer logic based on ls_orders
  ENDLOOP.
ENDLOOP.

For performance reasons, if necessary, I would recommend an index on lt_orders by client. And also assign field-symbols instead of copying to new objects (unless you are messing around with values that shouldnt be updated in the table data):

DATA: lt_client TYPE SORTED TABLE OF .... WITH UNIQUE KEY client_id.
FIELD-SYMBOLS: <fs_client> LIKE LINE OF lt_client,
               <fs_order>  LIKE LINE OF lt_orders.

LOOP AT lt_client ASSIGNING <fs_client>.
  " do client/customer logic based on <fs_client>
  LOOP AT lt_orders ASSIGNING <fs_order> WITH TABLE KEY client_id = <fs_client>-client_id.
    " do orders of customer logic based on <fs_order>
  ENDLOOP.
ENDLOOP.
Read only

michael_piesche
Active Contributor
0 Likes
1,076

khalilferhat, please follow up on your open question.

  • comment answers or your question if there are still open issues.
  • otherwise mark an answer as accepted if it helped you solve your problem
  • or post an answer of yourself and accept it if you found another useful solution yourself
  • or redirect your question to another question that is related and was useful to solve your problem
  • in the end, close your question