Technical Articles
How to use any conversion routine in ABAP 7.4 New syntax
Introduction:
In ABAP, we have several conversion routines available to convert data into external or internal formats. In the new syntax, we can only directly use the ALPHA conversion routine. For other conversion exit function modules, we need to call their respective functions.
In the FOR syntax, it is not possible to directly use other conversion exit function modules. However, the proposed solution will help us to overcome the issue.
Problem Statement:
Normally, when we need to convert fields within an internal table, we call the respective conversion exit function modules inside a LOOP and ENDLOOP block. When utilizing the new syntax to iterate through an internal table, we use the FOR syntax.
In a FOR loop, we can only utilize the ALPHA conversion routine for conversions in both input and output directions. To perform other conversions, we still need to revert to using a LOOP and ENDLOOP construct.
SELECT vbeln,posnr, matnr, kwmeng, vrkme
FROM vbap
INTO TABLE @DATA(lt_vbap)
WHERE vbeln IN @s_vbeln.
IF sy-subrc = 0.
LOOP AT lt_vbap INTO DATA(ls_vbap).
CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT'
EXPORTING
input = ls_vbap-matnr
IMPORTING
output = ls_vbap-matnr.
APPEND VALUE #( converted_matnr = ls_vbap-matnr ) TO lt_display.
ENDLOOP.
cl_demo_output=>display( lt_display ).
ENDIF.
Proposed Solution:
To avoid LOOP and ENDLOOP for conversions, we have to do the following steps in our program.
Step 1: Create a local class for the conversion routine. Based on our requirements, define one or more methods within the class. These method(s) should have a RETURNING VALUE parameter that stores the output of the method(s).
CLASS lcl_conversion_routine DEFINITION.
PUBLIC SECTION.
METHODS:
matnr_input IMPORTING iv_matnr TYPE matnr
RETURNING VALUE(rv_matnr) TYPE matnr,
matnr_output IMPORTING iv_matnr TYPE matnr
RETURNING VALUE(rv_matnr) TYPE matnr,
menge_output IMPORTING iv_menge TYPE any
iv_meins TYPE meins
RETURNING VALUE(rv_menge) TYPE char17,
meins_output IMPORTING iv_meins TYPE meins
RETURNING VALUE(rv_meins) TYPE meins.
ENDCLASS.
Step 2: In the method implementation, call the respective function modules by passing importing and returning parameters.
CLASS lcl_conversion_routine IMPLEMENTATION.
METHOD matnr_input.
*& Material Conversion External to Internal.
CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
EXPORTING
input = iv_matnr
IMPORTING
output = rv_matnr
EXCEPTIONS
length_error = 1
OTHERS = 2 ##FM_SUBRC_OK.
ENDMETHOD.
METHOD matnr_output.
*& Material Conversion Internal to External.
CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT'
EXPORTING
input = iv_matnr
IMPORTING
output = rv_matnr ##FM_SUBRC_OK.
ENDMETHOD.
METHOD meins_output.
*& Unit of Measure Internal to External Format.
CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT'
EXPORTING
input = iv_meins
IMPORTING
output = rv_meins
EXCEPTIONS
unit_not_found = 1
OTHERS = 2 ##FM_SUBRC_OK.
ENDMETHOD.
METHOD menge_output.
*& Quantity Internal to External Format.
WRITE iv_menge TO rv_menge UNIT iv_meins.
ENDMETHOD.
ENDCLASS.
TYPES: BEGIN OF ty_display,
unconverted_matnr TYPE text30,
converted_matnr TYPE text30,
unconverted_menge TYPE text30,
converted_menge TYPE text30,
unconverted_meins TYPE text30,
converted_meins TYPE text30,
END OF ty_display.
DATA: lt_display TYPE TABLE OF ty_display.
Step 3: In this scenario, need to convert the Material, Quantity and Unit of measure to external format. Create an object for the class and call the respective methods to convert field values to external format.
SELECT vbeln,posnr, matnr, kwmeng, vrkme
FROM vbap
INTO TABLE @DATA(lt_vbap)
WHERE vbeln IN @s_vbeln.
IF sy-subrc = 0.
lt_display = VALUE #( LET lo_conversion = NEW lcl_conversion_routine( ) IN
FOR ls_vbap IN lt_vbap
( unconverted_matnr = ls_vbap-matnr
unconverted_menge = ls_vbap-kwmeng
unconverted_meins = ls_vbap-vrkme
converted_matnr = lo_conversion->matnr_output( iv_matnr = ls_vbap-matnr )
converted_menge = lo_conversion->menge_output( iv_menge = ls_vbap-kwmeng
iv_meins = ls_vbap-vrkme )
converted_meins = lo_conversion->meins_output( iv_meins = ls_vbap-vrkme ) ) ).
lt_display = VALUE #( LET lt_external_values = lt_display
lo_conversion = NEW lcl_conversion_routine( ) IN
BASE lt_display
FOR ls_external_value IN lt_external_values
( unconverted_matnr = ls_external_value-converted_matnr
converted_matnr = lo_conversion->matnr_input( iv_matnr = CONV matnr( ls_external_value-converted_matnr ) ) ) ).
cl_demo_output=>display( lt_display ).
ENDIF.
Output of the program:
Conclusion:
By this way, we can use any conversion routines in the FOR syntax without calling the function modules directly. hope this will helpful.
Thankyou Saran, It's really helpful๐
What about making it fully generic? (conversion exit name as a parameter, only two methods, INPUT and OUTPUT)
Hi Sandra,
Obviously, we can make it more generic. We need to pass the function module name along with the corresponding input fields as input parameters.
Thanks. Too much generic for me, I'd keep one method per "direction", but it's personal ๐
very good
Hi Saran,
In the FOR Syntax, It is possible to use conversion routine. You can refer below block of code.
Also, its just a suggestion which basically makes your code simple & easy to understand.
Thanks
Hi Shrikant Patil,
The ALPHA IN/OUT conversion routines are only used for removing and adding leading zeros. They are not applicable for other conversion routines such as Material - MATN1, UoM - CUNIT, etc.
To change internal or external formats, we need to use the respective conversion exits. In regular ABAP, we can call the conversion exit directly. However, when using the FOR syntax, we cannot directly call the Function Module. Instead, we require a wrapper method to accomplish the same task.
I hope this is clear to you. Thank you.
Hi Saran,
Yes, I got your point. It's not gonna work for CUNIT.
Thanks for sharing! If someone really needs to use these conversion routines, it's definitely helpful. I haven't used them in ages though because ALV and SAP Gateway (for OData services) provide great support for these conversions out of the box. It is important to reference correct data types. Too many times I see conversion called unnecessarily where a better reference could solve it. So, good approach but check first if it's actually required. ๐
It would be neat though to have built-in conversion similar to ALPHA.