CDS - Conversion routine
This article is about easy currency and quantity conversion directly in Core Data Services.
Table of contents
Article update: Since release 7.57 (S/4 HANA 2022), DEFINE VIEW is marked as obsolete, you should use DEFINE VIEW ENTITY instead. These may differ from the examples in some places. You can find more information about the new views in this article.
The conversion of currencies has always been a topic in its own right and very often you will not be able to avoid such questions. So far, currency conversion has mainly been implemented using function modules, but there is now a simpler solution.
Currency
For converting the currency, we have already shown you a function for the select in the article for the data model, this function is also available in a similar way for the CDS view. We build on the view "ZBS_C_DmoPartnerSum", which calculates the sum per partner. Since we cannot use the field directly in the view, we create another CDS view and use this as a basis.
@AbapCatalog.sqlViewName: 'ZBSCDMOPARREPO'
@EndUserText.label: 'Reporting for Partners'
define view ZBS_C_DmoPartnerReporting
with parameters
@Environment.systemField: #SYSTEM_DATE
P_CalculationDate : abap.dats
as select from ZBS_C_DmoPartnerSum
{
key PartnerNumber,
PositionCurrency,
PriceForPartnerMaterial,
currency_conversion(
amount => PriceForPartnerMaterial,
source_currency => PositionCurrency,
round => 'X',
target_currency => cast( 'USD' as abap.cuky( 5 ) ),
exchange_rate_date => $parameters.P_CalculationDate,
exchange_rate_type => 'M',
error_handling => 'SET_TO_NULL'
) as PriceInUSD
}
We take the sum and convert it to our local currency, USD in this case, in a new column. We use the "currency_conversion" function and provide it with the necessary information. The mandatory fields (amount, source_currency, target_currency, exchange_rate_date) can be found in the documentation. We define the target currency and transfer the conversion date as a parameter. If no value is passed, the current day is used. The result of the view now looks like this:
Hint: We have also implemented error handling in the function, if there is a conversion error or a currency conversion is not possible, then the result is set to zero, as happened above.
Quantity
The conversion of quantities works in a similar way to that for currencies. To do this, we define a CDS view above the material table and implement the "unit_conversion" function, which has three mandatory fields (quantity, source_unit, target_unit).
@AbapCatalog.sqlViewName: 'ZBSCDMOMATCONV'
@EndUserText.label: 'Conversion for units'
define view ZBS_C_DmoMaterialConversion
as select from ZBS_I_DmoMaterial
{
key MaterialNumber,
MaterialName,
Stock,
StockUnit,
unit_conversion(
quantity => Stock,
source_unit => StockUnit,
target_unit => cast( 'ST' as abap.unit( 3 ) ),
error_handling => 'SET_TO_NULL'
) as UnitInPieces
}
Here, too, you should use appropriate error handling, since not every conversion will work. In the example above, we reset the erroneous values to zero.
Conclusion
In this small article, we wanted to show you that you don't have to create a new program to convert the currency, but can process everything in a Core Data Service so that it can be reused.