Sub topic 18
Reading and maintaining materials from code
This chapter assumes you can read basic ABAP and know the table layout from the earlier chapters: MARA at client level, MAKT for descriptions, MARC at plant level, MARD at storage location level, and MBEW for valuation. You also need to know what views and organizational levels are. Here you will read material data with ABAP SQL and create or change materials with BAPI_MATERIAL_SAVEDATA.
Choosing read or write access
Reading and writing follow different rules.
- Reading can go straight to the tables with ABAP SQL. Nothing is locked or changed, so this is safe and fast.
- Writing must go through a released interface such as a BAPI. A BAPI runs the same checks as the standard transactions. It applies the field rules of the material type, checks numbering, writes change documents, and fills dependent tables. A direct
INSERTorUPDATEon MARA or MARC skips all of that and leaves the data inconsistent.
Never write to material master tables directly.
Reading material data with ABAP SQL
The material master is split across tables, so a useful read is usually a join. The key is the material number (MATNR). Plant data adds WERKS. Descriptions add the language SPRAS.
SELECT mara~matnr,
makt~maktx,
mara~mtart,
marc~werks,
marc~dismm
FROM mara
INNER JOIN makt ON makt~matnr = mara~matnr
AND makt~spras = @sy-langu
INNER JOIN marc ON marc~matnr = mara~matnr
WHERE marc~werks = '1010'
INTO TABLE @DATA(lt_materials)
UP TO 100 ROWS.This query lists materials that are extended to plant 1010. It returns the material type (MTART) and the MRP type (DISMM).
Each join choice has a consequence:
- Description join. MAKT has one row per material and language. Without the
sprascondition, the join returns one row per language. - Inner join to MARC. Materials that exist only at client level are dropped. Use
LEFT OUTER JOINif you want them listed with empty plant fields. - Row limit.
UP TO n ROWSis a cap, not a sort. AddORDER BYif you need predictable results.
Where other data lives
| Data you need | Table | Key |
|---|---|---|
| General data, type, base unit | MARA | MATNR |
| Descriptions | MAKT | MATNR, SPRAS |
| Plant data (purchasing, MRP) | MARC | MATNR, WERKS |
| Storage location data | MARD | MATNR, WERKS, LGORT |
| Valuation and price control | MBEW | MATNR, BWKEY, BWTAR |
S/4HANA notes for reading
- The material number field is 40 characters in S/4HANA. Do not hard-code length 18 in your own types. Use
matnrormara-matnr. - Stock quantities are derived from the material document table
MATDOC. Do not rely on the stock fields of MARD for new code. Use the released stock views or standard reports. - For new development, especially ABAP Cloud and extensions, use the released CDS view
I_Productor the OData serviceAPI_PRODUCT_SRVinstead of reading MARA directly. Released interfaces stay stable across upgrades. Table layouts are not guaranteed to.
Maintaining materials with BAPI_MATERIAL_SAVEDATA
BAPI_MATERIAL_SAVEDATA creates a material or changes an existing one. It does both. If the material in headdata does not exist, it is created. If it exists, it is changed or extended.
The BAPI uses two parallel structures for each data area:
- A value structure, such as
BAPI_MARC, holds the data. - A flag structure ending in
X, such asBAPI_MARCX, marks which fields to apply.
The BAPI changes only fields whose flag is set. A value without its flag is ignored without an error. This is the most common reason a "successful" call changes nothing.
The headdata structure also names the views you are maintaining, through flags such as basic_view and purchase_view.
DATA: ls_head TYPE bapimathead,
ls_client TYPE bapi_mara,
ls_clientx TYPE bapi_marax,
ls_plant TYPE bapi_marc,
ls_plantx TYPE bapi_marcx,
lt_desc TYPE STANDARD TABLE OF bapi_makt,
ls_return TYPE bapiret2.
ls_head-material = 'ZDEMO_MAT_001'.
ls_head-ind_sector = 'M'.
ls_head-matl_type = 'HAWA'.
ls_head-basic_view = abap_true.
ls_head-purchase_view = abap_true.
ls_client-base_uom = 'EA'.
ls_clientx-base_uom = abap_true.
ls_plant-plant = '1010'.
ls_plant-pur_group = '001'.
ls_plantx-plant = '1010'.
ls_plantx-pur_group = abap_true.
APPEND VALUE #( langu = 'E' matl_desc = 'Demo trading good' ) TO lt_desc.
CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
EXPORTING
headdata = ls_head
clientdata = ls_client
clientdatax = ls_clientx
plantdata = ls_plant
plantdatax = ls_plantx
IMPORTING
return = ls_return
TABLES
materialdescription = lt_desc.
IF ls_return-type CA 'EAX'.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = abap_true.
ENDIF.The program fills the head, then the client-level data with its flags, then the plant-level data with its flags, and then the description. After the call it checks the return message. It commits only when there is no error.
Two details in this example matter:
- The plant key (
plant) must be set in both the value structure and the flag structure. The flag structure carries the key as a value, not as a flag. - The material number is supplied by the caller. That works only if the material type allows external number assignment. For internal numbering, the BAPI assigns the number, so leave the number empty and read it back from the result.
The call sequence
The BAPI itself never commits. The database changes become permanent only when you call BAPI_TRANSACTION_COMMIT. With wait = abap_true, the commit waits until the update is complete. You need this if the next statement reads the new material.
Common errors
- Flag missing. The value is set but the matching
Xfield is not. The field stays unchanged. - Too narrow a return check. The
returnparameter holds one summary message. For detailed diagnosis, also pass thereturnmessagestable and read all of its rows. - Missing view flags. If
headdatadoes not name the view you maintain, for examplemrp_viewfor MRP fields, the BAPI may ignore that data. - Number rules. Using an external number with a material type that only allows internal numbers produces an error message.
- Missing prerequisite data. Extending to a plant fails if the plant, the storage location, or the valuation area is not set up in customizing.
- Locking. If a user has the material open in a standard transaction, the BAPI reports a lock error. Retry or report it. Do not delete the lock.
- Forgotten commit. The call finishes without errors and then nothing is saved, because the program ended without the commit.
- Wrong test environment. Test the program in a sandbox client first. A created material is hard to remove, because deletion is a flag and a later archiving step.
Exercise
A program calls BAPIMATERIALSAVEDATA to change the MRP type in plant 1010. The return type is "S" and the program commits. The MRP type is unchanged in MARC. Give two likely causes.
Show answer
First, the flag field in the plant flag structure (BAPIMARCX-MRPTYPE) was not set to abaptrue, so the BAPI ignored the value. Second, the MRP view was not selected in headdata (mrpview), or the plant key was missing from the flag structure, so the plant data was not applied.
Exercise
Why should a program read material data with a SELECT but change it with a BAPI, instead of using UPDATE on MARC?
Show answer
A SELECT changes nothing, so it is safe. A BAPI runs the standard checks, numbering rules, field control and change documents. A direct UPDATE skips them and can leave inconsistent data.