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: 

Problem using AT..NEW statement

Former Member
0 Kudos

I have a internal table with structure

posting_date   amount
20090317       1500000
20090317      -950000
20090317        5000
20090319       8700
20090327       400000
20090327       32000

I am using AT NEW POSTING_DATE to perform some operation on the amount.

loop at itab.
AT NEW POSTING_DATE
 { 
      LOGIC
 }
ENDAT.
endloop.

all the record are working fine for AT NEW except for the last record.( posting date 20090327) It does not enter into AT..ENDAT

and simply goes to the endloop..Probably the reason for this is in AT new the current posting date is compared with the one that is above it...and if they are different AT NEW gets triggered. But in my case both the second last and last posting_date are the same, that's why AT NEW is not getting triggered..Please tell me how to solve this problem, how can I do the processing of last record?

PS this problem does not occur if both second last record and last record are different.

Edited by: priyasingh on Jul 22, 2009 11:01 AM

7 REPLIES 7

MarcinPciak
Active Contributor
0 Kudos

Hi,

AT NEW gets triggered here (in place where tested value has changed)


20090317       1500000   "<- here
20090317      -950000
20090317        5000
20090319       8700  "<- here
20090327       400000  "<- here
20090327       32000

If you want to get last value, use AT END OF event which get triggered


20090317       1500000   
20090317      -950000  
20090317        5000 "<- here
20090319       8700  "<- here
20090327       400000 
20090327       32000 "<-here

And in case you want totally last value, use AT LAST event


20090317       1500000   
20090317      -950000  
20090317        5000 
20090319       8700  
20090327       400000 
20090327       32000 "<- only here

Regards

Marcin

Former Member
0 Kudos

Hi

Use the event AT LAST. ENDAT

loop at itab.
   AT NEW POSTING_DATE
 { 
         LOGIC
 }
   ENDAT

   AT LAST.
       LOGIC
   ENDAT..
endloop.

That's good is you need to triggered the last record, u make sure not to elaborate the last record twice, u can check a flag for that:

DATA: FL_FIRST TYPE FLAG.

loop at itab.
   FL_FIRST = SPACE.
   AT NEW POSTING_DATE
      FL_FIRST = 'X'.
 { 
         LOGIC
 }
   ENDAT
   
   CHECK FL_FIRST IS INITIAL.

   AT LAST.
       LOGIC
   ENDAT..
endloop.

Max

Former Member
0 Kudos

Hi Priya,

Try the following logic as AT NEW alone wont work in ur case.

Describe itab into l_lines.

CNTR = 0.

loop at itab.

CNTR = CNTR + 1.

AT NEW POSTING_DATE

ENDAT.

if CNTR = l_lines -1. " (CHECK FOR SECOND LAST RECORD.)

l_pdate = wa_itab-pdate.

endif.

if CNTR = l_lines and l_pdate = wa_itab-pdate."(Checkfor last record and difference between last and 2nd last record)

{

LOGIC.

}

endif.

endloop.

CHEERS!!.

Regards,

Vimal

Edited by: Vimal V on Jul 22, 2009 11:15 AM

Former Member
0 Kudos

Hello Priya singh ,

where you want to trigger the event at the end of field value or at the first occurence of new field value ?.

Thanks and Regards ..

Priyank Dixit

Former Member
0 Kudos

Hi,

An alternative would be this. LOL....Its freaky though.

Do what the others have suggested or here it goes....

Append a record with initial values for the fields as the last entry in the internal table (provided you arent doing any kind of display with the data present there...If so, it will definitely be a pain in your neck). So your AT NEW event will get triggered for your record 20090327.

Former Member
0 Kudos

Hi,

at new event would process the records only when the column on which its used is changed..

so in the present context the last record will not b processed. But as per requirement for accessing the last record u can use

data: w_index type i, w_lines type i.

describe table itab lines w_lines.

w_index = w_lines.

read itab index w_index into <work_area>.

Regards,

Mdi.Deeba

Former Member
0 Kudos

Priya,

Can you try the same with :

ON CHANGE ON IT-POSTING_DATE

logic....

ELSE.

ENDON.