Sub topic 18
Reading and maintaining materials from code
This chapter assumes you can read basic ABAP and know the material master tables from the previous chapter: MARA (general data), MAKT (descriptions), MARC (plant data), MARD (storage location data) and MBEW (valuation). The rule is simple. Read with ABAP SQL. Write with a BAPI.
Reading materials with ABAP SQL
Each organizational level has its own table, so a read usually joins them. The query below lists materials extended to plant 1010, with description, material type and MRP type.
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.- The join to MAKT filters on the logon language. Without that condition you get one row per language.
- The join to MARC returns one row per plant. The
WHEREclause on the plant keeps the result to one row per material. - Never update these tables directly with
UPDATEorINSERT. You would bypass validation, change documents and dependent data.
Maintaining materials with a BAPI
A BAPI (Business Application Programming Interface) is a released function module that runs the same checks as the transaction. BAPI_MATERIAL_SAVEDATA creates a material or changes an existing one.
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.How the call works
- The header sets the material type, industry sector and which views to create. Views you do not switch on are not maintained.
- Every data structure has a twin ending in
X. The BAPI changes only fields whose flag is set. A value without its flag is ignored without an error. - The plant number appears in both the value and flag structures.
Common errors
- Forgetting the flag structure. The material is saved but the field stays empty.
- Skipping the commit. The BAPI does not commit, so the material disappears when the program ends.
- Checking only for type
E. TypesA(abort) andX(exit) also mean failure. - Setting a plant field without the matching view flag or with a plant that does not exist.
Exercise
A program calls BAPIMATERIALSAVEDATA with base_uom = 'EA' and no flag in clientdatax. It then commits. What happens to the unit of measure, and why?
Show answer
The BAPI ignores the field because its flag in the X structure is not set. The material is created without the base unit (or the call returns an error if the unit is mandatory). The value must be paired with ls_clientx-base_uom = abap_true.