Dear SAP Learners, in the blog you will learn how to perform unit and currency conversion in ABAP CDS Views
In SAP, amounts and measurements are stored in the databases in a source currency and source unit. They need to be converted to make values in different units or currencies.
In traditional ABAP, these currency and unit conversions is done with standard functions provided by SAP. For example
These standard function modules make use of conversion rules that are maintained in standard transactions and stored in tables.
ABAP CDS Built-in Conversion Functions
Two built-in functions are available to implement conversion in ABAP CDS Views. These are
1. Unit_Conversion(p1 => a1, p2 => a2, …. )
- Returns result of type abap.quan
- Converts a quantity in source unit into a value in target unit
- Rules maintained in transaction CUNI and stores in database table T006 are considered while performing the quantity unit conversions.
Now, lets look at the sample DDL source code.
Database table SPFLI contains a field DISTANCE in which the flight distance is stored for each flight connection and corresponding unit of measure is found in field DISTID.
Different unit of measures exists in the table for different flight connections, lets convert all of them into Miles “MI”.
In the CDS View definition above, the value in table field DISTANCE is converted into miles “MI” and returned as view field DISTANCE.
2. Currency_Conversion(p1 => a1, p2 => a2, ..)
The above conversions function is used to currency conversion in ABAP CDS Views. It
- Returns result of type abap.curr
- Converts an amount in source currency into a value in target currency.
- Based on exchange rate valid on a target date.
- Rules maintained in transaction OB08 and stores in database table TCUR are considered while performing the quantity unit conversions.
In SFLIGHT database table the airfare of all airlines is stored PRICE field and corresponding currency code CURRENCY field.
Lets convert all of them into US Dollar “USD”.
In the CDS View definition above, the value in table field PRICE is converted to US Dollar and returned as view field PRICE.
The above shown built-in conversion functions have extra parameter error_handling can be used in case of any error while performing conversion.
There are 3 possible values for this parameter
- FAIL_ON_ERROR – an error raises an exception, default value.
- SET_TO_NULL – the result is reset to null value.
- KEEP_UNCONVERTED – the source value is not changed.
Lets look a sample DDL source code to handle errors while conversion.
Congratulations!! you have successfully learned how to use unit and currency conversion in ABAP CDS Views.
Please check out our other ABAP CDS View Tutorials
SAPLearners.com is now on Telegram. Click here to join our channel and stay updated with the latest tutorials and updates.