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 .....AT NEW matnr or werks or kunnr.........endloop.

Former Member
0 Kudos

Hi All,

I need some logic to loop at an internal table and be able to separate code when ever there is a change in either material or plant or customer.

something like this.

  LOOP AT i_mseg.
    AT NEW ( matnr or werks or kunnr ).
       Perform do_new_code.
    ENDAT.
       Perform do_old_code.
  ENDLOOP.

Let me know how to deal with this kinda issue.

Thanks in advance,

Jr.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Look at the help for the AT NEW command. You'll find the SAP concept of the default key. It means that if you have an internal table like:


data: begin of itab occurs 0,
        matnr like ...
        werks like ...
        kunnr like ...
      end   of itab.

All you have to do is say:


loop at itab.
  at new KUNNR.
    perform do_new_code.
  endat.
endloop.

You'll get do_new_code performed for a break of any of the three fields.

Rob

4 REPLIES 4

abdul_hakim
Active Contributor
0 Kudos

hi

you cannot use OR addition with AT NEW instead you can use OR addition in ON CHANGE OF statement.

eg.

ON CHANGE OF i_mseg-matnr OR i_mseg-werks OR i_mseg-KUNNR.

ENDON.

<b>Keep in mind that ON CHANGE OF is outdated...</b>

May i know wat do you wanna achieve actually???

Cheers,

Abdul Hakim

0 Kudos

Hi Abdul,

Thanks for the reply.

I have a report for which i need to find the start balance at each customer before showing the rest of the transactions in a specified period. Fo this i have all the logic except that i'm unable to pull a few records . And on debuggin i found out that this is happening.

Right now i'm only checking for a change in Material number as i have sorted the internal table as per customer, plant and material. But when i have the same material at different customers, this isn't being considered as achange in the matrial number and so getting wrong data.


LOOP AT i_mseg.
    CLEAR: i_detail, i_lips, wa_mseg.
    wa_mseg = i_mseg.
    AT NEW matnr.
      CLEAR: i_summary.

      i_summary-kunnr = wa_mseg-kunnr.
      i_summary-werks = wa_mseg-werks.
      i_summary-matnr = wa_mseg-matnr.
      i_summary-btext = 'Begin. Balance'.

      PERFORM calculate_begin_balance
                                USING i_summary-kunnr
                                      i_summary-werks
                                      i_summary-matnr
                             CHANGING i_summary-menge.
      APPEND i_summary.
    ENDAT.

    i_detail-kunnr  = wa_mseg-kunnr.
    i_detail-werks  = wa_mseg-werks.
    i_detail-matnr  = wa_mseg-matnr.
    CASE wa_mseg-shkzg.
      WHEN 'S'. i_detail-menge = wa_mseg-menge.     
      WHEN 'H'. i_detail-menge = wa_mseg-menge * -1. 
    ENDCASE.
    APPEND i_detail.

ENDLOOP.

Thanks again..!

Jr.

0 Kudos

Hi,

What Rob suggested above will work for your requirement. Use that in your program.

Thanks.

Former Member
0 Kudos

Look at the help for the AT NEW command. You'll find the SAP concept of the default key. It means that if you have an internal table like:


data: begin of itab occurs 0,
        matnr like ...
        werks like ...
        kunnr like ...
      end   of itab.

All you have to do is say:


loop at itab.
  at new KUNNR.
    perform do_new_code.
  endat.
endloop.

You'll get do_new_code performed for a break of any of the three fields.

Rob