Sub topic 7
Material documents and the longer material number
This chapter assumes you know MM basics: goods movements, material documents, and the MKPF and MSEG tables. It also assumes you can read simple ABAP Open SQL.
What changed in ECC and what changed in S/4HANA
In ECC, a goods movement wrote a header row to MKPF and item rows to MSEG. It also updated aggregate tables such as MARD (stock per storage location), MCHB (batch stock), and the special stock tables. Reports read the aggregates for current stock and MSEG for history.
In S/4HANA, material document items are stored in one table, MATDOC. Header fields are held with each item, so a single read returns the movement history. The old tables MKPF and MSEG still exist as compatibility views, so older code that selects from them keeps working for reads. Stock quantities are no longer kept as stored aggregates. They are calculated from MATDOC when needed, with MATDOC_EXTRACT and pre-aggregated data supporting that on HANA. This follows the same principle as the Universal Journal: store line items, derive totals.
Reading movement history
This query returns movements for one material and plant from the single table.
SELECT mblnr, mjahr, zeile, matnr, werks, menge
FROM matdoc
WHERE matnr = @lv_matnr
AND werks = @lv_werks
INTO TABLE @DATA(lt_movements).The key of MATDOC is different from MSEG, so check the key fields in your system before relying on the order of results. Add ORDER BY if order matters.
Consequences for custom code
- Reads from MKPF and MSEG usually continue to work through the compatibility views.
- Writes to MKPF, MSEG, MARD and similar tables are not supported. Use standard BAPIs or function modules such as BAPIGOODSMVTCREATE.
- Code that reads stock from aggregates should be checked. Some of those tables are now views that calculate stock, which can cost more at runtime for large volumes.
- Custom indexes or appends on MKPF and MSEG need review, because the physical table is now MATDOC.
The ABAP Test Cockpit and the simplification items from SAP flag many of these cases. A later chapter covers those tools.
The longer material number
ECC limits the material number (MATNR) to 18 characters. S/4HANA supports an extended length of up to 40 characters. The extension is optional. In a system conversion the default stays at 18 characters, and you can activate the longer length through a configuration step.
The change reaches code that hard-codes the length:
- Fields declared as
CHAR18or withLENGTH 18can truncate longer numbers. Declare themTYPE matnrinstead. - Interfaces, IDocs, and files with fixed field widths may need changes on both sides.
- Offsets such as
lv_matnr+0(18)cut longer values.
" Fragile: fixed length
DATA lv_old TYPE c LENGTH 18.
" Safe: follows the data element, including a later length change
DATA lv_new TYPE matnr.Check the activation steps and restrictions for your release in SAP's documentation. Activation is hard to reverse once longer numbers exist in the data.
Exercise
A custom report in ECC selects from MSEG and updates a custom field in MARD with a direct UPDATE statement. What happens to each part after conversion, and what should you do?
Show answer
The MSEG read generally keeps working through the compatibility view, though you may want to read MATDOC directly. The direct update of MARD is not supported, because stock is calculated from MATDOC and stored aggregates are gone. Replace it with a standard posting through BAPIGOODSMVTCREATE, or move the custom field to a separate custom table.
Exercise
Why should you declare a variable as TYPE matnr instead of CHAR18?
Show answer
TYPE matnr follows the data element, so the variable keeps working if the material number length is extended to 40 characters. CHAR18 would truncate longer numbers.