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

on change

Former Member
0 Likes
1,473

i need some sample code and help on " On change of " pls help me

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,436

Hi

TABLES: MARA.

DATA: I_MARA TYPE TABLE OF MARA.

DATA: W_MARA TYPE MARA.

*

SELECT * FROM MARA INTO TABLE I_MARA UP TO 100 ROWS.

*

LOOP AT I_MARA INTO W_MARA.

ON CHANGE OF W_MARA-MATKL.

WRITE: / W_mara-MATKL.

ENDON.

ENDLOOP.

<b>reward if usefull</b>

Hi

TABLES: MARA.

DATA: I_MARA TYPE TABLE OF MARA.

DATA: W_MARA TYPE MARA.

*

SELECT * FROM MARA INTO TABLE I_MARA UP TO 100 ROWS.

*

LOOP AT I_MARA INTO W_MARA.

ON CHANGE OF W_MARA-MATKL.

WRITE: / W_mara-MATKL.

ENDON.

ENDLOOP.

<b>reward if usefull</b>

11 REPLIES 11
Read only

Former Member
0 Likes
1,436

Hi,

Can you pls be little bit clear.

Read only

0 Likes
1,436

on change of event in internal table events.

Read only

Former Member
0 Likes
1,436

When we use At new for a field, it will trigger whenever there is any change in al lthe fields from the left to that of the particular field. But when we use On change of it triggers only when there is any change in the particular field.

At new can only be used inside loop. On change of can used outside the loop.

No logical Expressions can be added with at new. Logical expressions like AND OR can be used with on change of.

When AT NEW occurs, the alpha-numeric fields have * in their value,where as in case of On Change, the alphanumeric fields have their Corresponding value, of that particular record, where the Event gets fired.

On Change of executes for the first value of field too, this is not the case with At New.

On change of cannot be used in ABAP objects At new can be used in this.

Read only

varma_narayana
Active Contributor
0 Likes
1,436

Hi..

ON CHANGE OF <field> can be used in any Loop. It is triggered whenever the Field value changes during the Loop processing.

Eg 1 :

SELECT * FROM MARA INTO WA.

ON CHANGE OF WA-MTART. "material type.

ENDON.

ENDSELECT.

eG 2:

LOOP AT ITAB INTO WA.

ON CHANGE OF WA-MTART.

WRITE:/ WA-MTART.

ENDON.

WRITE:/ WA-MATNR, WA-MEINS.

ENDLOOP.

reward if Helpful.

Read only

Former Member
0 Likes
1,436

Hi,

I think you are a beginner in ABAP.

Please find the below piece of code .

TABLES T100.

SELECT * FROM T100 WHERE SPRSL = SY-LANGU AND

MSGNR < '010'

ORDER BY PRIMARY KEY.

ON CHANGE OF T100-ARBGB.

ULINE.

WRITE: / '**', T100-ARBGB, '**'.

ENDON.

WRITE: / T100-MSGNR, T100-TEXT.

ENDSELECT.

Displays all messages with their numbers in the logon language,

provided the number is less than '010'.

Each time the message class changes, it is output.

Regards,

Soumya.

Read only

Former Member
0 Likes
1,437

Hi

TABLES: MARA.

DATA: I_MARA TYPE TABLE OF MARA.

DATA: W_MARA TYPE MARA.

*

SELECT * FROM MARA INTO TABLE I_MARA UP TO 100 ROWS.

*

LOOP AT I_MARA INTO W_MARA.

ON CHANGE OF W_MARA-MATKL.

WRITE: / W_mara-MATKL.

ENDON.

ENDLOOP.

<b>reward if usefull</b>

Read only

Former Member
0 Likes
1,436

Hi

ON CHANGE OF has become obsolete in latest sap versions.

Better use AT NEW statement

It will do the same function.

see the doc

All this AT NEW, AT FIRST, AT END OF and AT LAST are called control break statements of Internal tables and are used to calculate the TOTALS based on sertain key fields in that internal table

FIrst to use these statements the ITAB has to be sorted by the key fields on whcih you need the SUM of the fields.

Some time you will get * when mopving data from this int table to other table using these commands

so you have to use

READ TABLE ITAB INDEX SY-TABIX in AT..ENDAT..if you are using other fields between them

DATA: sflight_tab TYPE SORTED TABLE OF sflight

WITH UNIQUE KEY carrid connid fldate,

sflight_wa LIKE LINE OF sflight_tab.

SELECT *

FROM sflight

INTO TABLE sflight_tab.

sort sflight by carrid connid.

LOOP AT sflight_tab INTO sflight_wa.

AT NEW connid.

WRITE: / sflight_wa-carrid,

sflight_wa-connid.

ULINE.

ENDAT.

WRITE: / sflight_wa-fldate,

sflight_wa-seatsocc.

AT END OF connid.

SUM.

ULINE.

WRITE: / 'Sum',

sflight_wa-seatsocc UNDER sflight_wa-seatsocc.

SKIP.

ENDAT.

AT END OF carrid.

SUM.

ULINE.

