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: 

what is use of AT NEW and AT CHANGE

Former Member
0 Kudos
2,196

what is use of AT NEW and AT CHANGE

6 REPLIES 6

Former Member
0 Kudos
1,601

Hi,

At New

1. When a new record comes at new triggers. Atnew only used inside loop and endloop.

2. At new is controlbreak statment on at new the left side field change, the event

trigers and the values become 0 and *

On Change

1. On change of works same like at-new but the diff is it can be used out side of a loop,like select and endselect,case endcase.

2.. on change of is not a control break statement and we can use onchange of out side the loop

and

The Major difference between the two is that AT NEW can be used ONLY for INTERNAL TABLES, while ON CHANGE OF can be used for any loop processing i.e do..enddo, while..endwhile and loop..endloop also.

AT NEW <field>

The block will be executed when the SY-TABIX is 1 (or) when the SY-TABIX 1 value has a change with the SY-TABIX value.

With this block the field towards the right to the field are not accessable with in the block, and are shown with '*'. They can be accessed outside the block.

ON CHANGE OF <itab>-<field>

The block also works similar to the AT First. In this block all the fields are accesseble.

ON CHANGE OF triggers whenever there is a change in that particular field.

AT NEW triggers whenever there is a change in the fields before that particular field.ie. if there is a change in that combinations from the first field.

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

Regards,

Shiva.

Former Member
0 Kudos
1,601

Using AT NEW

Use at new to detect a change in a column from one loop pass to the next. This statements enable you to execute code at the beginning of a group of records.

Syntax for the at new Statement

sort by c.

loop at it.

at new c.

endat.

endloop.

where:

it is an internal table.

c is a component of it.

The following points apply:

This statements can only be used within loop at; it cannot be used within select.

at new does not have to come before at end of. It can appear in any order.

This statement can appear multiple times within the same loop. For example, you could have two at new and three at end of statements within one loop and they can appear in any order.

These statements should not be nested inside of one another (that is, at end of should not be placed inside of at new / endat).

There are no additions to these statements.

Each time the value of c changes, the lines of code between at new and endat are executed. This block is also executed during the first loop pass or if any fields to the left of c change. Between at and endat, the numeric fields to the right of c are set to zero. The non-numeric fields are filled with asterisks (*). If there are multiple occurrences of at new, they are all executed. at end of behaves in a similar fashion.

A control level is the component named on a control break statement; it regulates the control break. For example, in the following code snippet, f2 is a control level because it appears on the at new statement.

loop at it.

at new f2.

"(some code here)

endat.

endloop.

It is said that a control break is triggered if the control level changes. This means that when the contents of the control level change, the code between the at and endat is executed.

A control break is also triggered if any of the fields prior to the control level in the structure change. Therefore, you should define the internal table structure to begin with the fields that form your control levels. You must also sort by all fields prior to and including c.

Between at and endat, numeric fields to the right of the control level will be zero and non-numeric fields will be filled with asterisks.

Using ON CHANGE OF

Another statement you can use to perform control break processing is on change of. It behaves in a manner similar to at new.

Syntax for the on change of Statement

The following is the syntax for the on change of statement.

on change of v1 or v2 .

else.

endon.

where:

v1 and v2 are variable or field string names.

any number of or conditions might follow.

The following points apply:

If the value of any of the variables (v1, v2, and so on) changes from one test to the next, the statements following on change of are executed.

If no change is detected and else is specified, the statements following else are executed.

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

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.

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.

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

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

You can use else between on change of and endon.

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

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

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.

When a loop begins execution, the system creates a global auxiliary field for each field named in an on change of statement contained by the loop. On creation, these fields are given default initial values (blanks or zeros). They are freed when the loop ends.

Each time on change of is executed, the contents of its fields are compared with the contents of the global auxiliary fields. If they are different, the on change of is triggered and the auxiliary fields are updated with the new values. If they are the same, the code within on change of is not executed

Because global auxiliary fields do not exist outside a loop, you cannot use on change of outside of a loop.

I hope it helps.

Best Regards,

Vibha

Please mark all the helpful answers

Former Member
0 Kudos
1,601

AT NEW COMMAND GETS TRIGGERED WHEN

THERE IS CHANGE IN FIELD INPUT FROM THE FIRST COLUMN

ON CHANGE OF IS TRIGGERED WHEN THERE IS A

CHANGE IN FIELD INPUT OTHER THEN FIRST COLUMN I.E RIGHT SIDE COLUMNS

