In many business scenarios, users enter IDs without leading zeros, while the system internally stores them with leading zeros. At the same time, the values should still be displayed to the user without those zeros.
Traditionally in ABAP, this behavior is handled using conversion exits defined in the domain (for example, ALPHA used for fields like Sales Document numbers).
The typical approach looks like this:
- A domain contains the conversion exit (e.g.,
ALPHA). - The domain is assigned to a data element.
- The data element is assigned to the table field.
This automatically manages the conversion between internal format (with zeros) and external format (without zeros).
The Challenge
Sometimes we work with:
- Existing database tables
- Predefined types such as
ABAP.CHAR(10) - Fields without domains or conversion exits
In these cases, developers often implement workarounds such as:
- Adding leading zeros using RAP determinations
- Removing zeros in CDS views
- Performing manual conversions in ABAP logic
This increases complexity and introduces unnecessary processing logic.
A Simpler Approach in CDS
Now, CDS provides a much simpler solution.
You can apply a conversion exit directly at the element level using the annotation:
@AbapCatalog.typeSpec.conversionExit: 'ALPHA'
This allows the CDS field to behave as if a domain conversion exit were defined, without modifying the underlying table structure.
Example Scenario
Consider a table storing a 10-digit mobile number defined using a predefined type:
MobileNumber : abap.char(10);
Users may enter the number as:
987654321
But internally the system should store it as:
0987654321
Using the CDS annotation, the conversion can be handled automatically.
CDS Example
With this annotation:
- The system adds leading zeros during internal processing
- The value is displayed without leading zeros in the UI
This works seamlessly in:
- SAP Fiori applications
- RAP services
- OData APIs
Benefits of Element-Level Conversion Exits
Using this CDS annotation provides several advantages:
- No need to modify existing domains or data elements
- Eliminates manual zero-padding logic
- Reduces RAP determinations or helper code
- Keeps conversion logic clean and centralized in CDS
- Works smoothly with RAP and Fiori UI
Supported Environments
The annotation is supported in:
- SAP BTP ABAP Environment (Steampunk)
- SAP S/4HANA Public Cloud
- SAP S/4HANA Private Cloud from release 2025 onward
This approach reduces custom logic and provides a clean, maintainable way to handle format conversions in modern RAP and ABAP Cloud applications.
#ABAP #CDS #ABAPCloud #S4HANACloud #RAP #SAPDevelopers #SAPBTP
