Migrating Classic Code to ABAP Cloud

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

Many companies own ABAP programs that were written years ago, for example a report that adds up the approved expense claims and prints the total. Such code often works, but it uses statements and system objects that the newer, stricter way of writing ABAP does not allow. That newer way is called ABAP Cloud, and a team that wants its custom code to survive upgrades (the aim of a "clean core") has to move old code towards it. This page gives a method with four steps that you can repeat for every piece of old code: measure what is wrong, replace what you can, wrap what you cannot replace, and move the business logic into the RAP programming model. A small before and after example shows the first steps on real code. The page is written against Standard ABAP release 8.16 and ABAP for Cloud Development release 9.20 (2608). ABAP Cloud is the only model in the SAP BTP ABAP environment and in SAP S/4HANA Cloud Public Edition, and it can also be used for new and migrated code on premise and in SAP S/4HANA Cloud Private Edition; classic Standard ABAP keeps running on premise.

Before you read

Key terms

  • ABAP Cloud: the development model that combines ABAP for Cloud Development with released APIs only.

  • ABAP Test Cockpit (ATC): the tool that runs groups of checks on development objects and lists the findings.

  • Check variant: a named set of ATC checks.

  • Released API: an object that SAP has marked as stable and allowed for use by your own code.

  • Cloudification Repository: SAP’s published list of released objects and, for objects that are not released, their successors.

  • Wrapper: a small class of your own that hides a call to old code behind a released interface.

  • Clean core: keeping custom code away from SAP internals so that upgrades do not break it.

What has to change

Classic ABAP is written in the language version Standard ABAP. Code in that version may use every statement of the language and may call any visible object, including objects that SAP never meant for outside use. ABAP for Cloud Development allows only a restricted set of statements and only released objects. Three kinds of change follow when old code is moved.

  • Statements that are not allowed. Classic list output with WRITE, dynpro statements and executable programs (reports) are not allowed, and almost no obsolete language element is allowed (the page on classic reports shows what a report is). The page on obsolete language elements lists the old forms and their modern replacements, such as header lines, FORM routines and ADD.

  • Objects that are not released. A call to a function module, a class or a database table of SAP that carries no release contract for cloud development is reported by the syntax check. Most SAP database tables cannot be read directly; released CDS entities stand in for many of them, and the XCO library offers released APIs for tasks such as reading the current date and time.

  • Tools. In ABAP Cloud, ABAP Development Tools (ADT) is the only supported tool, so transactions such as SE80 and SE24 are not available. See ABAP Development Tools (ADT).

The method in four steps

flowchart TD A["1. Measure: run the ATC cloud readiness check"] --> B{"Finding?"} B -->|"Obsolete statement"| C["2a. Rewrite with modern statements"] B -->|"Object not released"| D{"Released successor exists?"} D -->|"Yes"| E["2b. Replace the call with the successor"] D -->|"No"| F["3. Wrap the legacy code behind a released interface"] C --> G["Run the check again"] E --> G F --> G G --> H["4. Move the logic into a RAP business object"]

Step 1: run an ATC readiness check

The ABAP Test Cockpit (ATC) is the tool that runs checks on your own development objects. SAP’s ABAP cheat sheet on ABAP for Cloud Development names the check variant for this purpose: ABAP_CLOUD_READINESS. In ADT you open the context menu of the class or package, choose Run As, then the entry for ABAP Test Cockpit with a choice of check variant, and enter the variant name. The findings appear in the ATC Problems view. Two ATC checks of SAP are named in the repository that publishes the Cloudification data: "Usage of Released APIs (Cloudification Repository)", which belongs to cloud readiness, and "Usage of APIs", which is part of the clean core checks and distinguishes classic from released APIs.

Count the findings per object before you change anything. The count tells you which programs are cheap to move and which need wrappers. An ATC run needs an SAP system; it is not run here. The playground used for the examples on this site has no ATC, but it has a linter, abaplint, that can be set to a cloud syntax profile, and the example below uses it as a rough stand-in for step 1.

Step 2: replace what is not allowed

