‎2006 Nov 23 11:31 AM
HI Experts,
What i sthe difference between On change and At new statements?
Regards
‎2006 Nov 23 11:34 AM
Hi,
at new belongs to loop-endloop
and on change you can use in selects, loops ,do-enddo,while..
A.
Message was edited by:
Andreas Mann
‎2006 Nov 23 11:34 AM
Hi,
at new belongs to loop-endloop
and on change you can use in selects, loops ,do-enddo,while..
A.
Message was edited by:
Andreas Mann
‎2006 Nov 23 11:35 AM
onchage of is not allowed in ABAP OBJECTS whereas AT NEW can be used
Onchange of can be used in select statements where as AT NEW cannot be used
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.
‎2006 Nov 23 11:35 AM
Hi
When you r using AT NEW statement you can give the parameter at runtime also whereas in on change of it is not possible.
Eg.: AT NEW (parameter name) this parameter you can give at runtime, it means you can decide at runtime on which field you wish to perform at new.
Where as in case of ON CHANGE OF <internal tablename - field name> these details you need to decide while writing the program. You can decide the fields at runtime.
Other than this there is no difference in the output.
Hope this helps you.
Regards
Haritha.
‎2006 Nov 23 11:37 AM
Hi Ravi,
On change of and AT NEW statment working wise they are same.
The difference is using AT NEW F1 will convert the Char fileds to * which are left side of Field F1. But on Change of Statement will not pose such type of problems.
Its so because on change of statement will take the data to temporary momory and will compare the data there so thats the reason why we dont get * .
BR,
RAVI
‎2006 Nov 23 11:39 AM
Hullo,
the 'ON CHANGE' of statement will be triggered every time the value of referenced field changes.
the 'AT NEW' of statement will be triggered every time the value of referenced field changes.
However, while the 'ON CHANGE' command will display everything, the 'AT NEW' command takes a somewhat more structured approach.
At the start of a new control level (i.e. immediately after AT), the following occurs in the output area of the current LOOP statement:
- All character type fields (on the right) are filled with "*" after the current
control level key.
- All other fields (on the right) are set to their initial values after the current
control level key.
Also the 'AT NEW' statement will be triggered if a field to the left of the current control level key is changed as well.
With the 'AT NEW' statement it is possible to use the 'SUM' statement, which will sum all the ABAP number types (p, i, f, etc.).
Important to remember is using the 'AT NEW' statement is to sort your internal table according to the control level keys you are using, and make sure that the fields you want to display are to the left of the control key in the data declaration.
Which one should you use? Well the 'ON CHANGE' statement works, but is considered to be a 'quick & dirty' solution. The 'AT NEW' would be the 'gentleman's' solution.
Hope this helps.
‎2006 Nov 23 11:43 AM
Hello Ravi,
This is what I found in SAP help:
"Normally, you use the statement to manipulate database fields during GET events or SELECT/ENDSELECT processing.
There are special control structures for processing control breaks in LOOP s on internal tables or extract datasets (AT).
NOTE:
ON CHANGE OF is unsuitable for recognizing control levels in loops of this type because it always creates a global auxiliary field which is used to check for changes. This global auxiliary field is only changed in the relevant ON CHANGE OF statement. It is not reset when the processing enters loops or subroutines, so unwanted effects can occur if the loop or subroutine is executed again. Also, since it is set to its initial value when created (like any other field), any ON CHANGE OF processing will be executed after the first test, unless the contents of the field concerned happen to be identical to the initial value.
Hope this helps.
‎2006 Nov 23 11:47 AM
Hi
You can check with the following code:
&----
*& Report ZCLASSICAL_REP1 *
*& *
&----
*& *
*& *
&----
REPORT ZCLASSICAL_REP2
No Standard Page Heading
line-count 42(3).
Tables sflight.
Data itab like sflight occurs 0 with header line.
Data lines type i.
Data color type i.
parameters: p_name(20) type c.
Top-Of-Page.
write:/45 'Intelligroup Asia Pvt. Ltd.,' color col_heading.
uline.
Start-Of-Selection.
Select * from sflight into table itab.
End-Of-Selection.
sort itab by carrid.
loop at itab.
At First.
write:/50 'Flight Details' color 6.
Endat.
at new (p_name).
lines = sy-linct - sy-linno.
skip lines.
new-page.
write:/5 'Airline Code' color col_heading, 27 'Connection No.' color
col_heading.
uline.
endat.
write:/5 itab-carrid, 30 itab-connid.
At Last.
uline.
write:/55 'Total Number Of Flights', sy-tfill color col_total.
uline.
Endat.
endloop.
lines = sy-linct - sy-linno.
skip lines.
End-Of-Page.
uline.
write:/10 'Page Number:', sy-pagno color col_total, 90 'Index Number:'
,sy-index color col_total.
uline.
Regards
Haritha.
‎2006 Nov 23 12:02 PM
Hi
'ON CHANGE OF' can be used with in a SELECT..ENDSELECT and in LOOP..ENDLOOP..
But AT NEW can be used only in LOOP..ENDLOOP
By the way.. 'ON CHANGE OF' is obsolute in SAP Netweaver 2004s.
'ON CHANGE OF'
The first time a statement ON CHANGE OF is executed, the statement block is executed if at least one of the specified data objects is not initial. The statement block is executed for each additional execution of the same statement ON CHANGE OF, if the content of one of the specified data objects has been changed since the last time the statement ON CHANGE OF was executed
AT NEW..ENDAT
Statement between AT NEW and ENDAT will be executed once per change of the field your are checking. and more over the right side fields after the field in checking in new are not visible
Sample Program.
&----
*& Report ZGD_TEST_HR_ORG_STR
*&
&----
*&
*&
&----
REPORT ZGD_TEST_HR_ORG_STR.
DATA: plvar LIKE hrsobid-plvar.
DATA: otype LIKE hrsobid-otype.
DATA: sobid LIKE hrsobid-sobid.
DATA: date LIKE sy-datum.
DATA: leading_position LIKE hrobject OCCURS 1 WITH HEADER LINE.
DATA: org_agent LIKE swhactor.
DATA: lv_sw_vapos TYPE xflag,
lv_subrc TYPE sy-subrc.
DATA spfli_wa TYPE spfli.
DATA carrid_buffer TYPE spfli-carrid.
IF plvar IS INITIAL.
CALL FUNCTION 'RH_GET_ACTIVE_WF_PLVAR'
IMPORTING
act_plvar = plvar
EXCEPTIONS
no_active_plvar = 1.
IF sy-subrc > 0.
MESSAGE e300(5w) RAISING no_active_plvar.
ENDIF.
ENDIF.
IF otype IS INITIAL OR sobid IS INITIAL.
otype = 'US'.
sobid = sy-uname.
ENDIF.
CALL FUNCTION 'RH_GET_LEADING_POSITION'
EXPORTING
plvar = plvar
otype = otype
sobid = sobid
date = sy-datum
auth = ' '
consider_vac_pos = space
TABLES
leading_pos = leading_position
EXCEPTIONS
no_lead_pos_found = 1
OTHERS = 2.
IF sy-subrc > 0.
MESSAGE e802(5w) WITH otype sobid
RAISING no_lead_pos_found.
ELSE.
LOOP AT leading_position.
write : / leading_position-otype,
leading_position-objid.
ENDLOOP.
Skip 2.
write / 'AT New'.
LOOP AT leading_position.
at new otype.
write : / leading_position-otype,
leading_position-objid.
endat.
ENDLOOP.
Skip 2.
Write 'On Change 1'.
LOOP AT leading_position.
ON CHANGE OF leading_position-otype.
write : / leading_position-otype,
leading_position-objid.
endon.
endloop.
ENDIF.
SELECT *
FROM spfli
INTO spfli_wa
ORDER BY carrid.
ON CHANGE OF spfli_wa-carrid.
write : / spfli_wa-carrid.
ENDON.
ENDSELECT.
" the below statement shows how the above can be used with IF
CLEAR carrid_buffer.
SELECT *
FROM spfli
INTO spfli_wa
ORDER BY carrid.
IF spfli_wa-carrid <> carrid_buffer.
carrid_buffer = spfli_wa-carrid.
ENDIF.
ENDSELECT.
‎2006 Nov 23 12:10 PM
hi,
one importatnt tip.
AT NEW : for this to work the field addressing shuld come as the first variable declared inside the internal table.
On CHANGE : the field addressing can come any where in the internal tabel.Regards
Anver
‎2006 Nov 23 12:37 PM
Hi ravi,
The Major Difference is :
a) When AT NEW occurs,
the alpha-numeric fields have ******* in their value,
b) 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.
endloop.
Regards,
amit m.