Skip to content

BAPI_MATERIAL_SAVEDATA

Create or extend material master records programmatically — the standard BAPI for material master maintenance.

Purpose

BAPI_MATERIAL_SAVEDATA creates new materials or extends existing ones to additional organizational levels (plant, storage location, sales org). It mirrors the MM01/MM02 transactions. Always call BAPI_TRANSACTION_COMMIT after success — the material is not saved until the commit.

Signature (key parameters)

Parameter Direction Type Description
HEADDATA Importing BAPIMATHEAD Material number, industry sector, material type
CLIENTDATA Importing BAPI_MARA Client-level data (base UOM, weight, dimensions)
CLIENTDATAX Importing BAPI_MARAX Checkbox — flag client fields to populate
PLANTDATA Importing BAPI_MARC Plant-level data (MRP type, lot size, procurement)
PLANTDATAX Importing BAPI_MARCX Checkbox for plant fields
SALESDATA Importing BAPI_MVKE Sales org / distribution channel data
SALESDATAX Importing BAPI_MVKEX Checkbox for sales fields
RETURN Exporting BAPIRET2 Single return message (header-level)
RETURNMESSAGES Tables BAPI_MATRETURN2 Detailed messages — check for E/A types

Example — create a new raw material

DATA: ls_head     TYPE bapimathead,
      ls_client   TYPE bapi_mara,
      ls_clientx  TYPE bapi_marax,
      ls_return   TYPE bapiret2,
      lt_messages TYPE STANDARD TABLE OF bapi_matreturn2,
      lt_desc     TYPE STANDARD TABLE OF bapi_makt.

" Header — material identity (Basic Data view only)
" To also create plant data add PLANTDATA/PLANTDATAX and set purchase_view/mrp_view
ls_head-material   = 'ZRAWMAT001'.   " or leave blank for number range
ls_head-ind_sector = 'M'.            " manufacturing
ls_head-matl_type  = 'ROH'.          " raw material
ls_head-basic_view = abap_true.

" Description — mandatory; at least one language required
APPEND VALUE #( langu     = sy-langu
                matl_desc = 'Demo raw material' ) TO lt_desc.

" Client data (Basic Data 1)
ls_client-base_uom   = 'KG'.
ls_client-unit_of_wt = 'KG'.
ls_client-net_weight = '4.800'.

ls_clientx-base_uom   = 'X'.
ls_clientx-unit_of_wt = 'X'.
ls_clientx-net_weight = 'X'.

CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
  EXPORTING
    headdata    = ls_head
    clientdata  = ls_client
    clientdatax = ls_clientx
  IMPORTING
    return      = ls_return
  TABLES
    materialdescription = lt_desc
    returnmessages      = lt_messages.

DATA(lv_error) = xsdbool( ls_return-type CA 'EA'
                        OR line_exists( lt_messages[ type = 'E' ] )
                        OR line_exists( lt_messages[ type = 'A' ] ) ).

IF lv_error = abap_false.
  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = abap_true.
  WRITE: / 'Material saved:', ls_head-material.
ELSE.
  CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
  IF ls_return-type CA 'EA'.
    WRITE: / ls_return-message.
  ENDIF.
  LOOP AT lt_messages INTO DATA(ls_msg) WHERE type CA 'EA'.
    WRITE: / ls_msg-message.
  ENDLOOP.
ENDIF.

Common pitfalls

Checkbox structures are mandatory

Every field set in CLIENTDATA, PLANTDATA, or SALESDATA must have 'X' in the corresponding checkbox structure. Unset fields are silently ignored — this is a common cause of missing data after what appears to be a successful save.

Views must be declared in HEADDATA

Set the BASIC_VIEW, PURCHASE_VIEW, MRP_VIEW, etc. flags in HEADDATA for each view you want to create. Without these flags the corresponding data structures are ignored even if populated.

  • Material number format: If HEADDATA-MATERIAL is blank, the system assigns the next number from the material type's number range. If you supply a number, it must be in internal format (18 chars, left-padded with leading zeros for numeric ranges).
  • Plant data requires valuation area: When passing PLANTDATA/PLANTDATAX, the BAPI internally reads the plant's valuation area (T001K). If the plant is not fully configured, it raises T001K_SINGLE_READ errors. Only pass plant data for plants that exist and are configured in your system.
  • Extending an existing material: Set the same material number in HEADDATA-MATERIAL and add only the views/org levels not yet created.
  • Description is mandatory: Pass at least one row in MATERIALDESCRIPTION with LANGU and MATL_DESC — omitting it causes "No description transferred" and the material is not created.

See also

  • Transaction MM01 — create material master manually
  • Transaction MM02 — change material master
  • Transaction MM60 — material master where-used list
  • BAPI_PO_CREATE1 — purchase order creation using material numbers

Comments