Question:
Hi Experts,
I have a requirement where my ON CHANGE criteria is dynamic. It is based on wa_charakeyfrmt-value which I am reading from a table. If it is 'DO' then ON CHANGE will have wa_errtabcopy-DO. Else if it is 'LI' then ON CHANGE will have wa_errtabcopy-LI. There will be many cases like this. So instead of writing so many IF/ELSE or SWITCH/CASE conditions is it possible to dynamically create ON CHANGE criteria for the last field. See the example code below:
IF wa_charakeyfrmt-value = 'DO'. "On DO change
LOOP AT i_errtabcopy into wa_errtabcopy
WHERE sold_to = wa_errtab-sold_to.
ON CHANGE OF wa_errtabcopy-key_value
OR wa_errtabcopy-rep_created_date
OR wa_errtabcopy-rep_created_time
OR wa_errtabcopy-rep_counter
OR wa_errtabcopy-DO.
ENDON.
ENDLOOP.
ELSEIF wa_charakeyfrmt-value = 'LI'. "On LI change
LOOP AT i_errtabcopy into wa_errtabcopy
WHERE sold_to = wa_errtab-sold_to.
ON CHANGE OF wa_errtabcopy-key_value
OR wa_errtabcopy-rep_created_date
OR wa_errtabcopy-rep_created_time
OR wa_errtabcopy-rep_counter
OR wa_errtabcopy-LI.
ENDON.
ENDLOOP.
|
|
|
Thanks
Gopal
Answer:
I'd have two workareas, one current and one previous and populate them dynamically (fields that aren't relevant are left blank, hence that part of the structure is alway equal); or, if there aren't many rules, create rule specific workareas with just the relevant fields and fill by move corresponding.
_________________
First he pinches my sig, Now he's cribbing my posts too!
Answer:
From the ABAP Help on ON CHANGE:
This control structure ... is particularly prone to errors and should be replaced by branches with explicitly declared auxiliary variables.
So, rather than use ON CHANGE, it is better to use variables to hold the previous value and the current one.
LOOP AT tab INTO curr.
IF curr NE prev AND sy-tabix gt 1.
" Do stuff
ENDIF.
...
prev = curr.
ENDLOOP.
" Do stuff again
For the current and previous record comparison, in your case, you want to detect different variables changing, so this functional method might help:
Signature:
IMPORTING is_thisline TYPE ANY
IMPORTING is_thatline TYPE ANY
IMPORTING it_excl TYPE string_table
RETURNING r_equal TYPE boolean
EXCEPTION component_not_found
Definition:
METHOD compare.
* IT_EXCL contains those fields to NOT consider during the
* compare
* E.g. If the line1 has fields A1,B1,C1,D1,E1, line 2 has A2, B2, C2, D2, E2
* and IT_EXCL contains B, C. Line 1 is the same as line 2 if A2 = A1, D2 = D1 and E2 = E1.
DATA: lp_data TYPE REF TO data.
FIELD-SYMBOLS: <ls_comp> TYPE ANY,
<ls_cha> TYPE string,
<ls_thischa> TYPE ANY,
<ls_thatcha> TYPE ANY,
<l_thisfld> TYPE ANY,
<l_thatfld> TYPE ANY.
* Create a copy of that line to work with. It's going to change. By
* taking a copy, we ensure that we don't change the input data
CREATE DATA lp_data LIKE is_thatline.
ASSIGN lp_data->* TO <ls_comp>.
<ls_comp> = is_thatline.
* Get the characteristics structure
ASSIGN COMPONENT c_s_chas OF STRUCTURE is_thisline TO <ls_thischa>.
ASSIGN COMPONENT c_s_chas OF STRUCTURE <ls_comp> TO <ls_thatcha>.
* Go through each component to omit - setting the value of the
* referenced characteristic in the comparison field to the same
* as the entry field
LOOP AT it_excl ASSIGNING <ls_cha>.
* Get the field for the characteristic
ASSIGN COMPONENT <ls_cha> OF STRUCTURE <ls_thischa> TO <l_thisfld>.
* Ensure component exists in the structure
IF sy-subrc IS NOT INITIAL.
MESSAGE e010 WITH <ls_cha> RAISING component_not_found.
EXIT.
ENDIF.
* Get the field for the characteristic
ASSIGN COMPONENT <ls_cha> OF STRUCTURE <ls_thatcha> TO <l_thatfld>.
* Ensure component exists in the structure
IF sy-subrc IS NOT INITIAL.
MESSAGE e010 WITH <ls_cha> RAISING component_not_found.
EXIT.
ENDIF.
* Assign the value to the comparison
<l_thatfld> = <l_thisfld>.
ENDLOOP.
* If the structures contain the same values (excepting those in the
* field list), then they are equal
CLEAR r_equal.
IF <ls_thischa> EQ <ls_thatcha>.
r_equal = 'X'.
ENDIF.
ENDMETHOD.
m@t
_________________
TULY The quality of answers is roughly proportional to the quality of the question.
The downside of being better than everyone else is that people tend to assume you're pretentious.
Answer:
...but then again, you're just a clever-clogs...
_________________
The Doc
Magna Grand Docot of the Tuly Idiot Order
2007 Basic Rules