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

CDS - Conversion routine

5594

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

Advertising


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.

 

Source:
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 Tuesday and Friday and irregularly in all other areas. Take a look at our tools and apps, we provide them free of charge.


CDS - Typed Literals

Category - ABAP

How can you work with even greater type precision in a Core Data Service when creating an element in the view? To find out, we'll look at typed literals and how they can help you in everyday use.

01/30/2026

RAP - CDS Pattern

Category - ABAP

How does the CDS pattern actually work, and what does CDS-only have to do with it? In this article, we'll look at the architecture and use of the pattern.

11/28/2025

CDS - Types of Data Definitions

Category - ABAP

When you create Core Data Services in the system, numerous types are available. This article will take a look at the different types and their uses.

11/21/2025

CDS - Writable View Entity

Category - ABAP

Can you perform an update to a Core Data Service in ABAP? Let's look at the new CDS view entities.

04/01/2025

CDS - Authority check (Part 2)

Category - ABAP

How do you deal with the issue of access control in Core Data Services in ABAP and how can you analyze errors? Read more in the article.

03/14/2025