Update custom fields in material related tables (Ex: MARA, MARC, MARD) using BAPI BAPI_MATERIAL_SAVEDATA
This document only describes how to update custom fields in MARC table using BAPI_MATERIAL_SAVEDATA with an Example. The same process can be followed to update any custom fields in other material related tables.
Steps:
1) Add the custom field ZZMM_SHELF_LIFE to table MARC using append structure
2) Extend the standard structure ‘BAPI_TE_MARC’ and ‘BAPI_TE_MARCX’ with the custom field ‘ZZMM_SHELF_LIFE ‘.
3) The new custom field needs to be added to the field selection group using transaction OMSR. This setting is really important otherwise the custom field will not get updated though the BAPI returns success message. You can use transaction OMSR directly or you can navigate through SPRO settings.
4) After above settings are done, Here is the logic and pseudo code how to update the custom field using BAPI ‘BAPI_MATERIAL_SAVEDATA’.
Define variables:
DATA : wa_extensionin TYPE bapiparex, wa_extensioninx TYPE bapiparexx, it_extensionin TYPE STANDARD TABLE OF bapiparex, it_extensioninx TYPE STANDARD TABLE OF bapiparexx.
DATA: wa_headdata TYPE bapimathead, wa_plantdata TYPE bapi_marc, wa_plantdatax TYPE bapi_marcx, wa_bapi_te_marc TYPE bapi_te_marc, wa_bapi_te_marcx TYPE bapi_te_marcx,
Populate BAPI Structures
wa_headdata-material = wa_marc_update-matnr. “Material
“Number wa_plantdata-plant = „140A‟. “Plant wa_plantdatax-plant = „140A‟. “Plant
Pass custom field ZZMM_SHELF_LIFE value to the structure
wa_bapi_te_marc-plant = „140A‟. “Plant” wa_bapi_te_marc-zzmm_shelf_life = 2000 “Custom Field Value
wa_bapi_te_marcx-plant = „140A‟. “Plant wa_bapi_te_marcx-zzmm_shelf_life = „X‟. “Change Flag
Pass the structure name to the BAPI EXTENSION Structure
wa_extensionin-structure = ‘BAPI_TE_MARC’. “Extension
“structure name
“Pass plant value to the offset postion as present in structure ‘BAPI_TE_MARC’.”
wa_extensionin-valuepart1+0(4) = wa_bapi_te_marc-plant.
“Pass custom field value to the offset postion as present in structure ‘BAPI_TE_MARC’.”
wa_extensionin-valuepart1+25(4) = wa_bapi_te_marc-mm_shelf_life.
APPEND wa_extensionin TO it_extensionin.
wa_extensioninx-structure = ‘BAPI_TE_MARCX’. “Extension
“structure name
“Pass plant value to the offset postion as present in structure ‘BAPI_TE_MARCX’.”
wa_extensioninx-valuepart1+0(4) = wa_bapi_te_marc-plant.
“Pass custom field change indicator value to the offset postion as present in structure ‘BAPI_TE_MARCX’.”
wa_extensioninx-valuepart1+7(1) = wa_bapi_te_marcx-zzmm_shelf_life.
APPEND wa_extensioninx TO it_extensioninx.
Call BAPI to update the data into the data base table MARC
CALL FUNCTION ‘BAPI_MATERIAL_SAVEDATA’ EXPORTING headdata = wa_headdata plantdata = wa_plantdata plantdatax = wa_plantdatax IMPORTING return = wa_return TABLES extensionin = it_extensionin extensioninx = it_extensioninx.
IF wa_return-type CA ‘EA’
ROLLBACK WORK.
- ELSE. CALL FUNCTION ‘BAPI_TRANSACTION_COMMIT’ EXPORTING wait = ‘X’.
- ENDIF.
Hi Sir ,
Your Document is so nice.
Many Many Thanks for sharing this type of Experience with Documents.
Thanks ,
Naren K
nice document. It is very helping. Thanks for sharing man.
Hi Manoj,
I believe if you switch to HTML editor and enclose your code into a <pre>code</pre> tags, you achieve much better readability:
Code pasted from ABAP editor will keep formated.
Regards, Hynek
Define variables:
DATA :
wa_extensionin TYPE bapiparex,
wa_extensioninx TYPE bapiparexx,
it_extensionin TYPE STANDARD TABLE OF bapiparex,
it_extensioninx TYPE STANDARD TABLE OF bapiparexx.
DATA:
wa_headdata TYPE bapimathead,
wa_plantdata TYPE bapi_marc,
wa_plantdatax TYPE bapi_marcx,
wa_bapi_te_marc TYPE bapi_te_marc,
wa_bapi_te_marcx TYPE bapi_te_marcx,
Populate BAPI Structures
wa_headdata-material = wa_marc_update-matnr. “Material Number
wa_plantdata-plant = "140A‟. “Plant
wa_plantdatax-plant = "140A‟. “Plant
Pass custom field ZZMM_SHELF_LIFE value to the structure
wa_bapi_te_marc-plant = "140A‟. “Plant”
wa_bapi_te_marc-zzmm_shelf_life = 2000 “Custom Field Value
wa_bapi_te_marcx-plant ="140A‟. “Plant
wa_bapi_te_marcx-zzmm_shelf_life = „X‟. “Change Flag
Pass the structure name to the BAPI EXTENSION Structure
wa_extensionin-structure = 'BAPI_TE_MARC'. “Extension structure name
"Pass plant value to the offset postion as present in structure 'BAPI_TE_MARC'.”
wa_extensionin-valuepart1+0(4) = wa_bapi_te_marc-plant.
"Pass custom field value to the offset postion as present in structure 'BAPI_TE_MARC'.”
wa_extensionin-valuepart1+25(4) = wa_bapi_te_marc-mm_shelf_life.
APPEND wa_extensionin TO it_extensionin.
wa_extensioninx-structure = 'BAPI_TE_MARCX'. “Extension
"structure name
"Pass plant value to the offset postion as present in structure 'BAPI_TE_MARCX'.”
wa_extensioninx-valuepart1+0(4) = wa_bapi_te_marc-plant.
"Pass custom field change indicator value to the offset postion as present in structure 'BAPI_TE_MARCX'.”
wa_extensioninx-valuepart1+7(1) = wa_bapi_te_marcx-zzmm_shelf_life.
APPEND wa_extensioninx TO it_extensioninx.
Call BAPI to update the data into the data base table MARC
CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
EXPORTING
headdata = wa_headdata
plantdata = wa_plantdata
plantdatax = wa_plantdatax
IMPORTING
return = wa_return
TABLES
extensionin = it_extensionin
extensioninx = it_extensioninx.
IF wa_return-type CA 'EA'
ROLLBACK WORK.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
ENDIF.
So Great!!
Today I spent almost the full day in the office trying to figure out why my MARC custom field was not being updated.
And it was the missing OMSR customizing!! I was about to turn me crazy, but finally I'm breathing in peace again. Everything works 🙂