Code examples · Modern ABAP · Intermediate
Clean core: read products with the released CDS views I_Product and I_ProductDescription
The problem
A custom app must show finished products with their description.
How we solve it
- 1The rulereleased only
- 2Readreleased CDS views
- 3Showprint
The result
The program prints five finished products with description, using only released objects.
SAP objects in play
1. The requirement
A custom app must show finished products with their description. The system is on the clean core path: custom code must not read SAP tables directly, because SAP may change them in an upgrade. The read must pass the ABAP Cloud syntax check.
2. How we design it
In ABAP Cloud you may only use objects that SAP has released as stable interfaces. Instead of the tables MARA and MAKT, SAP offers the released CDS views I_Product and I_ProductDescription. They return the same business data under stable names. The class is created with the language version ABAP for Cloud Development, so the compiler itself refuses anything that is not released. The language comes from the released class CL_ABAP_CONTEXT_INFO, not from SY-LANGU.
Why we decided it this way
- 1Released views instead of tablesSAP promises that a released view stays compatible across upgrades. A table can change its structure.
- 2Language version ABAP for Cloud DevelopmentThe compiler enforces the clean core rules. A forbidden object is a syntax error the day you write it, not a surprise in the upgrade.
- 3Context information from a released classThe user's language is read through a stable API instead of a global system variable.
Before you start
- A package whose ABAP language version is ABAP for Cloud Development (in ADT: package properties).
- An S/4HANA system that offers the released views I_Product and I_ProductDescription.
3. The code, step by step
Step 1 of 3: Set up the class in ABAP Cloud
A class that the Cloud compiler checks for you.
" Class frame and the user's language
CLASS zcl_clean_core_products DEFINITION PUBLIC FINAL CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
ENDCLASS.
CLASS zcl_clean_core_products IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
DATA(language) = cl_abap_context_info=>get_user_language_abap_format( ).
Part by part (plain text)
- Class frame and the user's language. The class looks like any runnable class. If the package has the language version ABAP for Cloud Development, every statement is checked against the list of released objects. CL_ABAP_CONTEXT_INFO is a released class: GET_USER_LANGUAGE_ABAP_FORMAT returns the language of the user in the format that the Description view expects.
Open in the portal
The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.
If it does not work
4. The final outcome
The program prints five finished products with description, using only released objects.
What you should see
Example output (your system has other data): FG-100 Bicycle frame (ST) FG-101 Bicycle wheel (ST) FG-200 Helmet size M (ST)
Try it and check the result
- Activate and run with F9. The console shows up to five finished products.
- Replace I_Product by the table mara in the SELECT and activate: the syntax check reports that the object is not released for ABAP Cloud. This is the clean core rule at work.
- In ADT select I_Product in the Project Explorer and open its properties: the API state shows that it is released and for which use.
Common mistakes
- Not every table has a released successor. When there is none, look for a released API, or ask for one through SAP's release request (the 'Influence' route) instead of reading the table.
- A released view can lag behind a table for new fields. Check the field list of the view, not only the table.
- Do not switch the language version back to 'Standard ABAP' to make a syntax error go away. That is exactly the shortcut that clean core is meant to prevent.