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

Problem while using ON CHANGE OF event.

Former Member
0 Likes
2,283

Hi experts,

I am facing a problem when using on change of event while an internal table is looping.In my itab first field is 'racct'.The first 10 values in this field are same.But when the loop passes at the first time this event triggers and i will not get the expected result.How can i handle this problem?.The given below are the sample code which i have written.Plz help me to solve this problem.

LOOP AT it_final INTO wa_final.

IF wa_final-prctr BETWEEN 'PC1000000' AND 'PC1000999'.

sum1 = sum1 + wa_final-hsl.

ELSEIF wa_final-prctr BETWEEN 'PC2000000' AND 'PC2000999'.

sum2 = sum2 + wa_final-hsl.

ENDIF.

ON CHANGE OF wa_final-racct.

wa_sum-gl_no = g_old_glno.

wa_sum-amount1 = sum1.

wa_sum-amount2 = sum2. .

APPEND wa_sum TO it_sum.

ENDON.

ENDLOOP.

1 ACCEPTED SOLUTION
Read only

RaymondGiuseppi
Active Contributor
0 Likes
1,712

Check documentation on [ON CHANGE OF statement|http://help.sap.com/abapdocu_70/en/ABAPON.htm], it will be executed

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.

Look at [AT statement|http://help.sap.com/abapdocu_70/en/ABAPAT_ITAB.htm] and try something like

  SORT it_final BY racct.
  LOOP AT it_final INTO wa_final.
    AT NEW racct.
      CLEAR: sum1,
             sum2.
    ENDAT.
    IF wa_final-prctr BETWEEN 'PC1000000' AND 'PC1000999'.
      sum1 = sum1 + wa_final-hsl.
    ELSEIF wa_final-prctr BETWEEN 'PC2000000' AND 'PC2000999'.
      sum2 = sum2 + wa_final-hsl.
    ENDIF.
    AT END OF wa_final-racct.
      wa_sum-gl_no = g_old_glno.
      wa_sum-amount1 = sum1.
      wa_sum-amount2 = sum2. .
      APPEND wa_sum TO it_sum.
    ENDAT.
  ENDLOOP.

Regards,

Raymond

Hi experts,

I am facing a problem when using on change of event while an internal table is looping.In my itab first field is 'racct'.The first 10 values in this field are same.But when the loop passes at the first time this event triggers and i will not get the expected result.How can i handle this problem?.The given below are the sample code which i have written.Plz help me to solve this problem.

LOOP AT it_final INTO wa_final.

IF wa_final-prctr BETWEEN 'PC1000000' AND 'PC1000999'.

sum1 = sum1 + wa_final-hsl.

ELSEIF wa_final-prctr BETWEEN 'PC2000000' AND 'PC2000999'.

sum2 = sum2 + wa_final-hsl.

ENDIF.

ON CHANGE OF wa_final-racct.

wa_sum-gl_no = g_old_glno.

wa_sum-amount1 = sum1.

wa_sum-amount2 = sum2. .

APPEND wa_sum TO it_sum.

ENDON.

ENDLOOP.

9 REPLIES 9
Read only

rathishr_nair
Explorer
0 Likes
1,712

Instead of Using ON CHANGE OF, use

AT END OF racct.

wa_sum-gl_no = g_old_glno.

wa_sum-amount1 = sum1.

wa_sum-amount2 = sum2. .

APPEND wa_sum TO it_sum.

clear wa_sum.

ENDAT.

Thanks

Rathish

Read only

0 Likes
1,712

Thank you Rathish ...I solved the problem ...thank u very much

Regards

Suresh Paul

Read only

deepak_dhamat
Active Contributor
0 Likes
1,712

LOOP AT it_final INTO wa_final.

IF wa_final-prctr BETWEEN 'PC1000000' AND 'PC1000999'.

sum1 = sum1 + wa_final-hsl.

ELSEIF wa_final-prctr BETWEEN 'PC2000000' AND 'PC2000999'.

sum2 = sum2 + wa_final-hsl.

ENDIF.

ON CHANGE OF wa_final-racct.

wa_sum-gl_no = g_old_glno.

wa_sum-amount1 = sum1.

wa_sum-amount2 = sum2. .

APPEND wa_sum TO it_sum.

ENDON.

ENDLOOP.

data : test(10) type  C value 'AC100002' .

DATA : TEST2(10) TYPE  N .

data : test1 type  p  .


TEST2 = TEST .

TEST1 = TEST2 .



IF TEST1 >= 100000 AND TEST1 <=109999 .

ENDIF.

USE THIS LOGIC ...

REGARDS

dEEPAK.

Read only

RaymondGiuseppi
Active Contributor
0 Likes
1,713

Check documentation on [ON CHANGE OF statement|http://help.sap.com/abapdocu_70/en/ABAPON.htm], it will be executed

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.

Look at [AT statement|http://help.sap.com/abapdocu_70/en/ABAPAT_ITAB.htm] and try something like

  SORT it_final BY racct.
  LOOP AT it_final INTO wa_final.
    AT NEW racct.
      CLEAR: sum1,
             sum2.
    ENDAT.
    IF wa_final-prctr BETWEEN 'PC1000000' AND 'PC1000999'.
      sum1 = sum1 + wa_final-hsl.
    ELSEIF wa_final-prctr BETWEEN 'PC2000000' AND 'PC2000999'.
      sum2 = sum2 + wa_final-hsl.
    ENDIF.
    AT END OF wa_final-racct.
      wa_sum-gl_no = g_old_glno.
      wa_sum-amount1 = sum1.
      wa_sum-amount2 = sum2. .
      APPEND wa_sum TO it_sum.
    ENDAT.
  ENDLOOP.

Regards,

Raymond

Read only

0 Likes
1,712

Thank you Raymond ...I solved the problem ...thank u very much

Regards

Suresh Paul

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
1,712

Donot use ON CHANGE its obsolete .

Read only

0 Likes
1,712

Not actually obsolete, ON CHANGE is not allowed in classes. So it is not more (and not less) obsolete that non-OO programming

Regards,

Raymond

Read only

0 Likes
1,712

I dont understand ,actually whats the use of ON CHANGE OF....when the loop passes the first time i will trigger whatever the value...than whats the use...otherwise we should handle it.

suresh paul

Read only

0 Likes
1,712

- ON CHANGE OF is triggered every time the value changes, first time it compares the current value with initial value (space, zero depend on type of data)

- ON CHANGE is not limited to LOOP/ENDLOOP but can be used in SELECT/ENDSELECT or other such statements.

- It is no more than storing the current value in hidden fields, and comparing then those previous values to the current one. We can easily build our own buffered values and program a similar behavior.

LOOP AT itab.
  ON CHANGE OF itab-field.
*  ...
  ENDON.
ENDLOOP.

CLEAR buffer.
LOOP AT itab.
  IF itab-field NE buffer-field.
*  ...
  ENDIF.
  buffer-field = itab-field.
ENDLOOP.

- Despite AT/ENDAT you can use any fields of the structure behind, in AT/ENDAT the fields after the "key" field are hidden in the processing block. There is also no need to SORT an internal table before using ON CHANGE.

Regards,

Raymond