In this post, you will learn,

  • How to use currency conversion in CDS View
  • How to use unit conversion in CDS View

Read earlier posts in this series at Exploring ABAP on HANA.

In ABAP, we use one of below or similar FM to convert amounts from one currency to another.

  1. CONVERT_AMOUNT_TO_CURRENCY
  2. CONVERT_TO_LOCAL_CURRENCY

We need to do this in a loop when we need to do this for multiple records. With CDS, we can do this while we fetch the data. Interesting, right?

Currency Conversion

Consider below table data. I have created a custom table for this demonstration. As you can see, GROSSCOL is a column that has gross collection for the movies in the table and all records have currency USD.

Let us convert it to a another currency, say EUR using a CDS view. The target currency can be passed using a parameter, to make this more dynamic.

Currency Conversion in CDS View

Execute the CDS view, provide the target currency as ‘EUR’ and the conversion is done.

The conversion errors can also be handled with possible error handling values as below.

  • FAIL_ON_ERROR – an error raises an exception. This is also the default if nothing is specified.
  • SET_TO_NULL – the result is reset to null value.
  • KEEP_UNCONVERTED – the source value is not changed.

Error handling node is specified as below.

Unit Conversion

Unit conversion has a similar syntax but it has less parameters than Currency conversion. Here is a standard demo view that explains Unit Conversion.

Code Reference :

"zjp_currency_conv

@AbapCatalog.sqlViewName: 'ZJP_SQL_CC'
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Currency Conversion'
define view zjp_currency_conv with parameters
    @Environment.systemField: #SYSTEM_DATE
    conv_date       : abap.dats,
    target_currency : abap.cuky
  as select from zmmovie {
  key movie                as Movie,
      @Semantics.amount.currencyCode : 'Currency'
      grosscol             as GrossCollection,
      @Semantics.currencyCode: true
      currency             as Currency,
      
      @Semantics.amount.currencyCode : 'TargetCurrency'
      currency_conversion( amount             => grosscol,
                           source_currency    => currency,
                           round              => 'X',
                           target_currency    => :target_currency,
                           exchange_rate_type => 'M',
                           exchange_rate_date => :conv_date,
                           error_handling     => 'SET_TO_NULL'
                         ) as TargetGrossCollection,
      @Semantics.currencyCode: true
      :target_currency     as TargetCurrency
} 

Visit ABAP on HANA series for Tutorials on CDS, AMDP, Eclipse, and ALV IDA.


If you like the content, please subscribe…

Join 4,016 other subscribers

Discovering ABAP YouTube Channel