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: 
AnslemArnolda
Participant
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.
6 Comments