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

2886

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



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


CDS - Migration of Views

Category - ABAP

Do you still have a lot of old Core Data Services in your ABAP system? Time to migrate to the new entity.

11/15/2024

CDS - Types and Enums

Category - ABAP

What will replace the data elements in the ABAP Dictionary and how can you use the types for Core Data Services today? Find out more here.

11/05/2024

ABAP in Practice - String Processing

Category - ABAP

In this practical example we look at the string processing to determine the CDS names in CamelCase and how you can implement this with ABAP.

10/15/2024

ABAP - CDS Extraktor

Category - ABAP

How does the CDS extractor work in ABAP and what challenges are there when developing with it? In this article we will look at a few details.

09/20/2024

ABAP Tools - Working with Eclipse (CDS Templates)

Category - ABAP

Did you select the wrong CDS template when creating the view? Here's a little tip to correct the view in ABAP afterwards.

07/02/2024