Former Member
0 Kudos
1,601

Hi,

This are the control break statement. Go through the document below.

Processing Control Levels

When you sort an extract dataset, control levels are defined in it. For general information about control levels, refer to Processing Internal Tables in Loops The control level hierarchy of an extract dataset corresponds to the sequence of the fields in the HEADER field group. After sorting, you can use the AT statement within a loop to program statement blocks that the system processes only at a control break, that is, when the control level changes.

AT NEW <f> | AT END OF <f>.

...

ENDAT.

A control break occurs when the value of the field <f> or a superior field in the current record has a different value from the previous record (AT NEW) or the subsequent record (AT END). Field <f> must be part of the HEADER field group.

If the extract dataset is not sorted, the AT... ENDAT block is never executed. Furthermore, all extract records with the value HEX 00 in the field <f> are ignored when the control breaks are determined.

The AT... ENDAT blocks in a loop are processed in the order in which they occur. This sequence should be the same as the sort sequence. This sequence must not necessarily be the sequence of the fields in the HEADER field group, but can also be the one determined in the SORT statement.

If you have sorted an extract dataset by the fields <f1>, <f2>, ..., the processing of the control levels should be written between the other control statements as follows:

LOOP.

AT FIRST.... ENDAT.

AT NEW <f1>....... ENDAT.

AT NEW <f2>....... ENDAT.

...

AT <fgi>..... ENDAT.

<single line processing without control statement>

...

AT END OF <f2>.... ENDAT.

AT END OF <f1>.... ENDAT.

AT LAST..... ENDAT.

ENDLOOP.

You do not have to use all of the statement blocks listed here, but only the ones you require.

REPORT DEMO.

DATA: T1(4), T2 TYPE I.

FIELD-GROUPS: HEADER.

INSERT T2 T1 INTO HEADER.

T1 ='AABB'. T2 = 1. EXTRACT HEADER.

T1 ='BBCC'. T2 = 2. EXTRACT HEADER.

T1 ='AAAA'. T2 = 2. EXTRACT HEADER.

T1 ='AABB'. T2 = 1. EXTRACT HEADER.

T1 ='BBBB'. T2 = 2. EXTRACT HEADER.

T1 ='BBCC'. T2 = 2. EXTRACT HEADER.

T1 ='AAAA'. T2 = 1. EXTRACT HEADER.

T1 ='BBBB'. T2 = 1. EXTRACT HEADER.

T1 ='AAAA'. T2 = 3. EXTRACT HEADER.

T1 ='AABB'. T2 = 1. EXTRACT HEADER.

SORT BY T1 T2.

LOOP.

AT FIRST.

WRITE 'Start of LOOP'.

ULINE.

ENDAT.

AT NEW T1.

WRITE / ' New T1:'.

ENDAT.

AT NEW T2.

WRITE / ' New T2:'.

ENDAT.

WRITE: /14 T1, T2.

AT END OF T2.

WRITE / 'End of T2'.

ENDAT.

AT END OF T1.

WRITE / 'End of T1'.

ENDAT.

AT LAST.

ULINE.

ENDAT.

ENDLOOP.

This program creates a sample extract, containing the fields of the HEADER field group only. After the sorting process, the extract dataset has several control breaks for the control levels T1 and T2.

In the loop, the system displays the contents of the dataset and the control breaks it encountered .

Please reward some points.

bye,

anomitro

Former Member
0 Kudos
1,601

Hi,

At New.

Effect

Beginning or end of a group of lines with the same content in the component comp1 comp2 ... and in the components to the left of comp1 comp2 .... The components comp1 comp2 ... can be specified, as described in the section Specification of Components, with the limitation that access to object attributes is not possible here.

Example:

codeLOOP AT itab result ...

[AT NEW comp1.

...

ENDAT.

endloop.[/code]

On Change of:

Effect:

The statements ON CHANGE OF and ENDON, which are forbidden in classes, define a control structure that can contain a statement block statement_block. After ON CHANGE OF, any number of data objects dobj1, dobj2... of any data type can be added..

Example:

In a SELECT loop, a statement block should only be executed if the content of the column CARRID has changed.

codeDATA spfli_wa TYPE spfli.

SELECT *

FROM spfli

INTO spfli_wa

ORDER BY carrid.

...

ON CHANGE OF spfli_wa-carrid.

...

ENDON.

...

ENDSELECT.[/code]

Reward Points For All help full answer

Regards

Fareedas

0 Kudos
1,601

whr r u working