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

At new command not working

Former Member
0 Likes
8,383

Hi all,

I pasted my sample code below. In this i have used "At new event" this command is not working in my code, the values are redundant, How to rectify this problem. Pls help me.

Thanks in advance.

Tables : RBKP,

RSEG.

Data : Begin of itab,

BELNR like RBKP-BELNR, " Invoice Document Number

BUZEI like RSEG-BUZEI, " Invoice Item Number

GJAHR like RBKP-GJAHR, " Fiscal Year

EBELN like RSEG-EBELN, " Purchase Order Number

MENGE like RSEG-MENGE, " Invoice Quantity

RMWWR like RBKP-RMWWR, " Invoice Amount

WAERS like RBKP-WAERS, " Currency

MATNR like RSEG-MATNR, " Material Number

LIFNR like RBKP-LIFNR, " Vendor Code

ZTERM like RBKP-ZTERM, " Terms of payment key

End of itab.

Data itab2 like itab occurs 0 WITH HEADER LINE.

data a like rbkp-lifnr.

Selection-screen : Begin of block a with frame title text-001.

Select-options : LIF For RBKP-LIFNR.

Select-options : gjahr For RBKP-gjahr.

Selection-screen : End of block a.

Start-of-selection.

select * from rbkp where LIFNR in LIF and GJAHR in gjahr.

if sy-subrc = 0.

select * from rseg where BELNR = rbkp-BELNR and gjahr = rbkp-gjahr.

if sy-subrc = 0.

itab-BELNR = RBKP-BELNR.

itab-BUZEI = RSEG-BUZEI.

itab-GJAHR = RBKP-GJAHR.

itab-EBELN = RSEG-EBELN.

itab-MENGE = RSEG-MENGE.

itab-RMWWR = RBKP-RMWWR.

itab-WAERS = RBKP-WAERS.

itab-MATNR = RSEG-MATNR.

itab-LIFNR = RBKP-LIFNR.

append itab to itab2.

endif.

endselect.

endif.

endselect.

Sort itab2 by LIFNR.

loop at itab2.

Move itab2 to itab.

at new LIFNR.

write : / itab-lifnr.

endat.

endloop.

Regards,

Kesav.

1 ACCEPTED SOLUTION
Read only

Former Member
3,378

Hi,

Declare the LIFNR as the first field in your internal table...The field position matters when you use AT NEW..AT END OF..

Because if you use AT NEW lifnr...The fields that are defined leftmost to that field will also be considered..Meaning if there is change in the value then AT NEW triggers..

You have two options..

1) Change the declaration.

Data : Begin of itab,

<b>LIFNR like RBKP-LIFNR, " Vendor Code</b>

BELNR like RBKP-BELNR, " Invoice Document Number

BUZEI like RSEG-BUZEI, " Invoice Item Number

GJAHR like RBKP-GJAHR, " Fiscal Year

EBELN like RSEG-EBELN, " Purchase Order Number

MENGE like RSEG-MENGE, " Invoice Quantity

RMWWR like RBKP-RMWWR, " Invoice Amount

WAERS like RBKP-WAERS, " Currency

MATNR like RSEG-MATNR, " Material Number

ZTERM like RBKP-ZTERM, " Terms of payment key

End of itab.

2) Instead of AT NEW..use ON CHANGE OF ..

loop at itab2.

Move itab2 to itab.

<b>ON CHANGE OF itab2-LIFNR.</b>

write : / itab-lifnr.

<b>ENDON.</b>

endloop.

Thanks

Naren

9 REPLIES 9
Read only

Former Member
0 Likes
3,378

Hi

Make lifnr as first field in the internal table,

Regards

Navneet

Read only

Former Member
0 Likes
3,378

Hi,

Your select query seems to be wrong. try like this

select * from rbkp into itab1

where LIFNR in LIF and GJAHR in gjahr.

if sy-subrc = 0.

select * from rseg

into itab2 for all entries in itab1

where BELNR = itab1-BELNR and gjahr = itab1.-gjahr.

Read only

Former Member
0 Likes
3,378

Hi Kesava,

The key field in RBKP table is BELNR. So while sorting sort with this also. Chk fro the changes in your code ( block letters)

Tables : RBKP,

RSEG.

Data : Begin of itab,

BELNR like RBKP-BELNR, " Invoice Document Number

BUZEI like RSEG-BUZEI, " Invoice Item Number

GJAHR like RBKP-GJAHR, " Fiscal Year

EBELN like RSEG-EBELN, " Purchase Order Number

