cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

CDS View Entity Incorrect Currency Format

SilasL
Explorer
0 Kudos
7,186

Hey All,

I'm trying to get a CDS view entity to take a currency amount and convert that amount to follow the format for the given currency code. I have a minimal version of my CDS view below.

@AbapCatalog.viewEnhancementCategory: [#NONE]

@AccessControl.authorizationCheck: #NOT_REQUIRED

@EndUserText.label: 'CDS for Currency Convertion'

@Metadata.ignorePropagatedAnnotations: true

@ObjectModel.usageType:{

serviceQuality: #X,

sizeCategory: #S,

dataClass: #MIXED

}

define view entity z_cds_currency_convert

as select from bseg

inner join t001 on bseg.bukrs = t001.bukrs

{

key t001.waers,

@Semantics.amount.currencyCode: 'waers'

bseg.dmbtr as original_amount,

cast('ADP' as abap.cuky) as new_key,

@Semantics.amount.currencyCode: 'new_key'

bseg.dmbtr as converted_amount

}

I've been testing with bseg-dmbtr, and I have one column of bseg-dmbtr that uses the local currency code from t001, and another column that uses a hard coded currency code in the view. This is what currency code ADP looks like in my system's table TCURX, in the table it has currency decimal 0.

So I'm expecting the output to have the currency amounts with no decimal places. However this is what I see when I go to data preview.

I've tried using currency_conversion function which has a parameter for target_currency, and that also hasn't worked. I tried every combination of decimal_shift and decimal_shift_back and it seems to also not have any effect on the output. Currency Code with more than 2 decimals also doesn't work.

However when I go back to CDS views, I can use decimal_shift and that works properly. Here is the code for the CDS View.

@AbapCatalog.sqlViewName: 'Z_CDS_CURRENCY'

@AbapCatalog.compiler.compareFilter: true

@AbapCatalog.preserveKey: true

@AccessControl.authorizationCheck: #NOT_REQUIRED

@EndUserText.label: 'CDS for Currency Test'

define view z_cds_currency_test_qzgbh1

with parameters

iv_target_key :abap.cuky

as select from bseg

inner join t001 on bseg.bukrs = t001.bukrs

{

@Semantics.currencyCode: true

t001.waers,

@Semantics.amount.currencyCode: 'waers'

bseg.dmbtr as original_amount,

@Semantics.currencyCode: true

cast ('ADP' as abap.cuky) as new_key,

@Semantics.amount.currencyCode: 'new_key'

decimal_shift(amount => bseg.dmbtr, currency => $parameters.iv_target_key ) as new_amount

}

With the following output

However decimal_shift isn't support for CDS View entity. Does anyone know how I can set up my CDS View entity to do the same?

Thanks,

Silas

View Entire Topic
ozturkozan
Explorer
0 Kudos

Hi Silas,

currency_conversion function should work for CDS. You can check below code. For sure, exchange rates for given date should be available at TCURR table for given date.

      currency_conversion(
        amount             => bseg.dmbtr,
        source_currency    => t001.waers,
        target_currency    => cast('ADP' as abap.cuky),
        exchange_rate_date => date which you want to make exchange rate conversion
      )        as PriceInADP
SilasL
Explorer
0 Kudos

Hey Ozan,

The currency_conversion didn't work for me, here is how I used currency_conversion.

define view entity z_cds_currency_convert

as select from bseg

inner join t001 on bseg.bukrs = t001.bukrs

{

key t001.waers,

@Semantics.amount.currencyCode: 'waers'

bseg.dmbtr as original_amount,

cast('IQD' as abap.cuky) as new_key,

@Semantics.amount.currencyCode: 'new_key'

currency_conversion(

amount => bseg.dmbtr,

source_currency => t001.waers,

target_currency => cast('IQD' as abap.cuky),

exchange_rate_date => $session.system_date,

client => $session.client ) as new_amount

}

I had to change to IQD as my system doesn't have an exchange rate between ADP and USD. So IQD has the following values in table tcurx.

So I'm expecting a currency amount with 3 decimal places.

However when I run this CDS View entity in data preview, I see the following output.

The converted amount doesn't have 3 decimal places.

Thanks,

Silas

ozturkozan
Explorer
0 Kudos

CURRENCY_CONVERSION function has decimal_shift and decimal_shift_back parameters. You can check below link.

https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abencds_f1_conv_func_unit_curr.htm#!ABAP_...

SilasL
Explorer
0 Kudos

Hey Ozan,

I've tried changing those parameters and the results are the same. The converted amount still only has 2 decimal places.

key t001.waers,

@Semantics.amount.currencyCode: 'waers'

bseg.dmbtr as original_amount,

cast('IQD' as abap.cuky) as new_key,

@Semantics.amount.currencyCode: 'new_key'

currency_conversion(

amount => bseg.dmbtr,

source_currency => t001.waers,

target_currency => cast('IQD' as abap.cuky),

exchange_rate_date => $session.system_date,

round => '',

client => $session.client,

decimal_shift => '',

decimal_shift_back => 'X'

) as new_amount

This is what my new currency_conversion looks like. I tried turning round off because I was wondering if it was rounding the number and the 3rd decimal digit was becoming 0 and maybe SAP wasn't showing that 0 at the end. I turned decimal_shift off, and left decimal_shift_back on since the parameter's description says, decimal places of the result are moved as specified by the decimal places of the target currency.

Data Preview looks like this.

Thanks,

Silas