For each finding there are two possible answers.

  • The finding is a statement. Rewrite it. Inline declarations, SELECT ... INTO TABLE @DATA(...), constructor expressions such as REDUCE and method calls replace header lines, ADD and FORM routines. This is ordinary modernisation and it also works in classic code.

  • The finding is an object that is not released. Look it up in the Cloudification Repository viewer. It lists released SAP objects and, for non-released ones, the successor to use. In ADT the Released Objects node of the project shows what the system offers. If a successor exists, replace the call, adapting the data it expects.

Change the language version of an object only after the readiness findings are under control. In ADT you open the object’s Properties, choose General and edit ABAP Language Version. After switching to ABAP for Cloud Development the code shows syntax errors until it is refactored, and it cannot be activated meanwhile.

Step 3: wrap legacy code that has no successor

Sometimes your own code or an SAP object has no released replacement, for example a custom function module that checks a claim and that many programs still need. The usual answer is a wrapper: a class written in Standard ABAP that makes the call, plus an interface and a factory class that you release for cloud development. Code in ABAP Cloud then calls only the released interface. The SAP tutorial that wraps the function module BAPI_PR_CREATE follows exactly this recipe:

  • a package for the wrapper objects;

  • an interface that mirrors the parameters of the old function module;

  • a wrapper class that implements the interface and does the CALL FUNCTION; this class is not released;

  • a factory class with a private constructor whose method returns a reference to the interface;

  • a release contract C1 on the interface and on the factory class, set in Properties on the API State tab, with Release State "Released" and "Use in Cloud Development" selected.

Until the interface and the factory are released, a class in ABAP for Cloud Development that uses them does not activate. The wrapper keeps the unsafe call in one place, so when a released successor appears later you change one class and not all callers. A wrapper does not make the old code clean; it makes the dependency visible and replaceable.

The next listing sketches such a wrapper for an assumed old function module ZEXP_CLAIM_CHECK of the Expense claims running example. The names are made up for this page. It requires an SAP system; not run here.

INTERFACE zif_exp_claim_check PUBLIC.
  METHODS is_valid
    IMPORTING claim_id      TYPE zexp_claim-claim_id
    RETURNING VALUE(result) TYPE abap_boolean.
ENDINTERFACE.

CLASS zcl_exp_claim_check_legacy DEFINITION PUBLIC FINAL CREATE PRIVATE
  GLOBAL FRIENDS zcl_exp_claim_check_factory.
  PUBLIC SECTION.
    INTERFACES zif_exp_claim_check.
ENDCLASS.

CLASS zcl_exp_claim_check_legacy IMPLEMENTATION.
  METHOD zif_exp_claim_check~is_valid.
    " Written in Standard ABAP: this is the only place that knows the old function module.
    CALL FUNCTION 'ZEXP_CLAIM_CHECK'
      EXPORTING
        claim_id = claim_id
      IMPORTING
        valid    = result.
  ENDMETHOD.
ENDCLASS.

CLASS zcl_exp_claim_check_factory DEFINITION PUBLIC FINAL CREATE PRIVATE.
  PUBLIC SECTION.
    CLASS-METHODS create
      RETURNING VALUE(result) TYPE REF TO zif_exp_claim_check.
ENDCLASS.

CLASS zcl_exp_claim_check_factory IMPLEMENTATION.
  METHOD create.
    result = NEW zcl_exp_claim_check_legacy( ).
  ENDMETHOD.
ENDCLASS.

The interface and the factory are the objects you release with contract C1 and "Use in Cloud Development". The wrapper class stays unreleased. A real wrapper to a remote or non-released API of SAP has further details (destinations, parameter structures) that the tutorial shows.

Step 4: move the logic into RAP

After the readiness findings are fixed, look at what the program does. A report that reads claims, changes them and prints a list is a user interface, a business rule and a data access mixed in one place, and ABAP Cloud has no reports or dynpros. The RAP programming model gives each part a home: a CDS view for the data, a behavior definition for the rules and changes, and a service for the user interface. Move the business rules into the behavior, keep the data model in CDS and let a Fiori app replace the list. The pages RAP Overview and Managed and Unmanaged RAP show the target. The Expense claims example would end as the interface view ZI_ExpClaim with its behavior definition and service. This step is larger than the others and is often planned per business object, not per program.