MENGE like RSEG-MENGE, " Invoice Quantity

RMWWR like RBKP-RMWWR, " Invoice Amount

WAERS like RBKP-WAERS, " Currency

MATNR like RSEG-MATNR, " Material Number

LIFNR like RBKP-LIFNR, " Vendor Code

ZTERM like RBKP-ZTERM, " Terms of payment key

End of itab.

Data itab2 like itab occurs 0 WITH HEADER LINE.

data a like rbkp-lifnr.

Selection-screen : Begin of block a with frame title text-001.

Select-options : LIF For RBKP-LIFNR.

Select-options : gjahr For RBKP-gjahr.

Selection-screen : End of block a.

Start-of-selection.

select * from rbkp where LIFNR in LIF and GJAHR in gjahr.

if sy-subrc = 0.

select * from rseg where BELNR = rbkp-BELNR and gjahr = rbkp-gjahr.

if sy-subrc = 0.

itab-BELNR = RBKP-BELNR.

itab-BUZEI = RSEG-BUZEI.

itab-GJAHR = RBKP-GJAHR.

itab-EBELN = RSEG-EBELN.

itab-MENGE = RSEG-MENGE.

itab-RMWWR = RBKP-RMWWR.

itab-WAERS = RBKP-WAERS.

itab-MATNR = RSEG-MATNR.

itab-LIFNR = RBKP-LIFNR.

append itab to itab2.

endif.

endselect.

endif.

endselect.

<b>Sort itab2 by belnr LIFNR.</b>

loop at itab2.

Move itab2 to itab.

at new LIFNR.

write : / itab-lifnr.

endat.

endloop.

Reward if useful.

Regards,

Chitra

Read only

Former Member
3,379

Hi,

Declare the LIFNR as the first field in your internal table...The field position matters when you use AT NEW..AT END OF..

Because if you use AT NEW lifnr...The fields that are defined leftmost to that field will also be considered..Meaning if there is change in the value then AT NEW triggers..

You have two options..

1) Change the declaration.

Data : Begin of itab,

<b>LIFNR like RBKP-LIFNR, " Vendor Code</b>

BELNR like RBKP-BELNR, " Invoice Document Number

BUZEI like RSEG-BUZEI, " Invoice Item Number

GJAHR like RBKP-GJAHR, " Fiscal Year

EBELN like RSEG-EBELN, " Purchase Order Number

MENGE like RSEG-MENGE, " Invoice Quantity

RMWWR like RBKP-RMWWR, " Invoice Amount

WAERS like RBKP-WAERS, " Currency

MATNR like RSEG-MATNR, " Material Number

ZTERM like RBKP-ZTERM, " Terms of payment key

End of itab.

2) Instead of AT NEW..use ON CHANGE OF ..

loop at itab2.

Move itab2 to itab.

<b>ON CHANGE OF itab2-LIFNR.</b>

write : / itab-lifnr.

<b>ENDON.</b>

endloop.

Thanks

Naren

Read only

Former Member
0 Likes
3,378

hi

good

you have placed the AT NEW statement in a wrong place plz debug the report ,and checked it.

thanks

mrutyun^

Read only

varma_narayana
Active Contributor
0 Likes
3,378

Hi Kesava...

Change the code like this .. it will work..

Note:

1.Use joins instead of Nested SELECTs which gives the Worst performance.

2. You can use ON CHANGE OF <field> since the LIFNR is not the first field in ITAB.

Tables : RBKP,

RSEG.

Data : Begin of itab,

BELNR like RBKP-BELNR, " Invoice Document Number

BUZEI like RSEG-BUZEI, " Invoice Item Number

GJAHR like RBKP-GJAHR, " Fiscal Year

RMWWR like RBKP-RMWWR, " Invoice Amount

WAERS like RBKP-WAERS, " Currency

LIFNR like RBKP-LIFNR, " Vendor Code

ZTERM like RBKP-ZTERM, " Terms of payment key

EBELN like RSEG-EBELN, " Purchase Order Number

MATNR like RSEG-MATNR, " Material Number

MENGE like RSEG-MENGE, " Invoice Quantity

End of itab.

Data itab2 like itab occurs 0 WITH HEADER LINE.

data a like rbkp-lifnr.

Selection-screen : Begin of block a with frame title text-001.

Select-options : LIF For RBKP-LIFNR.

Select-options : gjahr For RBKP-gjahr.

Selection-screen : End of block a.

Start-of-selection.

SELECT RBKPBELNR RBKPGJAHR RBKPBUZEI RBKPRMWWR.

