What is the best solution to update the MARA table?

Georges O. picture Georges O. · Oct 31, 2016 · Viewed 8.5k times · Source

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:

  1. Select one MARA entry from the table (OK)
  2. Edit fields (OK)
  3. Check for each fields, if the new value is already on the available values
  4. 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 :)

Answer

Gert Beukema picture Gert Beukema · Oct 31, 2016

Solution 2. Period. For details see the great comments from @chrisian and @dirk-trilsbeek.

In regards to your follow up question about the field mapping. The more user friendly names are great for people using the BAPI's from outside of SAP but they do make it hard to map to the fields we know inside SAP. Luckily SAP used the same data elements most of the time in these cases so that is one way to match them. Otherwise the different BAPIs often have conversion function modules to translate from the BAPI fields to the database fields. For the material BAPI you mention you can check the subroutines <TABLE>_UEBERGEBEN (this translates to <TABLE>_TRANSFER) where <TABLE> is MARA, MARC etc. Note that for instance the MARA_UEBERGEBEN routine calls the FM MAP2I_BAPI_MARA_TO_MARA_UEB and this FM has the translation from the BAPI structure to MARA, e.g. NET_WEIGHT maps to NTGEW.