Example: a report before and after

The running example is a report that adds up the amounts of the claims with a given status. The lab table ZEXP_CLAIM in the local playground is a reduced version of the one in the running example: it has only claim_id, status and amount (an integer). The playground is not SAP software and has no ATC, so everything below is explained for classic and cloud languages alike, and only the parts the playground can run were run.

Before: classic report

The report below uses a header line, FORM routines with PERFORM, ADD, a WRITE list and START-OF-SELECTION. It runs in the playground. Running it prints the line below the listing.

REPORT zexp_old.

DATA gt_claims TYPE TABLE OF zexp_claim WITH HEADER LINE.
DATA gs_claim  TYPE zexp_claim.
DATA gv_total  TYPE i.

START-OF-SELECTION.
  PERFORM load_demo_data.
  PERFORM add_up_approved USING 'A' CHANGING gv_total.
  WRITE / |Approved total: { gv_total }|.

FORM load_demo_data.
  gs_claim-claim_id = 'C1'.
  gs_claim-status   = 'A'.
  gs_claim-amount   = 120.
  INSERT zexp_claim FROM gs_claim.
  gs_claim-claim_id = 'C2'.
  gs_claim-status   = 'R'.
  gs_claim-amount   = 50.
  INSERT zexp_claim FROM gs_claim.
  gs_claim-claim_id = 'C3'.
  gs_claim-status   = 'A'.
  gs_claim-amount   = 30.
  INSERT zexp_claim FROM gs_claim.
ENDFORM.

FORM add_up_approved USING iv_status TYPE zexp_claim-status
                     CHANGING cv_total TYPE i.
  SELECT * FROM zexp_claim INTO TABLE gt_claims WHERE status = iv_status.
  LOOP AT gt_claims.
    ADD gt_claims-amount TO cv_total.
  ENDLOOP.
ENDFORM.
Approved total: 150

To imitate step 1 in the playground, abaplint was run with its syntax version set to Cloud and the rules for obsolete statements and strict Open SQL switched on. This is a community linter and not the ATC, so it shows the kind of findings, not the findings the ATC would give. Real output for the report above:

src/zexp_old.prog.abap[1, 1]  - Statement does not exist in the configured ABAP version(or a parser error), "REPORT" (parser_error) [E]
src/zexp_old.prog.abap[3, 41] - WITH HEADER LINE is obsolete (obsolete_statement) [E]
src/zexp_old.prog.abap[7, 1]  - Statement does not exist in the configured ABAP version(or a parser error), "START" (parser_error) [E]
src/zexp_old.prog.abap[8, 3]  - Indentation problem, expected 0 spaces (indentation) [E]
src/zexp_old.prog.abap[9, 3]  - Indentation problem, expected 0 spaces (indentation) [E]
src/zexp_old.prog.abap[10, 3] - Indentation problem, expected 0 spaces (indentation) [E]
src/zexp_old.prog.abap[10, 3] - Statement does not exist in the configured ABAP version(or a parser error), "WRITE" (parser_error) [E]
src/zexp_old.prog.abap[29, 3] - SELECT: "iv_status" must be escaped with @ in strict mode (check_syntax) [E]
src/zexp_old.prog.abap[31, 5] - Statement "ADD" is obsolete (obsolete_statement) [E]
abaplint: 9 issue(s) found, 6 file(s) analyzed

The report itself, the START-OF-SELECTION event and the WRITE list are rejected as statements that do not exist in the cloud language. The header line and ADD are obsolete. The SELECT uses a host variable without the @ escape that strict Open SQL requires. The three indentation lines follow from the first parser errors and disappear when those are fixed. A real ATC run would also report any call to a non-released object; this report has none, because it reads only a table of its own.

After: a class with a method

Step 2 moves the logic out of the report into a method. The class has no output statement, no header line and no FORM. It declares the result table inline in the SELECT, escapes the host variable with @, and adds the amounts with REDUCE. An ABAP Unit test replaces the printed line as the proof that it works: it fills the table, calls the method and compares the total. The class and test run in the playground and the test passes.