RBKPWAERS RBKPLIFNR RBKP~ZTERM

RSEGEBELN RSEGMATNR RSEG~MENGE

FROM RBKP

INNER JOIN RSEG

ON RBKPBELNR = RSEGBELNR

AND RBKPGJAHR = RSEGGJAHR

AND RBKPBUZEI = RSEGBUZEI

INTO TABLE ITAB2

WHERE RBKPLIFNR IN LIF AND RBKPGJAHR IN GJAHR.

Sort itab2 by LIFNR.

loop at itab2.

Move itab2 to itab.

on change of itab-LIFNR.

write : / itab-lifnr.

endat.

endloop.

REWARD IF HELPFUL.

Read only

Former Member
0 Likes
3,378

Hi

i am sending a sample code where that events worked

you can understand very easily

REPORT ZDAN108.

  • Using AT FIRST , AT NEW, AT THE END OF , AT LAST.

DATA: BEGIN OF ITAB OCCURS 0,

F1 TYPE I,

F2(6) TYPE C,

F3(10) TYPE N,

F4(16) TYPE P DECIMALS 2,

END OF ITAB.

DATA: SUB_TOT(10) TYPE P DECIMALS 3.

**--1

ITAB-F1 = 1.

ITAB-F2 = 'ONE'.

ITAB-F3 = 10.

ITAB-F4 = '1000.00'.

APPEND ITAB.

CLEAR ITAB.

ITAB-F1 = 1.

ITAB-F2 = 'ONE'.

ITAB-F3 = 20.

ITAB-F4 = '2000.00'.

APPEND ITAB.

CLEAR ITAB.

ITAB-F1 = 1.

ITAB-F2 = 'ONE'.

ITAB-F3 = 30.

ITAB-F4 = '3000.00'.

APPEND ITAB.

CLEAR ITAB.

*--2

ITAB-F1 = 2.

ITAB-F2 = 'TWO'.

ITAB-F3 = 10.

ITAB-F4 = '1000.00'.

APPEND ITAB.

CLEAR ITAB.

ITAB-F1 = 2.

ITAB-F2 = 'TWO'.

ITAB-F3 = 20.

ITAB-F4 = '2000.00'.

APPEND ITAB.

CLEAR ITAB.

*-- 3

ITAB-F1 = 3.

ITAB-F2 = 'THREE'.

ITAB-F3 = 10.

ITAB-F4 = '1000.00'.

APPEND ITAB.

CLEAR ITAB.

ITAB-F1 = 3.

ITAB-F2 = 'THREE'.

ITAB-F3 = 20.

ITAB-F4 = '2000.00'.

APPEND ITAB.

CLEAR ITAB.

SORT ITAB BY F1.

LOOP AT ITAB.

AT FIRST.

WRITE: /35 ' MATERIAL DETAILS:'.

ULINE.

ENDAT.

AT NEW F1.

WRITE: / 'DETAILS OF MATERIAL:' COLOR 7 , ITAB-F1.

ULINE.

ENDAT.

WRITE: / ITAB-F1, ITAB-F2, ITAB-F3, ITAB-F4.

SUB_TOT = SUB_TOT + ITAB-F4.

AT END OF F1.

ULINE.

WRITE: / 'SUB TOTAL :' COLOR 3 INVERSE ON, SUB_TOT COLOR 3 INVERSE ON.

CLEAR SUB_TOT.

ENDAT.

AT LAST.

SUM.

ULINE.

WRITE: 'SUM:', ITAB-F4.

ULINE.

ENDAT.

ENDLOOP.

<b>Reward if usefull</b>

Read only

Former Member
0 Likes
3,378

HI,

change the code like this

Data : Begin of itab,

LIFNR like RBKP-LIFNR, " Vendor Code

BELNR like RBKP-BELNR, " Invoice Document Number

BUZEI like RSEG-BUZEI, " Invoice Item Number

GJAHR like RBKP-GJAHR, " Fiscal Year

EBELN like RSEG-EBELN, " Purchase Order Number

MENGE like RSEG-MENGE, " Invoice Quantity

RMWWR like RBKP-RMWWR, " Invoice Amount

WAERS like RBKP-WAERS, " Currency

MATNR like RSEG-MATNR, " Material Number

ZTERM like RBKP-ZTERM, " Terms of payment key

End of itab.

Rgds,

Prajith

Read only

Former Member
0 Likes
3,378

after every control break statement u have read table statement with sy-index ......

its actually a bug in sap..................u can find this in OSS also......