ABAP: Conversion INPUT / OUTPUT like ALPHA, etc.

In ABAP you can have types which requires a conversion because the Database wants all numbers filled with zeros. Therefore exists Functions to convert the data the required way. One example is the function CONVERSION_EXIT_ALPHA_INPUT and CONVERSION_EXIT_ALPHA_OUTPUT. This Functions get any number fields like '123' and fill it with zeros until the length of defined type. F.e. when the type is defined as CHAR length 5. It fills so: ‚00123‘.

Sometimes it is hard to recognize if a given variable has an CONVERSION Function. Especially when you write more dynamic code and you work with Field-symbols or ANY variables.

So i write a small function, which might come handy to you. It receives i_value and exports e_value with TYPE ANY and looks up if the given Value has a CONVERSION Function and if it has it calls it. So have fun with never ever caring about conversion-functions.

Comments are welcome

*Copyright (c) 2009, J.Rumpf, www.web-dreamer.de / BSD Licence
*All rights reserved.
*
*Redistribution and use in source and binary forms, with or without modification
*are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, th
* is list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of the owner nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific
* prior written permission.
*
*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
*ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
*WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
*DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
*FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
*DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
*SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
*OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
*OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

IMPORTING i_value type ANY .
EXPORTING e_value type ANY.

DATA:
ref_typ TYPE REF TO cl_abap_typedescr,
red_d TYPE REF TO cl_abap_elemdescr,
routine TYPE c LENGTH 30,
error TYPE REF TO cx_root.
TRY.
CALL METHOD cl_abap_typedescr=>describe_by_data “Run time Type Service rufen
EXPORTING
p_data = i_value
RECEIVING
p_descr_ref = ref_typ. ” Elementarer typ?
red_d ?= ref_typ. ” bind Elementtype
IF red_d IS BOUND.
IF red_d->edit_mask IS NOT INITIAL.
routine = red_d->edit_mask.
REPLACE ALL OCCURRENCES OF ‘=’ IN routine WITH ”.
CONDENSE routine NO-GAPS.
CONCATENATE ‘CONVERSION_EXIT_’ routine ‘_INPUT’ INTO routine.
CALL FUNCTION routine ” call Conversion routine
EXPORTING
input = i_value
IMPORTING
output = e_value.
ELSE.
e_value = i_value.
ENDIF.
ELSE.
RAISE EXCEPTION “TODO your exception type
.
ENDIF.
CATCH cx_root INTO error.
RAISE EXCEPTION “TODO your exception type
previous = error
.
ENDTRY.


Beitrag veröffentlicht

in

, ,

von

Schlagwörter: