Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
saranramu
Explorer

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.

 

 

 

 

 
10 Comments
Labels in this area