Skip to Content
Technical Articles
Author's profile photo Anslem Arnolda

Dynamic Internal Table Value Conversions

Value conversions within SAP causes headaches for many ABAP developers as knowing the data type for each variable for conversion is a hassle. Also converting values within an internal table in one go could reduce the number of code lines a developer needs to write.
The example given below will give you a clear understanding on how we could implement a common method to do value conversions for any given data type. In other words, this will be a replacement to value conversion FM’s such as ‘CONVERSION_EXIT_ALPHA_INPUT’, ‘CONVERSION_EXIT_MATN1_INPUT’, | {  variable alpha = in}| (>ABAP 7.4). Another method is given below to convert values in a dynamic internal table.
By using the above two methods together, we can create a common functionality to do conversions for any dynamically created internal table.
It is recommended to use the following functionality within a utility class that is accessible globally within SAP. Static methods could be used to implement the logic in value conversions for any dynamic internal table. This will also allow developers to call each method independently where it could be used for single data type value conversions as well.

First lets see how we can implement a common method to convert any input value. The code for this method is given below.

 

Importing Parameter

IV_INPUT	TYPE ANY

Exporting Parameter

value( EV_OUTPUT )	TYPE ANY

Method Implementation ()

    data: lo_elem      type ref to cl_abap_elemdescr,
          lo_type      type ref to cl_abap_typedescr,
          ls_fieldinfo type rsanu_s_fieldinfo.

    "Set initial output value to input value. This allows to exit in failure conditions.
    ev_output = iv_input.

    lo_elem ?= cl_abap_elemdescr=>describe_by_data( iv_input ).

    "If the data has no DDIC structure, exit
    if lo_elem->is_ddic_type( ) <> abap_true.
      return.
    endif.

    data(ls_dfies) = lo_elem->get_ddic_field( sy-langu ).

    "If DDIC structure has no conversion exit, exit.
    if ls_dfies-convexit is initial.
      return.
    endif.

    "If alpha just convert right away and return
    if ls_dfies-convexit = gc_alpha.
      ev_output = |{ iv_input alpha = in }|.
      return.
    endif.

    move-corresponding ls_dfies to ls_fieldinfo.

    try.
        cl_rsan_ut_conversion_exit=>try_conv_int_ext_int(
          exporting
            i_fieldinfo = ls_fieldinfo
            i_value = iv_input
            i_conversion_errors_type = '*'
          importing
            e_value = ev_output ).
      catch cx_root into data(lv_exc).
        clear ev_output.
        return.
    endtry.
The above method could be used to create our next static method, which will be used for value conversion of any field in an internal table. The method implementation is as follows.

Changing Parameter

IT_OUT	TYPE TABLE

Method Implementation ()

    data: lt_table type ref to cl_abap_tabledescr,
          lt_data  type ref to data.

    get reference of it_out into lt_data.
    lt_table  ?= cl_abap_structdescr=>describe_by_data_ref( lt_data ).

    loop at it_out assigning field-symbol(<fs_out>).

      loop at lt_table->key assigning field-symbol(<fs_fields>).
        assign component <fs_fields> of structure <fs_out> to field-symbol(<fs_value>).
        if <fs_value> is assigned.
          "Call the previous method here
        endif.
      endloop.

    endloop.

 

By looking at the above two methods we can see how common methods as such could be implement to make the life a developer easier. I will be adding more such findings in near future.

Assigned Tags

      6 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Sandra Rossi
      Sandra Rossi

      Instead of cl_rsan_ut_conversion_exit=>try_conv_int_ext_int, I think you should better use the function module RS_CONV_EX_2_IN, because it's officially released.

      Author's profile photo Anslem Arnolda
      Anslem Arnolda
      Blog Post Author

      Thank you for the suggestion Sandra.

      Author's profile photo Tomas Buryanek
      Tomas Buryanek

      Hello,
      have you thought about calling conversion exit FM dynamically? If yes, then why did you choose not do it this way?

      Example:

      function_name = 'CONVERSION_EXIT_' && ls_dfies-convexit && '_INPUT'.
      
      CALL FUNCTION function_name ...
      Author's profile photo Anslem Arnolda
      Anslem Arnolda
      Blog Post Author

      Hello!

      Yes this is possible too. But these functions are outdated from ABAP 7.4 above. And I have used the utility class here to keep the consistency of using methods.

       

      Author's profile photo Tomas Buryanek
      Tomas Buryanek

      I think they are still OK to use (not obsolete). And only the ALPHA conversion has modern alternative in Embedded Expressions.

      Author's profile photo Bernd Dittrich
      Bernd Dittrich

      Thanks for sharing! To call the output conversion you can also consider using the write statement which triggers the respective conversion exit bound to the DE implicitly , so in your case a

      WRITE <fs_value> to <fs_value>.

      might also do the trick.

      This works not for the other direction, unfortunately...