CLASS zcl_exp_claim_total DEFINITION PUBLIC FINAL CREATE PUBLIC.
  PUBLIC SECTION.
    METHODS add_up
      IMPORTING status        TYPE zexp_claim-status
      RETURNING VALUE(result) TYPE i.
ENDCLASS.

CLASS zcl_exp_claim_total IMPLEMENTATION.
  METHOD add_up.
    SELECT amount FROM zexp_claim
      WHERE status = @status
      INTO TABLE @DATA(amounts).
    result = REDUCE i( INIT sum = 0
                       FOR amount IN amounts
                       NEXT sum = sum + amount-amount ).
  ENDMETHOD.
ENDCLASS.
CLASS ltcl_total DEFINITION FINAL FOR TESTING DURATION SHORT RISK LEVEL HARMLESS.
  PRIVATE SECTION.
    METHODS adds_approved_claims FOR TESTING.
ENDCLASS.

CLASS ltcl_total IMPLEMENTATION.
  METHOD adds_approved_claims.
    DELETE FROM zexp_claim.
    DATA claims TYPE STANDARD TABLE OF zexp_claim WITH EMPTY KEY.
    claims = VALUE #( ( claim_id = 'C1' status = 'A' amount = 120 )
                      ( claim_id = 'C2' status = 'R' amount = 50 )
                      ( claim_id = 'C3' status = 'A' amount = 30 ) ).
    INSERT zexp_claim FROM TABLE @claims.
    DATA(cut) = NEW zcl_exp_claim_total( ).
    cl_abap_unit_assert=>assert_equals( act = cut->add_up( 'A' ) exp = 150 ).
  ENDMETHOD.
ENDCLASS.

The test passed in the playground, with the expected value 150 that the old report printed. When the expected value was changed to 999 the same test failed with an assertion error, which shows that it does check the result. Running abaplint with the same cloud settings on this class reported no finding for it. In a real system you would also switch the class to the language version ABAP for Cloud Development and activate it in ADT; that step requires an SAP system and is not run here. The same logic in a real system would read the claims through the interface view ZI_ExpClaim and not through the table, and the trigger for the calculation would be a RAP action or a determination, not a report, as step 4 describes.

Order of work for a whole code base

Do not move everything at once.

  1. Run the readiness check over the whole custom package set and keep the result as a table of objects and findings.

  2. Start with objects that have few findings and no users, to learn the rules cheaply.

  3. Replace statements and obsolete elements first, because these changes are mechanical and also help classic code.

  4. Look up every non-released object in the Cloudification Repository; replace where a successor exists and list the rest for wrappers.

  5. Write wrappers for the remaining objects and release the interface and the factory.

  6. Set the language version of the finished classes to ABAP for Cloud Development, so the syntax check keeps them clean.

  7. Move user interfaces and business rules into RAP business objects, one business object at a time.

Older material you may meet

Books and blog posts written before ABAP Cloud describe how to build reports, dynpros, FORM routines, header lines and function modules, and they call SAP function modules and tables directly. For example, Complete ABAP by Kiran Bandari (SAP PRESS, 2016) is written for SAP NetWeaver AS ABAP 7.4 and 7.5 and teaches that classic style; the concepts it teaches (tables, classes, exceptions) still apply, but it does not know language versions, release contracts or the readiness check, and a program written after it will meet the findings described above when it moves to ABAP Cloud. Where such a book and the current documentation differ, follow the documentation.

Summary

  • Classic code is written in Standard ABAP; ABAP Cloud allows only a restricted set of statements and released objects, so old code must be changed before it can move.

  • Step 1 measures: the ATC check variant ABAP_CLOUD_READINESS lists the findings in ADT. It needs an SAP system.

  • Step 2 replaces: modern statements for obsolete ones, and a successor from the Cloudification Repository for each object that is not released.

  • Step 3 wraps what has no successor: a Standard ABAP wrapper class behind an interface and a factory that are released with contract C1 and "Use in Cloud Development".

  • Step 4 moves the logic into RAP, because ABAP Cloud has no reports or dynpros.

  • The before and after example shows a report with header line, FORM and ADD becoming a class with a unit test; the class and test were run in the playground, ATC and language-version steps were not.