WRITE: / 'Carrier Sum',

sflight_wa-seatsocc UNDER sflight_wa-seatsocc.

NEW-PAGE.

ENDAT.

AT LAST.

SUM.

WRITE: / 'Overall Sum',

sflight_wa-seatsocc UNDER sflight_wa-seatsocc.

ENDAT.

ENDLOOP.

Regards

Anji

Read only

Former Member
0 Likes
1,436

Hi

where as in case of ON CHANGE,

the alpha-numeric fields have their corresponding value,

of that particular record,

where the Event gets fired.

*----


Other differences are :

ON CHANGE OF can be used any where in the program..

on change of differs from at new in the following respects:

1.It can be used in any loop construct, not just loop at. For example, it can be used within select and endselect, do and enddo, or while and endwhile, as well as inside get events.

2.A single on change of can be triggered by a change within one or more fields named after of and separated by or. These fields can be elementary fields or field strings. If you are within a loop, these fields do not have to belong to the loop.

3.When used within a loop, a change in a field to the left of the control level does not trigger a control break.

4.When used within a loop, fields to the right still contain their original values; they are not changed to contain zeros or asterisks.

5.You can use else between on change of and endon.

6.You can use it with loop at it where . . ..

7.You can use sum with on change of. It sums all numeric fields except the one(s) named after of.

8.Any values changed within on change of remain changed after endon. The contents of the header line are not restored as they are for at and endat.

while

AT NEW can be used only within a loop of an INTERNAL TABLE..

*----


5. Sample program to get the taste of it

(just copy paste)

6.

REPORT ABC.

DATA : BEGIN OF ITAB OCCURS 0,

bukrs like t001-bukrs,

f1(10) type c,

end of itab.

itab-bukrs = '1000'.

itab-f1 = '1111111'.

append itab.

itab-bukrs = '1100'.

itab-f1 = '3333333'.

append itab.

itab-bukrs = '1200'.

itab-f1 = '555555'.

append itab.

*----


AT NEW

loop at itab.

at new bukrs.

write 😕 itab-bukrs , itab-f1.

endat.

endloop.

*----


AT ONCHANGE

loop at itab.

ON CHANGE OF ITAB-BUKRS.

write 😕 itab-bukrs , itab-f1.

ENDON.

<b>ON CHANGE OF f.</b>

Executes the processing block enclosed by the "ONCHANGE OF f" and "ENDON" statements whenever the contents ofthe field f change (control break processing).

Normally, you use the statement to manipulate database fields during GET events or SELECT/ENDSELECT processing.

Example

TABLES T100. 
SELECT * FROM T100 WHERE SPRSL = SY-LANGU AND 
                         MSGNR < '010' 
                   ORDER BY PRIMARY KEY. 
  ON CHANGE OF T100-ARBGB. 
    ULINE. 
    WRITE: / '***', T100-ARBGB, '***'. 
  ENDON. 
  WRITE: / T100-MSGNR, T100-TEXT. 
ENDSELECT.

Displays all messages with their numbers in the logon language,provided the number is less than '010'.

Each time the message class changes, it is output.

<b>Reward if usefull</b>

Read only

Former Member
0 Likes
1,436

Hi Chaaya,

On Change of

is obselette in SAP.. Please dont use this in your development.....

<i>Control goes in the On Change of when the previous line item doesn't match with the existing line item..</i>

Example:

<b>Suppose Internal table data:</b>

<i>MIKE : TEST

MIKE : HELLO

LAKE : TUBE

SHIP : SAIL..</i>

<b>Control will flow it into this way.</b><i>first the line item reads then the control goes to OnChange of

then the second line item case It will not goes into Onchange of

then The Third Line item again control goes into ONChange of Becasue the 2 line item data(MIKE) is not same with the third line item data(LAKE)

and fourth line again control goes into OnChange of .</i>

                    • Reward if usefull**********************

Read only

Former Member
0 Likes
1,436

Hi,

see below sample code.

<b>DATA T100_WA TYPE T100.

SELECT * FROM T100

INTO T100_WA

WHERE SPRSL = SY-LANGU

AND MSGNR < '010'

ORDER BY PRIMARY KEY.

ON CHANGE OF T100_WA-ARBGB.

ULINE.

WRITE: / '**', T100_WA-ARBGB, '**'.

ENDON.

WRITE: / T100_WA-MSGNR, T100_WA-TEXT.

ENDSELECT</b>

Reward if useful.

Read only

Clemenss
Active Contributor
0 Likes
1,436

Hi CHAAYA,

lots of information has been given here.

Note:

Technically, ON CHANGE OF is not related to any control structure like LOOP, SELECT or whatever.

If the program flow comnes to an ON CHANGE OF <f> block, it will just check if the field <f> has been changed since ON CHANGE OF <f> has been processed before.

According to SAP online documentation (you should have access), normally, you use the statement to manipulate database fields during GET events or SELECT/ENDSELECT processing. The Online help also has an example. Please read it carefully and then consider if this or any more questions are to be posted here.

Regards,

Clemens