2008 Jan 15 9:16 PM
Greeting Fellow Abapers,.
I have been running into an issue that I need some advice on. I am allowing users to edit a Ztable via ALV. When the quantity field is updated everything is fine. However, when the field "lic_plate" is edited the code appends another row into the Ztable instead of modifying it. Also, when the delete row functionality of the grid is used the row is not deleted from the Ztable.
Here is the ztable structure. The fields I allow to be edited are in BOLD...
ZPALLET-VBELN
ZPALLET-MATNR
ZPALLET-LINE_NUM
ZPALLET-LIC_PLATE
ZPALLET-LOT_NUMBER
ZPALLET-PAL_TYPE
ZPALLET-MAN_DATE
ZPALLET-QUANTITY
ZPALLET-PROC_DATE
Here is the source code that the APPEND is taking place...
FORM save_database .
Getting the selected rows index*
CALL METHOD o_grid->get_selected_rows
IMPORTING
et_index_rows = i_selected_rows.
Through the index capturing the values of selected rows*
LOOP AT i_selected_rows INTO w_selected_rows.
READ TABLE itab INTO wa INDEX w_selected_rows-index.
IF sy-subrc EQ 0.
MOVE-CORRESPONDING wa TO w_modified.
APPEND w_modified TO i_modified.
ENDIF.
ENDLOOP.
IF sy-subrc = 0.
MODIFY zpallet FROM TABLE i_modified.
ENDIF.
ENDFORM.
Please help. I am in your debt.
...as always, points will be awarded.
Best,
Dan
2008 Jan 16 4:34 AM
Hello Dan
When you are using an editable ALV for table maintenance you have to take care that the users
- cannot edit the key fields of existing DB records and
- every new record (row) does not match any existing record (i.e. has identical key field values)
Instead of relying on selected rows for the DB update I would recommend to store a "PBO image" of your data and compare this with the "PAI image" of the data as soon as the user pushes the SAVE button.
Example:
DATA:
gt_outtab_pbo TYPE < your table type>, " PBO image
gt_outtab TYPE < your table type>. " PAI image
" 1. Select data from DB table and store in both itabs:
SELECT * ... INTO TABLE gt_outtab.
gt_outtab_pbo = gt_outtab.
" 2. Display editable ALV list -> user modifies gt_outtab
" 3. SAVE function requested
" ... compare gt_outtab vs. gt_outtab_pbo
" .... INSERT, UPDATE, or DELETE DB records
" Finally set:
gt_outtab_pbo = gt_outtab.
" 2. User continues with editing
In order to compare PBO vs. PAI data you may have a look at my sample coding:
[Comparing Two Internal Tables - A Generic Approach|https://www.sdn.sap.com/irj/sdn/wiki?path=/display/snippets/comparing%2btwo%2binternal%2btables%2b-%2ba%2bgeneric%2bapproach]
Regards,
Uwe
2008 Jan 15 11:52 PM
What's the primary key to ZPALLET? My guess would be, because you are using a "modify dbtab" construct rather than an "update dbtab", then SAP is doing an insert when it doesn't match the primary key... I'd suggest a little re-write around the "modify" to instead use discrete "insert" and "update" statements so you can tell what's happening exact.y.
"The MODIFY statement inserts one or several lines specified in source in the database table specified in target, or overwrites existing lines."
Jonathan
2008 Jan 16 4:48 PM
Jonathon,
Thanks for your reply.
The key fields are as follows...
ZPALLET-VBELN
ZPALLET-MATNR
ZPALLET-LINE_NUM
ZPALLET-LIC_PLATE
ZPALLET-LOT_NUMBER
This must be why the "LIC_PLATE" record is being appended instead of modifiied.
I am able to duplicate the row using the built in ALV functionality then update the ZTABLE however, a DELETE statement is not processing.
Is there some way to FLAG the deleted record or maybe compare the edited ITAB then remove the record from the ZTABLE?
Thanks,
Dan
2008 Jan 16 4:34 AM
Hello Dan
When you are using an editable ALV for table maintenance you have to take care that the users
- cannot edit the key fields of existing DB records and
- every new record (row) does not match any existing record (i.e. has identical key field values)
Instead of relying on selected rows for the DB update I would recommend to store a "PBO image" of your data and compare this with the "PAI image" of the data as soon as the user pushes the SAVE button.
Example:
DATA:
gt_outtab_pbo TYPE < your table type>, " PBO image
gt_outtab TYPE < your table type>. " PAI image
" 1. Select data from DB table and store in both itabs:
SELECT * ... INTO TABLE gt_outtab.
gt_outtab_pbo = gt_outtab.
" 2. Display editable ALV list -> user modifies gt_outtab
" 3. SAVE function requested
" ... compare gt_outtab vs. gt_outtab_pbo
" .... INSERT, UPDATE, or DELETE DB records
" Finally set:
gt_outtab_pbo = gt_outtab.
" 2. User continues with editing
In order to compare PBO vs. PAI data you may have a look at my sample coding:
[Comparing Two Internal Tables - A Generic Approach|https://www.sdn.sap.com/irj/sdn/wiki?path=/display/snippets/comparing%2btwo%2binternal%2btables%2b-%2ba%2bgeneric%2bapproach]
Regards,
Uwe
2008 Jan 17 9:07 PM
Uwe,
I have successfully implemented your Class to achieve the results I wanted. Thank you so much for your time.
Regards,
Dan
2008 Jan 17 9:09 PM
Hello Dan
I am glad that there is at least a second developer out there in the SAP/SDN world who uses my class functionality...
Kind Regards,
Uwe
2008 Jan 16 5:31 AM
Dan,
Try with the code like below
DATA : v_flag.
LOOP AT i_selected_rows INTO w_selected_rows.
READ TABLE itab INTO wa INDEX w_selected_rows-index.
IF sy-subrc EQ 0.
v_flag = 'X'
MOVE-CORRESPONDING wa TO w_modified.
APPEND w_modified TO i_modified.
ENDIF.
ENDLOOP.
IF v_flag = 'X'.
UPDATE zpallet FROM TABLE i_modified.
ENDIF.
ENDFORM.
****For Delete also,
LOOP AT i_selected_rows INTO w_selected_rows.
READ TABLE itab INTO wa INDEX w_selected_rows-index.
IF sy-subrc EQ 0.
v_flag = 'X'
MOVE-CORRESPONDING wa TO w_modified.
APPEND w_modified TO i_modified.
ENDIF.
ENDLOOP.
IF v_flag = 'X'.
DELETE zpallet FROM TABLE itab..
ENDIF.
ENDFORM.
Note : itab should have the same structure like database table "zpallet "
Don't forget to reward if useful.....
2008 Jan 17 2:59 PM
Muralikrishna,
Thank you for your reply. If you look at my response to Jonathon, the fields noted are key fields so I think the Update and Delete statements will not work. I am at a loss here. I do not want to allow the entire grid to be editable. We only want the user to edit the two noted fields.
With your sample code I flag the V_FLAG successfully however we cannot process UPDATE/DELETE and recieve SY-SUBRC = 4. Still, MODIFY statement works.
Thank you in advance,
Dan