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: 

on change of statement not working within loop

Former Member
0 Kudos

Hi all,

I am using on change of statement within a loop of an internal table. I am refreshing and clearing the header of internal table every time the program enters the loop. But the program is not executing the code within the on change of statement every first time the program enters the loop.

Please help me to solve this issue

Thanks,

Rajan

9 REPLIES 9

Former Member
0 Kudos

Hello

For internal table (loop/endloop) use AT CHANGE OF because:

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.

It is from SAP documentation.

Former Member
0 Kudos

Hi,

How about using AT NEW instead of ON CHANGE OF.

Since you are using ON CHANGE OF inside a loop, you can also use AT NEW.

Basic difference between AT NEW and ON CHANGE OF is the latter can be used outside scope of a loop.

Go ahead with AT NEW, it should solve your problem.

I hope it helps,

NaPPy

Former Member
0 Kudos

Hi,

Don't Clear the work area of a Internal table before the ON CHANGE OF statement within a Loop and Make sure that you Sorted the Internal Table by Field which is Given in ON CHANGE OF command. Clear Internal Table Header Before Loop or Before Endloop Statement.

With Regards

Kesavaperumal

Former Member
0 Kudos

Do not clear the header line while entering the loop and then check the code in degugger.

Please update on the result.

Former Member
0 Kudos

Hi,

You can use AT NEW instead of ON CHANGE 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.

VivekG
Participant
0 Kudos

Hi Rajan,

While using these statements keep one thing in mind that in the internal table there should not be any field on the left of this field(on which u r using on change of) which is also changing its values.

So better keep this field at the 1st place in internal table and then try using ON CHANGE OF and then u'll see it wrkng.

hope it helps

Vivek Gupta

Former Member
0 Kudos

Hi,

Please check the following code. On change is working correctly.

You have to sort the internal table on the basis of field you have use further for 'ON CHANGE OF'.

TYPES : BEGIN OF y_itab ,

number type i ,

END OF y_itab.

DATA: itab TYPE STANDARD TABLE OF y_itab.

data : e_itab LIKE LINE OF itab.

e_itab-number = '112'.

append e_itab to itab.

e_itab-number = '112'.

append e_itab to itab.

e_itab-number = '110'.

append e_itab to itab.

e_itab-number = '110'.

append e_itab to itab.

e_itab-number = '111'.

append e_itab to itab.

e_itab-number = '111'.

append e_itab to itab.

e_itab-number = '3'.

append e_itab to itab.

LOOP AT itab INTO e_itab.

on change of e_itab-number.

write 😕 e_itab-number .

endon.

ENDLOOP.

Only on the first occurence of number,

code inside 'ON CHANGE OF' .... 'ENDON' will be executed.

kesavadas_thekkillath
Active Contributor
0 Kudos

ON CHANGE is a obsolete statement. Donot use it .

Without your code its hard to specify a solution.

Former Member
0 Kudos

Thanks for all your inputs