SAP BAPI for Material Creation: BAPI_MATERIAL_SAVEDATA ABAP Generator
This generator writes a complete ABAP report that creates a material master in SAP ECC 6.0 with BAPI_MATERIAL_SAVEDATA, covering basic, sales, purchasing, MRP and accounting views. It fills each data structure and its X flag structure, handles internal or external numbering and commits only when the BAPI returns no error.
Fill in
INTERNAL calls BAPI_MATERIAL_GETINTNUMBER first.
PCE is the ISO code behind EA/ST in a standard system.
ABAP report: BAPI_MATERIAL_SAVEDATA
REPORT z_bapi_material_savedata.
* Creates a material with BAPI_MATERIAL_SAVEDATA (SAP ECC 6.0).
* Every value in a data structure needs its flag in the matching X structure.
DATA: ls_head TYPE bapimathead,
ls_mara TYPE bapi_mara,
ls_marax TYPE bapi_marax,
ls_marc TYPE bapi_marc,
ls_marcx TYPE bapi_marcx,
ls_mvke TYPE bapi_mvke,
ls_mvkex TYPE bapi_mvkex,
ls_mbew TYPE bapi_mbew,
ls_mbewx TYPE bapi_mbewx,
ls_makt TYPE bapi_makt,
lt_makt TYPE STANDARD TABLE OF bapi_makt,
ls_return TYPE bapiret2,
ls_ret1 TYPE bapireturn1,
ls_matnr TYPE bapimatinr,
lt_matnr TYPE STANDARD TABLE OF bapimatinr.
START-OF-SELECTION.
* Material number: external number, or ask the number range for one
IF 'EXTERNAL' = 'INTERNAL'.
CALL FUNCTION 'BAPI_MATERIAL_GETINTNUMBER'
EXPORTING
material_type = 'FERT'
industry_sector = 'M'
required_numbers = 1
IMPORTING
return = ls_ret1
TABLES
material_number = lt_matnr.
IF ls_ret1-type = 'E' OR ls_ret1-type = 'A'.
WRITE: / 'Internal number assignment failed:', ls_ret1-message.
RETURN.
ENDIF.
READ TABLE lt_matnr INTO ls_matnr INDEX 1.
ls_head-material = ls_matnr-material.
ELSE.
CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
EXPORTING
input = 'FG-100200'
IMPORTING
output = ls_head-material
EXCEPTIONS
OTHERS = 1.
ENDIF.
* Header: industry sector, material type and the views to create
ls_head-ind_sector = 'M'.
ls_head-matl_type = 'FERT'.
IF 'Y' = 'Y'.
ls_head-basic_view = 'X'.
ENDIF.
IF 'Y' = 'Y'.
ls_head-sales_view = 'X'.
ENDIF.
IF 'N' = 'Y'.
ls_head-purchase_view = 'X'.
ENDIF.
IF 'Y' = 'Y'.
ls_head-mrp_view = 'X'.
ENDIF.
IF 'Y' = 'Y'.
ls_head-account_view = 'X'.
ENDIF.
* Client level (MARA). BASE_UOM is the internal unit (e.g. ST for each
* in many systems); BASE_UOM_ISO takes the ISO code and is converted.
ls_mara-base_uom_iso = 'PCE'.
ls_mara-matl_group = '001'.
ls_marax-base_uom_iso = 'X'.
ls_marax-matl_group = 'X'.
* Plant level (MARC). Key field PLANT carries the value in the X structure.
ls_marc-plant = '1000'.
ls_marc-mrp_type = 'PD'.
ls_marc-mrp_ctrler = '001'.
ls_marc-lotsizekey = 'EX'.
ls_marc-proc_type = 'E'.
ls_marc-pur_group = '001'.
ls_marcx-plant = '1000'.
ls_marcx-mrp_type = 'X'.
ls_marcx-mrp_ctrler = 'X'.
ls_marcx-lotsizekey = 'X'.
ls_marcx-proc_type = 'X'.
ls_marcx-pur_group = 'X'.
* Sales level (MVKE)
ls_mvke-sales_org = '1000'.
ls_mvke-distr_chan = '10'.
ls_mvke-delyg_plnt = '1000'.
ls_mvke-item_cat = 'NORM'.
ls_mvkex-sales_org = '1000'.
ls_mvkex-distr_chan = '10'.
ls_mvkex-delyg_plnt = 'X'.
ls_mvkex-item_cat = 'X'.
* Valuation (MBEW). Assumes valuation area = plant (valuation at plant level).
ls_mbew-val_area = '1000'.
ls_mbew-val_class = '7920'.
ls_mbew-price_ctrl = 'S'.
ls_mbew-price_unit = 1.
ls_mbewx-val_area = '1000'.
ls_mbewx-val_class = 'X'.
ls_mbewx-price_ctrl = 'X'.
ls_mbewx-price_unit = 'X'.
IF 'S' = 'S'.
ls_mbew-std_price = '245.50'.
ls_mbewx-std_price = 'X'.
ELSE.
ls_mbew-moving_pr = '245.50'.
ls_mbewx-moving_pr = 'X'.
ENDIF.
* Description (MAKT). LANGU is the internal 1-character key, e.g. E.
ls_makt-langu = 'E'.
ls_makt-matl_desc = 'Hydraulic pump assembly 40 bar'.
APPEND ls_makt TO lt_makt.
* Do not pass data for views that were not requested
IF 'Y' <> 'Y'.
CLEAR: ls_mvke, ls_mvkex.
ENDIF.
IF 'Y' <> 'Y'.
CLEAR: ls_mbew, ls_mbewx.
ENDIF.
IF 'Y' <> 'Y' AND 'N' <> 'Y'.
CLEAR: ls_marc, ls_marcx.
ENDIF.
CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
EXPORTING
headdata = ls_head
clientdata = ls_mara
clientdatax = ls_marax
plantdata = ls_marc
plantdatax = ls_marcx
valuationdata = ls_mbew
valuationdatax = ls_mbewx
salesdata = ls_mvke
salesdatax = ls_mvkex
IMPORTING
return = ls_return
TABLES
materialdescription = lt_makt.
IF ls_return-type = 'E' OR ls_return-type = 'A'.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
WRITE: / 'Error:', ls_return-id, ls_return-number, ls_return-message.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
WRITE: / ls_head-material, ls_return-type, ls_return-message.
ENDIF.
Check in SE37 and SE11 on your release: BAPI_MATERIAL_SAVEDATA parameters, the BAPI_MARA, BAPI_MARC, BAPI_MVKE and BAPI_MBEW fields used, and whether your system uses MATERIAL_LONG (40-character numbers). Mandatory fields per view come from your material type and field selection (OMS2, OMSR), so a material type may need fields not in this template. Sales views often also need TAXCLASSIFICATIONS. Test in a sandbox client first.
Get your material load reviewed
Send us your material type, required views and field selection and we will check the generated program against your ECC setup before you run a migration load.
Optional. The tool and every export work without this. No spam.
How to use it
Pick external or internal numbering, then enter the material type, plant, sales area, valuation class and price. Set the views you want to Y. The report is ready to paste into SE38 as an executable program. Run it once in a test client, read the message in the list output, and check the result in MM03. For a bulk load, keep the same structure and loop over your file, calling BAPI_TRANSACTION_COMMIT per material so one bad record does not roll back the batch.
- INTERNAL numbering calls BAPI_MATERIAL_GETINTNUMBER with material type and industry sector, then uses the returned number.
- EXTERNAL numbering runs CONVERSION_EXIT_MATN1_INPUT so numeric numbers get leading zeros.
- Valuation area is set to the plant, which assumes valuation at plant level (the usual ECC setup).
How the BAPI structures map to tables
HEADDATA (BAPIMATHEAD) carries the material number, industry sector, material type and one flag per view. CLIENTDATA (BAPI_MARA) maps to MARA, PLANTDATA (BAPI_MARC) to MARC, SALESDATA (BAPI_MVKE) to MVKE, VALUATIONDATA (BAPI_MBEW) to MBEW and the MATERIALDESCRIPTION table (BAPI_MAKT) to MAKT. Each data structure has an X twin (BAPI_MARAX, BAPI_MARCX and so on). A value is written only when its X field is 'X'. Key fields are the exception: in BAPI_MARCX the PLANT field holds the plant value, in BAPI_MVKEX the sales org and channel hold their values, and in BAPI_MBEWX the valuation area holds its value.
Gotchas that cause most failures
The single RETURN parameter is a BAPIRET2 structure, not a table, and it often reports only the last message. If you need every message, add the RETURNMESSAGES table to the call. Units are the next trap: BASE_UOM expects the internal unit (for each, that is commonly ST, shown as EA in English), so the template uses BASE_UOM_ISO with PCE and lets the BAPI convert. Languages work the other way: LANGU is the internal key E, while LANGU_ISO would be EN. Finally, the BAPI respects field selection for the material type, so a required field you did not pass produces an error even if the view flag is set.
- Forgetting an X flag: the value is silently ignored.
- Sales view without a tax classification can fail on systems that require it.
- Calling COMMIT after an E message saves nothing but hides the error.
How to check the result
Open the material in MM03 and confirm each requested view exists for plant, sales area and valuation area. In SE16, MARA shows the client record, MARC the plant record, MVKE the sales record and MBEW the valuation record with VPRSV equal to your price control and STPRS or VERPR holding the price. MAKT holds the description per language. If a view is missing but no error came back, the matching view flag in HEADDATA was blank. If a field is empty although you passed it, its X flag was missing in the matching X structure.
Frequently Asked Questions
Which SAP BAPI is used for material creation?
BAPI_MATERIAL_SAVEDATA creates and changes material masters in SAP ECC. It takes HEADDATA plus one data structure and one X structure per level (client, plant, sales, valuation, storage location), with descriptions and units in tables. The same call creates a new material or extends an existing one to new plants, sales areas or valuation areas.
Why do I need BAPI_TRANSACTION_COMMIT after BAPI_MATERIAL_SAVEDATA?
BAPIs do not commit on their own. The material is written to the database only when BAPI_TRANSACTION_COMMIT runs, usually with WAIT = 'X' so the update finishes before the next step. If the RETURN type is E or A, call BAPI_TRANSACTION_ROLLBACK instead so no partial data is kept in the same logical unit of work.
How do I extend an existing material to a new plant with this BAPI?
Call BAPI_MATERIAL_SAVEDATA with the existing material number in HEADDATA, the MRP or other plant view flag set, and only PLANTDATA and PLANTDATAX filled for the new plant. Client data is not needed. The same approach extends a material to a new sales area or valuation area.
What does the X structure do in BAPI_MATERIAL_SAVEDATA?
Every data structure such as BAPI_MARC has a matching X structure such as BAPI_MARCX. Setting a field to 'X' tells the BAPI to write that field; blank means leave it alone. This lets the same BAPI change one field without clearing the rest. Key fields like PLANT carry their value, not 'X'.
If you want planners and buyers to ask your ECC system about materials in plain English on your own servers, with any creation going through BAPIs like this one after approval, we can set that up.
Related Tools
SAP ECC BAPI_ACC_DOCUMENT_POST G/L and Vendor Posting ABAP Generator
Enter company code, dates, accounts and amount to get an ABAP report that checks or posts a G/L or vendor invoice document with BAPI_ACC_DOCUMENT_POST.
Developer ToolsABAP BAPI Return Handling and Commit Generator
Wrap any BAPI call with BAPIRET2 error checking, message text building and a safe commit or rollback.
SAPSAP Material Master Creation Request Form Template
A request form for new SAP materials that collects the views, MRP and costing data master data teams need before running MM01.
SAPSAP MM and QM BAPI and API List Reference
A searchable list of the common SAP MM and QM BAPIs and S/4HANA OData APIs, what each does, and whether it needs a commit.
More Developer Tools tools
See all- SAP ECC Enhancement Transactions and Exit Tables Reference (SMOD, CMOD, SE18, MODSAP, SXC_EXIT)
- SAP ECC Data Dictionary and Repository Tables Reference (DD02L, DD03L, T100, E070, TSTC)
- SAP ECC ABAP Runtime Errors (ST22 Dumps) and Error Log Transactions Reference
- SAP ECC Background Job History ABAP Query Generator (TBTCO + TBTCP)
- SAP ECC Background Job Creation ABAP Generator (JOB_OPEN, SUBMIT, JOB_CLOSE)
- SAP ECC IDoc Segment Data Reader ABAP Generator (EDIDC, EDID4, EDIDS)
- SAP ECC Custom Inbound IDoc Function Module Skeleton Generator
- SAP ECC BAPI_SALESORDER_CHANGE ABAP Generator
- SAP ECC BAPI_PO_CHANGE ABAP Generator
Keep exploring
Free tools
- SAP ECC BAPI_PO_CHANGE ABAP GeneratorGenerate a working ABAP program that changes an SAP purchase order item with BAPI_PO_CHANGE, with POITEM/POITEMX, schedule line date conversion, test run and commit handling.
- SAP Sales Order Create BAPI ABAP Generator (BAPI_SALESORDER_CREATEFROMDAT2)Fill in order type, sales area, customer, material and quantity to get an ABAP skeleton that creates a sales order through the standard BAPI.
- SAP BAPI Production Order Create and Release ABAP TemplateFill in material, plant, order type and dates and get an ABAP skeleton that creates and releases a production order with a commit.
ERP fixes and how-tos
- How to create a material in SAP MM (MM01) and mass-change it with MM17Create a material master with MM01: material type, views, plant and valuation data, then mass-change fields with MM17. Notes for ECC and S/4HANA.
- How to Create a Material in SAP S/4HANA: MM01, Fiori and Manufacturing ViewsCreate a material in SAP S/4HANA with MM01 or the Manage Product Master Fiori app, and see which views a manufactured part needs (MRP, costing).
- How to Use BAPI_TRANSACTION_COMMIT CorrectlyBAPIs never commit their own update. Here is when to call BAPI_TRANSACTION_COMMIT with WAIT, and why a BAPI can run fine and still save nothing.
Integrations
- How to automate email into SAP ECC 6.0Turn inbound emails into SAP ECC 6.0 transactions: customer POs into sales orders, supplier confirmations, RFQs and invoices, with review and BAPI posting.
- How to add AI agents to SAP ECC 6.0How to add AI agents to SAP ECC 6.0: a tool layer over BAPIs, RFC and IDocs, human approval before any posting, and an on-prem model that never leaves your network.