Currency Conversion in SAP ABAP Using CONVERT_TO_LOCAL_CURRENCY with Custom F4 Help

Estimated read time 5 min read

Introduction :

Currency conversion is a common requirement in SAP applications, especially when working with international business transactions. SAP provides the standard function module CONVERT_TO_LOCAL_CURRENCY  to convert an amount from one currency to another using the exchange rates maintained in the system. In this blog, we will create a simple ABAP report that allows users to select source and target currencies using custom F4 help and perform currency conversion based on the latest exchange rate.

Code :

REPORT zva_convert_currency.

*———————————————————————*
* Selection Screen for Input Parameters
*———————————————————————*
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.

PARAMETERS:
p_amount TYPE ktwrt, ” Amount to be converted
p_fcurr TYPE tcurc-waers, ” Source currency
p_tcurr TYPE tcurc-waers. ” Target currency

SELECTION-SCREEN END OF BLOCK b1.

*———————————————————————*
* F4 Help for Source Currency (From Currency)
*———————————————————————*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fcurr.
” Call reusable F4 help form
PERFORM get_currency_f4 USING ‘P_FCURR’.

*———————————————————————*
* F4 Help for Target Currency (To Currency)
*———————————————————————*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_tcurr.
” Call reusable F4 help form
PERFORM get_currency_f4 USING ‘P_TCURR’.

*———————————————————————*
* Main Processing Logic – Currency Conversion
*———————————————————————*
START-OF-SELECTION.

” Variable to store converted amount
DATA: lv_local_amount TYPE ktwrt.

” Standard FM for currency conversion
CALL FUNCTION ‘CONVERT_TO_LOCAL_CURRENCY’
EXPORTING
date = sy-datum ” Conversion date
foreign_amount = p_amount ” Input amount
foreign_currency = p_fcurr ” Source currency
local_currency = p_tcurr ” Target currency
type_of_rate = ‘M’ ” Exchange rate type (M = average rate)
read_tcurr = ‘X’ ” Read TCURR table
IMPORTING
local_amount = lv_local_amount ” Converted amount result
EXCEPTIONS
no_rate_found = 1
overflow = 2
no_factors_found = 3
no_spread_found = 4
derived_2_times = 5
OTHERS = 6.

” Check conversion success
IF sy-subrc = 0.
WRITE: / ‘Amount :’, p_amount,
/ ‘From Currency :’, p_fcurr,
/ ‘To Currency :’, p_tcurr,
/ ‘Converted Amount :’, lv_local_amount.
ELSE.
WRITE: / ‘Currency conversion failed.’.
ENDIF.

*———————————————————————*
* Form Routine – F4 Help for Currency Fields
*———————————————————————*
FORM get_currency_f4 USING pv_field.

” Internal table to hold currency list
SELECT a~waers,
b~ltext
FROM tcurc AS a
INNER JOIN tcurt AS b
ON a~waers = b~waers
WHERE b~spras = @SY-langu
INTO TABLE @DATA(lt_list).

” Standard F4 help function module
CALL FUNCTION ‘F4IF_INT_TABLE_VALUE_REQUEST’
EXPORTING
retfield = ‘WAERS’ ” Return field
dynpprog = sy-repid ” Current program
dynpnr = sy-dynnr ” Screen number
dynprofield = pv_field ” Target field on screen
value_org = ‘S’ ” Structured values
TABLES
value_tab = lt_list.

ENDFORM.

Output :

Conclusion :

Using the standard function module CONVERT_TO_LOCAL_CURRENCY , currency conversion can be implemented easily and efficiently in ABAP. By combining it with a custom F4 help for currency selection, users can quickly choose currencies and perform conversions without manual input errors. This approach provides a simple and user-friendly solution for handling currency conversions in SAP applications.

 

 

​ Introduction :Currency conversion is a common requirement in SAP applications, especially when working with international business transactions. SAP provides the standard function module CONVERT_TO_LOCAL_CURRENCY  to convert an amount from one currency to another using the exchange rates maintained in the system. In this blog, we will create a simple ABAP report that allows users to select source and target currencies using custom F4 help and perform currency conversion based on the latest exchange rate.Code :REPORT zva_convert_currency.

*———————————————————————*
* Selection Screen for Input Parameters
*———————————————————————*
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.

PARAMETERS:
p_amount TYPE ktwrt, ” Amount to be converted
p_fcurr TYPE tcurc-waers, ” Source currency
p_tcurr TYPE tcurc-waers. ” Target currency

SELECTION-SCREEN END OF BLOCK b1.

*———————————————————————*
* F4 Help for Source Currency (From Currency)
*———————————————————————*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fcurr.
” Call reusable F4 help form
PERFORM get_currency_f4 USING ‘P_FCURR’.

*———————————————————————*
* F4 Help for Target Currency (To Currency)
*———————————————————————*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_tcurr.
” Call reusable F4 help form
PERFORM get_currency_f4 USING ‘P_TCURR’.

*———————————————————————*
* Main Processing Logic – Currency Conversion
*———————————————————————*
START-OF-SELECTION.

” Variable to store converted amount
DATA: lv_local_amount TYPE ktwrt.

” Standard FM for currency conversion
CALL FUNCTION ‘CONVERT_TO_LOCAL_CURRENCY’
EXPORTING
date = sy-datum ” Conversion date
foreign_amount = p_amount ” Input amount
foreign_currency = p_fcurr ” Source currency
local_currency = p_tcurr ” Target currency
type_of_rate = ‘M’ ” Exchange rate type (M = average rate)
read_tcurr = ‘X’ ” Read TCURR table
IMPORTING
local_amount = lv_local_amount ” Converted amount result
EXCEPTIONS
no_rate_found = 1
overflow = 2
no_factors_found = 3
no_spread_found = 4
derived_2_times = 5
OTHERS = 6.

” Check conversion success
IF sy-subrc = 0.
WRITE: / ‘Amount :’, p_amount,
/ ‘From Currency :’, p_fcurr,
/ ‘To Currency :’, p_tcurr,
/ ‘Converted Amount :’, lv_local_amount.
ELSE.
WRITE: / ‘Currency conversion failed.’.
ENDIF.

*———————————————————————*
* Form Routine – F4 Help for Currency Fields
*———————————————————————*
FORM get_currency_f4 USING pv_field.

” Internal table to hold currency list
SELECT a~waers,
b~ltext
FROM tcurc AS a
INNER JOIN tcurt AS b
ON a~waers = b~waers
WHERE b~spras = @SY-langu
INTO TABLE @DATA(lt_list).

” Standard F4 help function module
CALL FUNCTION ‘F4IF_INT_TABLE_VALUE_REQUEST’
EXPORTING
retfield = ‘WAERS’ ” Return field
dynpprog = sy-repid ” Current program
dynpnr = sy-dynnr ” Screen number
dynprofield = pv_field ” Target field on screen
value_org = ‘S’ ” Structured values
TABLES
value_tab = lt_list.

ENDFORM.Output :Conclusion :Using the standard function module CONVERT_TO_LOCAL_CURRENCY , currency conversion can be implemented easily and efficiently in ABAP. By combining it with a custom F4 help for currency selection, users can quickly choose currencies and perform conversions without manual input errors. This approach provides a simple and user-friendly solution for handling currency conversions in SAP applications.    Read More Technology Blog Posts by Members articles 

#SAP

#SAPTechnologyblog

You May Also Like

More From Author