Sub topic 14
Custom code: finding and fixing what breaks
Custom ABAP code is often the largest source of effort in a conversion project. SAP's standard objects are handled by the conversion tools. Your own reports, enhancements, interfaces, and Z tables are not. This chapter explains how to find the affected objects, sort them, and fix them.
Prerequisites
This chapter assumes you can read and write basic ABAP (Open SQL, internal tables, data elements). It also assumes you know the S/4HANA data model changes covered earlier: the Universal Journal (ACDOCA), Business Partner, and MATDOC. You should be able to open the ABAP Test Cockpit (ATC) and the Simplification Item Catalog.
Why custom code breaks
Custom code breaks for four main reasons.
- Removed or replaced objects. Tables, function modules, transactions, and BAPIs that no longer exist or are no longer supported.
- Changed data model. Aggregate and index tables are replaced by compatibility views. You can still read through those views, but you cannot write to them.
- Changed field semantics. The material number grows from 18 to 40 characters. Customer and vendor become roles of a Business Partner.
- Changed database behavior. SAP HANA does not return rows in primary key order unless you ask for it with
ORDER BY.
The first two causes usually show up as syntax errors or ATC findings. The last two often compile cleanly and produce wrong results at runtime. That makes them the dangerous ones.
The overall workflow
1. Collect usage data
Most systems contain a lot of code nobody runs. Two tools record what executes:
- ABAP Call Monitor (SCMON) records which ABAP entry points are called.
- Usage Procedure Logging (UPL) records the same kind of data at the level of called units, and has low overhead.
The transaction SUSG aggregates this data from one or more systems. Collect it for a full business cycle, ideally including a year-end close. Otherwise you will mark quarterly or yearly programs as unused by mistake.
2. Scope the code
Use the usage data to sort objects into three groups. Objects used in production go into scope. Objects never used in the collection period are candidates for deletion, which you should confirm with the process owner. Objects that run only rarely, such as legal reports, need an explicit decision. Removing dead code before the conversion reduces the later work directly.
3. Run ATC with the S/4HANA readiness checks
ATC compares your code against the Simplification Database, which is the machine-readable form of the Simplification Item Catalog. You normally run this from a central check system. That system is a separate, recent ABAP system that uses remote checks against your ECC system. This way you can use the newest check content without upgrading ECC. Check the SAP documentation for the minimum release of the central system.
The check variant is named by target release, for example S4HANA_READINESS_2023. The names and recommended settings change between releases. See SAP Note 2436688 and the notes for your target release. Do not rely on a variant name copied from an older project.
The SAP-delivered Custom Code Migration app (Fiori) works on top of this. It lists findings, ties them to usage data, and lets you assign them to developers.
Classifying findings
Not every finding needs the same response. A table helps you plan effort.
| Category | Typical example | Typical action |
|---|---|---|
| Syntax error after conversion | Read from a removed table or function module | Replace with the successor object listed in the simplification item |
| Write to a compatibility view | `INSERT` or `UPDATE` on MSEG, BSEG-related index tables, or old aggregates | Use the standard API or posting function module instead |
| Semantic change | Field length, field meaning, or key change | Review logic and retype variables |
| Performance | Code that assumed pre-aggregated totals or nested `SELECT` loops | Rewrite to use CDS views or one set-based `SELECT` |
| Database behavior | `SELECT` without `ORDER BY` followed by `READ ... BINARY SEARCH` | Add `ORDER BY` or sort explicitly |
Example: field length
The material number is a good case because it compiles everywhere. In the old code, the variable is typed as 18 characters.
REPORT zdemo_matnr_length.
DATA lv_old TYPE c LENGTH 18.
DATA lv_new TYPE matnr.
lv_new = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.
lv_old = lv_new.
WRITE: / 'Typed as c18 :', lv_old,
/ 'Typed as MATNR:', lv_new.On S/4HANA, lv_old holds only the first 18 characters. No error is raised. A long material number then gets cut off, and the program may read or post against the wrong material. ATC has checks for this pattern, but only when the type is a literal length. Variables inside generic structures or interface parameters need a manual review.
The fix is to type everything by data element (TYPE matnr) and not by length. Do the same for structures, interfaces, file layouts, and any CONCATENATE or offset logic such as lv_matnr+0(18). Be careful with external interfaces. IDocs, flat files, and RFC consumers outside SAP may still expect 18 characters, and they need their own change or a limit on the numbers you create.
Example: reading movement history
Old reports that read MKPF and MSEG usually still run, because both names exist as compatibility views over MATDOC. These reports often become slow or inconsistent when they join the views. The cleaner pattern is a direct read of MATDOC.
REPORT zdemo_matdoc_read.
PARAMETERS: p_matnr TYPE matnr,
p_werks TYPE werks_d.
SELECT mblnr, mjahr, zeile, matnr, werks, menge
FROM matdoc
WHERE matnr = @p_matnr
AND werks = @p_werks
ORDER BY mjahr, mblnr, zeile
INTO TABLE @DATA(lt_movements).
IF sy-subrc <> 0.
WRITE / 'No movements found.'.
ELSE.
LOOP AT lt_movements INTO DATA(ls_move).
WRITE: / ls_move-mblnr, ls_move-mjahr, ls_move-zeile, ls_move-menge.
ENDLOOP.
ENDIF.The ORDER BY is deliberate. The SELECT has to state its order explicitly, because HANA does not guarantee any order without it.
A write to MSEG does not work this way. Code that inserts into or updates MKPF/MSEG, or into the old stock tables, must be replaced by a goods movement BAPI such as BAPI_GOODSMVT_CREATE. The same rule applies to finance. Reading old totals tables like GLT0 through compatibility views is possible, but direct writes are not.
Example: Business Partner
Custom code that selects from KNA1 or LFA1 for read-only purposes often keeps working, because the tables remain as part of the Customer/Vendor Integration (CVI). Code that creates or changes customers or vendors directly in the database tables, or through old BAPIs and batch input recordings of XD01 or XK01, needs rework. Use Business Partner APIs and transactions instead. Also check any code that assumes the customer number and vendor number are independent. After CVI, the BP number and the customer or vendor number are usually the same, but this depends on your number range configuration.
Modifications and enhancements
Custom code is not only Z objects. Check three further kinds of change.
- Modifications to SAP objects, listed in the modification browser (SE95). They are adjusted in SPDD and SPAU during the conversion, and their volume affects your effort estimate.
- Implicit and explicit enhancements. These can fail to activate if the enhanced object was changed or removed.
- Appends and custom fields on tables that were merged, such as those in finance. Check whether the custom fields are carried into ACDOCA through the field extension tools.
A callout on a common planning mistake:
Fixing before or after conversion
You can fix in two places.
- Before conversion, on ECC. Fixes that are valid on both releases, such as adding
ORDER BYor typing by data element, can be made in ECC and transported normally. Changes to code that is unused or being deleted also belong here. - After conversion, on S/4HANA. Fixes that use S/4HANA-only objects, such as MATDOC or Business Partner APIs, have to be made on the converted system. They are usually done in a dedicated development system that has been converted, then transported forward.
The right split depends on your approach. In a system conversion you normally fix the readiness findings that are release-independent in ECC, and the rest in the converted development system. In a new implementation you often rewrite or re-evaluate each object, and the question becomes whether the requirement should be met with standard functions or extensions instead.
Common mistakes
- Collecting usage data for only a few weeks.
- Treating ATC findings as the whole task list and skipping the review of silent semantic changes.
- Not clearing findings on code that will be deleted, which inflates the count and hides the real ones.
- Using an outdated check variant.
- Forgetting external consumers of interfaces after changing field lengths.
Exercise
A custom report declares DATA lv_matnr(18) TYPE c., then fills it from a MATNR field. ATC reports no syntax error after conversion. Why is the report still a risk, and what is the fix?
Show answer
On S/4HANA the MATNR data element is longer than 18 characters, so the assignment silently truncates longer numbers without raising an error. The report could then read or post against the wrong material. Replace the length-based declaration with DATA lv_matnr TYPE matnr., and check offsets such as +0(18) and any external interfaces or files that assume 18 characters.
Exercise
Your usage data covers three weeks in July. A developer wants to delete every program that shows no usage. Give one reason this is unsafe and one step that makes it safer.
Show answer
Programs that run only at quarter-end, year-end, or for legal reporting would not appear in a three-week window, so deleting them would remove code that is still needed. Collect SCMON or UPL data over a full business cycle including year-end, and have process owners confirm any rarely used object before deletion.