I'm facing to a problem today: how to update the MARA table with custom and non-custom fields?
I found some solutions, but I would like to know what is the best solution.
I came from HCM module. On this module we have changelogs. So I would like to update the MARA table with log changes if possible.
Context:
- Select one MARA entry from the table (OK)
- Edit fields (OK)
- Check for each fields, if the new value is already on the available values
- Update the table
Logic:
DATA:
lt_mara TYPE TABLE OF mara,
ls_mara TYPE mara.
lv_matnr = '000000000024856';
* Seelct data
"" matnr from CONVERSION_EXIT_MATN1_INPUT
SELECT SINGLE * INTO ls_mara FROM mara WHERE matnr = lv_matnr.
* Modification
ls_mara-vlumn = '999.9'.
"ls_mara-z* = '...'.
* Checks : volumn is numeric, ...
" [...]
* Update
" [...]
I have only information about the MARA, no data about related tables.
Solution 1 - MATERIAL_MAINTAIN_DARK
Use function module MATERIAL_MAINTAIN_DARK
.
CALL FUNCTION 'MATERIAL_MAINTAIN_DARK'
EXPORTING
kz_activ_cad = blank
flag_muss_pruefen = fest_x
sperrmodus = fest_e
max_errors = 0
p_kz_no_warn = fest_x " 'N' ?
kz_prf = blank " 's' ?
kz_verw = fest_x
kz_aend = fest_x
kz_dispo = fest_x
kz_test = blank
kz_mdip = blank
kz_mprp = blank
kz_ale = blank
kz_actv = blank
TABLES
AMARA_UEB = TMARA_UEB
AMERRDAT = lt_amerrdat
EXCEPTIONS
OTHERS = 7.
" Loop lt_amerrdat.
" CALL FUNCTION 'RPY_MESSAGE_COMPOSE' [...]
" WRITE:/ lv_errmsg.
" ROLLBACK WORK.
" or
" CALL FUNCTION 'DB_COMMIT'.
(I used this logic of code https://archive.sap.com/discussions/thread/169786)
Problem: I successfully executed the code, but now I caught some functional errors. If I correctly understand the functionality of this FM, the modification will be executed through a tcode (i.e.: MM01/02/03). But, I don't know what was the initial tcode for each row and I have functional issues (i.e: Article category not correct, ...) depending of the tcode used.
Do you know how I can skip these checks? Or known the initial tcode?
Solution 2 - BAPI_MATERIAL_SAVEDATA
Use function module BAPI_MATERIAL_SAVEDATA
.
This FM allow to update MARA table with standard fields + custom (via EXTENSION(X))
For information, my BAPI_TE_MARA
& BAPI_TE_MARAX
looks like:
- MATERIAL (MATNR, char, 18)
- .APPEND (ZBAPI_TE_MARAX)
- NOCHANGE (BAPIUPDATE, char, 1)
I guess I have to add each Z* fields on it, before use this FM ? Moreover, I didn't find a solution to update the fields of the table. If I'm checking the FM, I have some objects but the columns names are not the same. How I can find the mapping between the fields on this FM and the fields on the MARA table ?
Solution 3
Has I make my checks on integrity on my code, I guess I can use a simple INSERT/UPDATE (MODIFY) ? This should be the simplest solution.
CONCATENATE sy-mandt lv_matnr INTO lv_mara_key.
" Lock object
CALL FUNCTION 'ENQUEUE_E_TABLE'
EXPORTING
MODE_RSTABLE = 'E'
tabname = 'MARA'
varkey = lv_mara_key
EXCEPTIONS
foreign_lock = 1 system_failure = 2 OTHERS = 3.
ls_mara-ernam = sy-uname.
" ...
" std & custo
MODIFY mara FROM ls_mara.
" Unlock object
CALL FUNCTION 'DEQUEUE_E_TABLE'
EXPORTING
MODE_RSTABLE = 'E'
tabname = 'MARA'
varkey = lv_mara_key
EXCEPTIONS
foreign_lock = 1 system_failure = 2 OTHERS = 3.
I'm interested in all recommendations, tutorials or advices :)