SAP ABAP CDS View
The function of currency_conversion
is a function for performing currency conversion in CDS (Core Data Services) view. In SAP systems, it is often necessary to convert between different currencies in order to present consistent currency amounts in reports and analysis. currency_conversion
Functions allow defining currency conversion rules in CDS views to convert values from one currency to another in query results. This is useful in multi-country or multi-currency business environments.
currency_conversion
The syntax of the function is as follows:
currency_conversion(
amount = source_amount,
source_cur = source_currency,
target_cur = target_currency,
validitydate = date,
factor = factor_type,
decimal = rounding_mode
) as converted_amount
Let's elaborate on the meaning and function of these parameters:
-
source_amount
: The original amount that needs currency conversion. It can be a field or a constant value in a CDS view. -
source_currency
: The currency code of the original amount. It can be a field or a constant value in a CDS view. -
target_currency
: The target currency code, which is the currency to convert the amount to. It can be a field or a constant value in a CDS view. -
validitydate
: An optional parameter that specifies the effective date for the conversion rate. If this parameter is not provided, the system will use the current date. -
factor_type
: Optional parameter to specify which type of exchange rate factor to use for conversion. For example, you can choose to use average rates, end-of-month rates, yearly rates, etc. If this parameter is not provided, the system will use the default exchange rate type. -
rounding_mode
: An optional parameter that specifies how the converted amount is rounded. For example, you can specify rounding or truncation. If this parameter is not provided, the system will use the default rounding method. -
converted_amount
: As a result, this name is used to store the transformed amount, which will become part of the CDS view and returned in the query results.
Now, let us illustrate the usage and effect of the function through an example currency_conversion
. Suppose we have a SAP system that contains a SalesOrder
CDS view named CDS that displays information about a sales order, including the order amount and order currency. We want to convert the order amount from the order currency to the local currency so we can view the order amount in the local currency.
Suppose we have the following data:
order number | order amount | order currency |
---|---|---|
1001 | 1000 | USD |
1002 | 1500 | EUR |
1003 | 2000 | GBP |
We want to convert these order amounts into local currency (assume local currency is CNY), and assume the conversion rates are 1 USD = 6.5 CNY, 1 EUR = 7.8 CNY, 1 GBP = 8.5 CNY.
currency_conversion
First, to use a function for currency conversion in a CDS view , we can define the view like this:
@AbapCatalog.sqlViewName: 'ZSALESORDER'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Sales Order View'
define view ZSalesOrder as select from sflights as flights {
key flights.carrid,
key flights.connid,
flights.fldate,
currency_conversion(
amount = flights.price,
source_cur = flights.currency,
target_cur = 'CNY',
validitydate = flights.fldate
) as local_amount
} where flights.price is not null;
In the CDS view above, we used currency_conversion
a function to convert a field flights
in the table price
(OrderAmount) from currency
a field (OrderCurrency) to the local currency CNY
and stored the result in local_amount
a new field called .
Now, when we query this CDS view:
select carrid, connid, fldate, price, currency, local_amount
from ZSalesOrder;
The query results will look like this:
carrid | connid | fldate | price | currency | local_amount |
---|---|---|---|---|---|
AA | 0017 | 2023-01-12 | 1000 | USD | 6500 |
LH | 0400 | 2023-02-28 | 1500 | EUR | 11700 |
BA | 0080 | 2023-03-15 | 2000 | GBP | 17000 |
In the query results, local_amount
the field shows the value of the order amount in local currency (CNY), converted using the corresponding exchange rate. For example, the order amount in the first line is converted from USD to CNY, and the result is 6500 CNY.
Summarize
SAP ABAP CDS View
The currency_conversion
function is a powerful currency conversion tool that allows flexible currency conversion rules to be defined in CDS views, ensuring accurate presentation of data in a multi-currency environment. By judicious use of currency_conversion
functions, enterprises can better manage multi-country business and get consistent currency amounts in reports and analysis.