2019 Oct 10 5:57 PM
Hello all,
SAP ECC 6.0 (SAP_ABA 701), so cannot use secondary keys in internal tables... 😞
We have a report to maintain data of table MLEA.
One of the requirement is that In this table the field EAN11 must be unique, same value in any other record is not allowed.
The report have a selection screen so may be not all the database records are displayed.
We need to check if any line of the ALV have the same value in field EAN11, then error.
If not, we need to check if any record in database have the same value in field EAN11, but before to mark as erroneous, we need to check if the same record is in the ALV and have other value in EAN11 (the user could have modified the value in the report...)
---
I dont like the solution of the problem.
Any help improving the code?
DATA:
lt_alv TYPE STANDARD TABLE OF mlea,
lt_alv_sort_keys TYPE SORTED TABLE OF mlea WITH UNIQUE KEY matnr meinh lifnr lfnum,
lt_alv_sort_ean TYPE SORTED TABLE OF mlea WITH UNIQUE KEY ean11,
lt_ean_db TYPE SORTED TABLE OF mlea WITH NON-UNIQUE KEY ean11.
INSERT LINES OF lt_alv INTO TABLE lt_alv_sort_keys.
INSERT LINES OF lt_alv INTO TABLE lt_alv_sort_ean.
* Select DB records with same EAN
IF lt_alv IS NOT INITIAL.
SELECT matnr meinh lifnr lfnum ean11
INTO CORRESPONDING FIELDS OF TABLE lt_mlea_db
FROM mlea
FOR ALL ENTRIES IN lt_alv
WHERE ean11 EQ lt_alv-ean11.
ENDIF.
LOOP AT lt_alv ASSIGNING <ls_alv>.
* Check if other lines in ALV have the same EAN
LOOP AT lt_alv_sort_ean
ASSIGNING <ls_alv_sort_ean>
WHERE ean11 EQ <ls_alv>-ean11 "same value in EAN11
AND ( matnr NE <ls_alv>-matnr "different key fields
OR meinh NE <ls_alv>-meinh
OR lifnr NE <ls_alv>-lifnr
OR lfnum NE <ls_alv>-lfnum ).
"ERROR
EXIT.
ENDLOOP.
IF sy-subrc NE 0.
* Check if any DB record has the same EAN
LOOP AT lt_ean_db
ASSIGNING <ls_ean_db>
WHERE ean11 EQ <ls_alv>-ean11.
* Check is not the same record
CHECK <ls_alv>-matnr NE <ls_ean_db>-matnr
OR <ls_alv>-meinh NE <ls_ean_db>-meinh
OR <ls_alv>-lifnr NE <ls_ean_db>-lifnr
OR <ls_alv>-lfnum NE <ls_ean_db>-lfnum.
* Check if exists in ALV with other value
READ TABLE lt_alv_sort_keys
ASSIGNING <ls_alv_sort_keys>
WITH KEY matnr = <ls_alv>-matnr
meinh = <ls_alv>-meinh
lifnr = <ls_alv>-lifnr
lfnum = <ls_alv>-lfnum.
IF sy-subrc NE 0
OR sy-subrc EQ 0 AND <ls_alv_sort_keys>-ean11 EQ <ls_alv>-ean11.
"ERROR
EXIT.
ENDIF.
ENDLOOP.
ENDIF.
ENDLOOP.
Thanks in advance.
Hello all,
SAP ECC 6.0 (SAP_ABA 701), so cannot use secondary keys in internal tables... 😞
We have a report to maintain data of table MLEA.
One of the requirement is that In this table the field EAN11 must be unique, same value in any other record is not allowed.
The report have a selection screen so may be not all the database records are displayed.
We need to check if any line of the ALV have the same value in field EAN11, then error.
If not, we need to check if any record in database have the same value in field EAN11, but before to mark as erroneous, we need to check if the same record is in the ALV and have other value in EAN11 (the user could have modified the value in the report...)
---
I dont like the solution of the problem.
Any help improving the code?
DATA:
lt_alv TYPE STANDARD TABLE OF mlea,
lt_alv_sort_keys TYPE SORTED TABLE OF mlea WITH UNIQUE KEY matnr meinh lifnr lfnum,
lt_alv_sort_ean TYPE SORTED TABLE OF mlea WITH UNIQUE KEY ean11,
lt_ean_db TYPE SORTED TABLE OF mlea WITH NON-UNIQUE KEY ean11.
INSERT LINES OF lt_alv INTO TABLE lt_alv_sort_keys.
INSERT LINES OF lt_alv INTO TABLE lt_alv_sort_ean.
* Select DB records with same EAN
IF lt_alv IS NOT INITIAL.
SELECT matnr meinh lifnr lfnum ean11
INTO CORRESPONDING FIELDS OF TABLE lt_mlea_db
FROM mlea
FOR ALL ENTRIES IN lt_alv
WHERE ean11 EQ lt_alv-ean11.
ENDIF.
LOOP AT lt_alv ASSIGNING <ls_alv>.
* Check if other lines in ALV have the same EAN
LOOP AT lt_alv_sort_ean
ASSIGNING <ls_alv_sort_ean>
WHERE ean11 EQ <ls_alv>-ean11 "same value in EAN11
AND ( matnr NE <ls_alv>-matnr "different key fields
OR meinh NE <ls_alv>-meinh
OR lifnr NE <ls_alv>-lifnr
OR lfnum NE <ls_alv>-lfnum ).
"ERROR
EXIT.
ENDLOOP.
IF sy-subrc NE 0.
* Check if any DB record has the same EAN
LOOP AT lt_ean_db
ASSIGNING <ls_ean_db>
WHERE ean11 EQ <ls_alv>-ean11.
* Check is not the same record
CHECK <ls_alv>-matnr NE <ls_ean_db>-matnr
OR <ls_alv>-meinh NE <ls_ean_db>-meinh
OR <ls_alv>-lifnr NE <ls_ean_db>-lifnr
OR <ls_alv>-lfnum NE <ls_ean_db>-lfnum.
* Check if exists in ALV with other value
READ TABLE lt_alv_sort_keys
ASSIGNING <ls_alv_sort_keys>
WITH KEY matnr = <ls_alv>-matnr
meinh = <ls_alv>-meinh
lifnr = <ls_alv>-lifnr
lfnum = <ls_alv>-lfnum.
IF sy-subrc NE 0
OR sy-subrc EQ 0 AND <ls_alv_sort_keys>-ean11 EQ <ls_alv>-ean11.
"ERROR
EXIT.
ENDIF.
ENDLOOP.
ENDIF.
ENDLOOP.
Thanks in advance.
2019 Oct 10 9:12 PM
Typo error: I think lt_ean_db and lt_mlea_db should be the same internal table.
The code is buggy because you defined lt_alv_sort_keys and lt_alv_sort_ean with a UNIQUE key. It should be NON-UNIQUE.
Moreover, lt_ean_db is not needed, you can use lt_alv_sort_ean instead.
Apart that, I don't see a better solution (eventually a little performance gain by initializing the "index tables" with lt_alv and updating only the lines corresponding to values changed, but I think it's overkill: tiny performance gain versus worse code maintainability).
2019 Oct 10 11:13 PM
Sorry for the typos, This is a simplified code from the original report, I've writen it very quick only to post this question. The name of the variables are different from the original report.
You are right, lt_alv_sort_ean must have NON-UNIQUE KEY. And lt_ean_db is lt_mlea_db, i've fixed it.
There is other typo in the code, the last READ should use <ls_ean_db> not <ls_alv>.
But, lt_ean_db and lt_alv_sort cannot be the same table. Because you dont have in lt_alv all the records from the database,only the selection from the selection screen (not in this code snipped). And the user can add manually other new lines in the alv. So you need to select the values from the db before the validations. And may be you have the same record from DB in your ALV but with the value changed...
2019 Oct 11 2:00 AM
From ABAP 7.4 I think you could try LOOP...GROUP BY until ean11 to find the group size and from there know which records been duplicated.
So for the same ideal, with older version, maybe AT NEW...AT END could do the same. Ideal is you check the duplicated record of ALV first by check the collected record at AT END event, remove it or do what ever there. do the same with records selected from DB.
| User | Count |
|---|---|
| 4 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |