This is a test message to test the length of the message box.
ABAP CDS Conversion routine
Created by Software-Heroes

CDS - Conversion routine


This article is about easy currency and quantity conversion directly in Core Data Services.

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.



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,
        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.



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,
        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.



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.


SAP Documentation - Conversion functions

Included topics:
CDSCore Data ServiceConversion routine
Comments (0)

And further ...

Are you satisfied with the content of the article? We post new content in the ABAP area every Friday and irregularly in all other areas. Take a look at our tools and apps, we provide them free of charge.

ABAP Tools - Work with Eclipse (CDS Analysis)

Category - ABAP

In the complex world of CDS views, it is important to have the right tool at hand to ensure an analysis of the structures and data sources.


CDS - View Entity

Category - ABAP

In this article you will learn more about the new Core Data Service entities, what they bring and what makes them different from the old views.


CDS - Learnings

Category - ABAP

In this article we summarize the learned content and show you the way for what you will need the CDS Views in the future.


CDS - Virtual fields

Category - ABAP

This article is about virtual fields in Core Data Services and how you can subsequently deliver such complex data.


CDS - Authority check

Category - ABAP

How and where are the permissions for a Core Data Service delimited? Check out this article for the details.