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: 

How to stop looping internal table base on condition

former_member635273
Participant
0 Kudos

This is my code. I am using cl_gui_alv_grid and it has editable column. I need to check wa_res-sec_qty GT wa_res-lfimg or not.

FORM fm_save_data .
  LOOP AT it_res INTO wa_res.
    wa_res_selected-sec_num  = wa_res-sec_num.
    wa_res_selected-sec_item = wa_res-sec_item.
    wa_res_selected-vbeln    = wa_res-vbeln.
    wa_res_selected-posnr    = wa_res-posnr.
    wa_res_selected-banfn    = wa_res-banfn.
    wa_res_selected-bnfpo    = wa_res-bnfpo.
    wa_res_selected-sec_qty  = wa_res-sec_qty.
    wa_res_selected-est_date = wa_res-est_date.

    wa_res_selected-created_date = wa_res-created_date.
    wa_res_selected-created_time = wa_res-sec_num.
    wa_res_selected-created_by   = wa_res-created_by.

    IF wa_res-sec_qty > wa_res-lfimg.
      MESSAGE s398(00) WITH 'Sec. Qty Can not higher than QTY OD'   DISPLAY LIKE 'E'.
      CALL SCREEN 0120.
    ENDIF.
    wa_requisition_items_to_delete-preq_item =  wa_res_selected-bnfpo.
    wa_requisition_items_to_delete-delete_ind = 'X'.
    APPEND wa_requisition_items_to_delete TO it_requisition_items_to_delete.
    CLEAR wa_requisition_items_to_delete.
    APPEND wa_res_selected TO it_res_selected.
    CLEAR wa_res_selected.
  ENDLOOP.

  PERFORM fm_delete_pr.
  MODIFY ztblzsco FROM TABLE it_res_selected.

ENDFORM.

when i try that code for the first time, it's worked. but when i try second time there is an error like this,

Thanks in advance

4 REPLIES 4

Abinathsiva
Active Contributor
0 Kudos

Hi,

Try code below

FORM fm_save_data .
  LOOP AT it_res INTO wa_res.

IF wa_res-sec_qty < wa_res-lfimg.
      
    wa_res_selected-sec_num  = wa_res-sec_num.
    wa_res_selected-sec_item = wa_res-sec_item.
    wa_res_selected-vbeln    = wa_res-vbeln.
    wa_res_selected-posnr    = wa_res-posnr.
    wa_res_selected-banfn    = wa_res-banfn.
    wa_res_selected-bnfpo    = wa_res-bnfpo.
    wa_res_selected-sec_qty  = wa_res-sec_qty.
    wa_res_selected-est_date = wa_res-est_date.

    wa_res_selected-created_date = wa_res-created_date.
    wa_res_selected-created_time = wa_res-sec_num.
    wa_res_selected-created_by   = wa_res-created_by.

    
    wa_requisition_items_to_delete-preq_item =  wa_res_selected-bnfpo.
    wa_requisition_items_to_delete-delete_ind = 'X'.
    APPEND wa_requisition_items_to_delete TO it_requisition_items_to_delete.
    CLEAR wa_requisition_items_to_delete.
    APPEND wa_res_selected TO it_res_selected.
    CLEAR wa_res_selected.

else.

MESSAGE s398(00) WITH 'Sec. Qty Can not higher than QTY OD'   DISPLAY LIKE 'E'.
      CALL SCREEN 0120.
    ENDIF.
  ENDLOOP.

  PERFORM fm_delete_pr.
  MODIFY ztblzsco FROM TABLE it_res_selected.

ENDFORM.

0 Kudos

still error sir.

Sandra_Rossi
Active Contributor

Read the message:

Internal tables cannot be changed during a LOOP over them

Did you see what ABAP statement is concerned? (we can't see it because you didn't paste it in your question)

Did you see that an internal table to "display" in ALV is to be passed in a "CHANGING" parameter? (CL_GUI_ALV_GRID, CL_SALV_TABLE, etc.) It's because it's potentially changed by these API, especially in EDIT mode.

Generally speaking, the runtime error TABLE_FREE_IN_LOOP occurs in that case:

DATA(itab) = VALUE string_table( ( `Hello` ) ).
LOOP AT itab INTO DATA(line).
  itab = VALUE string_table( ). "<======= SHORT DUMP AT RUNTIME
ENDLOOP.

former_member1716
Active Contributor
0 Kudos

saddam.id,

As rightly indicated by sandra.rossi, the issue is due to your code that is trying to change the Internal table that is already looped over.

In your case, you have a subroutine as PERFORM fm_delete_pr, the code inside this routine should be trying to delete the entries from the table IT_RES which is looped. This behavior is not allowed, what you could do alternatively is fetch /filter the records which wants to be displayed into a different internal table simply ignoring the entries that needs to be deleted and use it accordingly.

Below Screen shot should help you understand it better.

Regards!