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

2437

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.


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

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.

08/25/2023

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.

07/29/2022

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.

06/10/2022

CDS - Virtual fields

Category - ABAP

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

06/03/2022