Search code examples
abapsap-data-dictionary

What is the best solution to update the MARA table?


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 :)


Solution

  • Finally this is the solution choosen. We used BAPI_MATERIAL_SAVEDATA with the extension. I commented the code to help you if you want to use it :)

    FORM update_mara
      USING
        us_result TYPE mara. "Input entry of mara with masterdata to update in the system
    
      DATA :  ls_mara           TYPE bapi_te_mara,
              ls_head           TYPE bapimathead,
              lt_extensionin    TYPE TABLE OF bapiparex,
              ls_extensionin    TYPE bapiparex,
              lt_extensioninx   TYPE TABLE OF bapiparexx,
              ls_extensioninx   TYPE bapiparexx,
              ls_return         TYPE bapiret2,
              lv_x              TYPE char41,
              lv_mara           TYPE string. "Container for all data of ls_mara
    
      " MASTERDATA PART
      MOVE-CORRESPONDING us_result TO ls_mara.
      ls_mara-material = ls_head-material = us_result-matnr.
      "At least the mendatory key field: 'material'
    
      " DATA PART
      " Use extension BAPI_TE_MARA
      ls_extensionin-structure = 'BAPI_TE_MARA'.
      lv_mara = ls_mara.
      " Fill data into the extension
      ls_extensionin+30(960) = lv_mara.
      APPEND ls_extensionin TO lt_extensionin.
    
      " FLAG PART
      CLEAR lv_x.
      DO 41 TIMES. "Don't forget to change 'lv_x TYPE char41' if needed
        CONCATENATE lv_x 'X' INTO lv_x.
        "Add an 'X' where you want to activate the update
        "Here, it's for all fields
      ENDDO. " End of the do loop for all fields of the mara
    
      " Use extension BAPI_TE_MARAX
      ls_extensioninx-structure  = 'BAPI_TE_MARAX'.
      ls_extensioninx-valuepart1(18)  = us_result-matnr.
      ls_extensioninx-valuepart1+19   = lv_x.
      " The first part (18) of the extensioninx is the matnr and the next ones some 'X' flags to activate the update or not.
      APPEND ls_extensioninx TO lt_extensioninx.
    
      " PROCESS PART
      CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
        EXPORTING
          headdata     = ls_head
        IMPORTING
          return       = ls_return
        TABLES
          extensionin  = lt_extensionin
          extensioninx = lt_extensioninx
      .
    
      IF ls_return-type = 'E'.
        CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
      ELSE.
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
            wait = 'X'.
      ENDIF.  " End if of check of BAPI result
    
    ENDFORM.                    " UPDATE_MARA