Sub topic 7
Material documents and the longer material number
This chapter covers two S/4HANA changes in materials management (MM). The first is that material documents are stored in one table, MATDOC. The second is that the material number can be up to 40 characters long. Both change how custom code reads and writes data.
It assumes you know ECC inventory management basics (goods receipts, goods issues, material documents), have read the earlier chapters on the HANA database and the simplified data model, and can read simple ABAP Open SQL.
How ECC stored inventory data
In ECC, one goods movement wrote to several places:
- MKPF held the material document header: document number, posting date, user.
- MSEG held the document items: material, plant, quantity, movement type.
- Aggregate and history tables held stock totals, updated on every posting. Examples are MARD (storage location stock), MARC (plant data), MCHB (batch stock), MKOL (special stock from vendor) and history tables such as MARDH and MARCH for period-end stock.
The aggregates existed because summing millions of MSEG rows on a disk-based database was too slow. The cost was redundancy and lock contention. Many postings for the same material and plant updated the same MARD row, so they queued behind each other.
What S/4HANA does instead
S/4HANA stores each material document item once, in MATDOC. A MATDOC row carries both the header fields (such as posting date) and the item fields (such as material, plant and quantity). Stock quantities are no longer kept as stored totals that each posting must update. They are derived from the MATDOC line items. SAP also keeps some pre-computed data internally for speed, but that is an implementation detail and not an interface you should use.
This is the same idea as the Universal Journal in finance, where totals are computed from line items instead of read from aggregate tables.
What this means in practice
| Aspect | ECC | S/4HANA |
|---|---|---|
| Document storage | MKPF header plus MSEG items | MATDOC, one row per item with header fields included |
| Stock quantity | Stored and updated in MARD, MCHB, MKOL and others | Derived from MATDOC line items |
| Period-end history | Separate history tables such as MARDH | No stored copies. Derived from line items |
| Posting contention | Concurrent postings update the same aggregate row | Postings mainly insert new rows |
| Reading old table names | Direct table access | Read access works through compatibility views |
Compatibility views and their limits
SAP did not want every custom report that reads MKPF or MSEG to break. In S/4HANA, those names point to compatibility views that rebuild the old structure from MATDOC. A SELECT on MSEG usually keeps working.
The limits matter:
- Reads work, writes do not. Views cannot be updated. Custom code that does
INSERT,UPDATEorMODIFYon MSEG, MKPF or the stock tables fails or has no effect. Post movements through BAPIs such asBAPI_GOODSMVT_CREATEor the standard transactions instead. - Performance can differ. A view that reconstructs a whole MSEG row is more work than reading only the columns you need from MATDOC. For new code, read MATDOC directly and select only the fields you need.
- Custom fields need attention. Append structures or custom fields on MSEG, MKPF or the stock tables must be checked in the conversion. Use SAP's documentation for your release to see how custom fields are carried over.
- Database-level reads can bypass the logic. Native SQL, or database views built directly on the old tables, may not see what you expect. Check these during custom code analysis.
Reading movements from MATDOC
The following report reads movement history for one material and plant from MATDOC. In ECC, the same task needed a join of MKPF and MSEG.
REPORT zmatdoc_demo.
PARAMETERS: p_matnr TYPE matnr,
p_werks TYPE werks_d.
START-OF-SELECTION.
SELECT mblnr, mjahr, zeile, matnr, werks, menge
FROM matdoc
WHERE matnr = @p_matnr
AND werks = @p_werks
INTO TABLE @DATA(lt_movements).
IF sy-subrc <> 0.
WRITE / 'No movements found.'.
ELSE.
cl_demo_output=>display( lt_movements ).
ENDIF.Add a restriction on the posting date or the document year when you can. MATDOC can be very large, and HANA is fast, but it does not make an unrestricted read free.
For stock figures, avoid summing MATDOC yourself unless you know the movement types and special stock indicators involved. SAP provides released CDS views and standard APIs for current stock. Reimplementing the stock logic by hand is a common source of wrong numbers.
The extended material number
The second change is about the material number field, MATNR.
- In ECC, MATNR is 18 characters long.
- From S/4HANA 1709 onward, the data element supports up to 40 characters.
The longer length is not on by default. An administrator activates the extended material number function in customizing, and chooses the length to allow, up to 40. Until activation, the system behaves as before and enforces 18 characters.
Two points about activation:
- It is a one-way decision. Once the function is active, you cannot switch it off. Decide with the business and the interface owners first, and test it in a sandbox system.
- The conversion does not need it. A system conversion can finish with 18 characters. You can activate the longer length later. Many projects do this because external systems, such as ones that supply 40-character part numbers, are the reason to switch.
Why a longer number breaks code
A longer MATNR does not change the data in existing records. It changes the assumption that a material number fits in 18 characters. That assumption appears in code in several forms:
- Variables declared as
CHAR18orc LENGTH 18instead ofTYPE matnr. - Offset and length access such as
lv_matnr+0(18). - Structures, file layouts and IDoc segments with an 18-character field.
- Flat file interfaces and RFC calls where the partner system expects 18 characters.
- Conversion exits. MATNR uses the exit
MATN1, and code that bypasses the exit can show internal values rather than external ones.
REPORT zmatnr_length_demo.
DATA lv_bad(18) TYPE c.
DATA lv_good TYPE matnr.
START-OF-SELECTION.
lv_good = 'LONG-PART-NUMBER-0000000000000000000001'.
lv_bad = lv_good.
WRITE: / 'Typed as MATNR:', lv_good,
/ 'Typed as CHAR18:', lv_bad.With the longer length active, lv_bad silently cuts the value to 18 characters, and two different materials can end up with the same truncated key. The compiler does not warn you. ATC checks for S/4HANA readiness flag many of these patterns, but not every one. The fix is to type variables with TYPE matnr (or a data element that refers to it) so they follow the system setting.
Planning checklist
- Run the readiness tools from the earlier chapters and note the findings for MATDOC, MKPF, MSEG and MATNR.
- List custom code and interfaces that write to MM tables. Replace each with a BAPI or a standard API.
- List custom code that reads MKPF and MSEG. Keep it as is if it works, and rewrite the slow or high-volume reads to use MATDOC.
- Decide whether you need 40-character material numbers. If you do, list every interface that carries MATNR, and agree the new length with each partner.
- Test in a sandbox that mirrors production data volumes, including stock reconciliation against ECC for a sample of materials.
Exercise
A custom report in your ECC system updates a stock quantity field in MARD with a direct UPDATE statement. Why is this a problem in S/4HANA, and what should replace it?
Show answer
In S/4HANA, stock quantities are derived from MATDOC line items and no longer stored as totals that code can update. A direct UPDATE therefore fails or has no effect on the real stock, and it would bypass the posting logic anyway. Replace it with a proper goods movement, for example a posting through BAPI_GOODSMVT_CREATE, which creates the MATDOC line items that the stock figures come from.
Exercise
Your system conversion to S/4HANA finished, and the extended material number function is not active. A colleague says a custom variable declared as CHAR18 is now safe to ignore forever. What is wrong with that view?
Show answer
The variable only works while the length stays at 18. The extended length can be activated later, and the activation cannot be undone. After that, a CHAR18 variable truncates longer material numbers without any error, and different materials can collide on the same cut-off value. Declare the variable with TYPE matnr so it follows the system setting.