cancel
Showing results for 
Search instead for 
Did you mean: 

Check for duplicate(primary key) before inserting to ztable via alv grid.

former_member300568
Participant

I have a Ztable which has 14 fields out of which the first 5 are key fields.

I also have a z program which displays a alv Grid. We can add entries to this ztable via this Z program by pressing the (+) button via the tool bar.

So when i enter a bunch of value via the grid ( copy paste) i get all the values through internal table. This itab is expected to have 10 k lines of data already plus the one we are adding via the alv grid.

I have itab X which has 1000 lines and through ALV GRID i am inserting 20 lines. So not itab y has 1020 lines. Before committing i.e inserting to ztable i should check for duplicates. The z table has first 6 fields as primary key.

Key 1 ,2 and 3 are company code , sales org and another value through selection screen which will be taken care through validation.

My Code is like this, but it is allowing duplicates.I would like to have best approach.

This is because i have sorted by erdat and it still allows duplicates. Any ideas will be greatly helpful.

  DESCRIBE TABLE datatab_y LINES lv_y.( already existing plus new ones)
  DESCRIBE TABLE datatab_x LINES lv_x. ( already existing in the grid)

* total number of lines inserted
* thought i could to do cnt times to check

  cnt = lv_y - lv_x.

  SORT datatab_y by erdat DESCENDING.

loop at datatab_y.
IF datatab_y-key3 IS NOT INITIAL.
   at new key3.
    new_row = sy-tabix.
  endat.
  at end of key3.
    check sy-tabix > new_row.
    lv_dupflg = datatab_y-key3.
    perform send_dupkey_msg USING lv_dupflg.
  endat.
ELSEIF datatab_y-key4 IS NOT INITIAL.
   at new key4.
    new_row = sy-tabix.
  endat.
  at end of key4.
    check sy-tabix > new_row.
    lv_dupflg = datatab_y-key4.
    perform send_dupkey_msg USING lv_dupflg.
  endat.
ENDIF.

former_member300568
Participant
0 Kudos

jelena.perfiljeva Your insight will be appreciated. I am in a position to use loop inside loop which i dont want to use. Please share your Thoughts. This is a code to check for duplicates.

Accepted Solutions (1)

Accepted Solutions (1)

DoanManhQuynh
Active Contributor
0 Kudos

you can consider to check duplicate right after user finish input the new line ( in event data change of oo alv ). it just simple compare the key fields, no need to loop.

if you still want to check in one go like you are doing now, you should loop through your inserted records then read the original itab with those key, dont kow why you have to go through AT NEW, AT END.

anw, you can check this example report for more info: BCALV_EDIT_04

former_member300568
Participant
0 Kudos

@Quynh Doan Manh Thanks, I am having the alv modified rows: after-images in a itab(lt_data) and i am calling my perform to check for duplicates using this itab.

However when i try to enter value i get a dump "CL_GUI_ALV_GRID===============CP"

LOOP AT lt_data INTO ls_data.
    READ TABLE lt_exist WITH KEY
       attrid = ls_data-attrid
       bukrs  = ls_data-bukrs
       prdha  = ls_data-prdha
       matnr  = ls_data-matnr
       datbi  = ls_data-datbi
       BINARY SEARCH.
    IF ls_data-matnr IS NOT INITIAL.
      lv_dupflg = ls_data-matnr.
    ELSEIF  ls_data-prdha IS NOT INITIAL.
      lv_dupflg = ls_data-prdha.
    ENDIF.
    IF sy-subrc eq 0.
      PERFORM send_dupkey_msg USING lv_dupflg.
      exit.
    ENDIF.
ENDLOOP.
DoanManhQuynh
Active Contributor
0 Kudos

You should goto ST22 to check more detail on that dump, with only "CL_GUI_ALV_GRID===============CP" i cant tell what problem.

Answers (1)

Answers (1)

former_member539238
Participant
0 Kudos

If you want new data with no duplicates then you can follow the below steps.

DATA: lt_list.

DATA: lt_list_final.

Field-symbol <fs> like line of lt_list_final.

Loop through the lt_list.

read table lt_list final assigning <fs> with key values = lt_list entry.

if sy-subrc = 0.

"change the values as needed in <fs>. Because entry already exists

else.

"Create a new structure and append to lt_list_final. Because entry does not exist

endif.

endloop.