Generalize the call to conversion exit functions

By | May 21, 2012 | Tricks, Utilities | 11,599 | 1

Well, everybody will be aware of the conversion exit functions available in SAP. The SAP provided conversion exit functions must be called individually in the programs to convert the field value internally and externally. Now, why can’t we do this dynamically by avoiding individual calls to the functions?

Background

Yes, it can be done . Many of you would have come across the statement “DESCRIBE” in ABAP which has an option called “MASK” which provides the conversion routine defined for the field in the ABAP Dictionary. Each conversion routine is linked to two function modules such as “CONVERSION_EXIT_XX_INPUT” and “CONVERSION_EXIT_XX_OUTPUT”,where the ‘XX’ is replaced by the “MASK”. SAP standard code uses the CONCATENATE statement along with the MASK to display the conversion exit functions through transaction SE11 ( available in include LSD11F01 , routine OBJ_GOTO ).

Other than using concatenate statement to build the function module name, the function name can be also be selected from DB view V_FDIR with search pattern FUNCNAME = ‘CONVERSION_EXIT**’.

So using the concatenate statement let’s generalize the call to conversion exits.This idea doesn’t include the range conversion exits functions like CONVERSION_EXIT_MATN1_RANGE_I etc.

Generalize Conversion Exit

Create a function module like below:

Generalize Conversion Exit

 
FUNCTION ygeneralize_conversion.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(INPUT_VALUE)
*"     REFERENCE(INPUT_OUTPUT) TYPE  I
*"  EXPORTING
*"     REFERENCE(OUTPUT_VALUE)
*"----------------------------------------------------------------------
 
  FIELD-SYMBOLS: <fs_t> TYPE ANY.
 
  DATA:lfd_convexit TYPE funcname,
       wf_data      TYPE REF TO data.
 
  DATA:lfd_length  TYPE i,
       lfd_mask    TYPE string.
 
* Determine the mask from the domain
  DESCRIBE FIELD input_value OUTPUT-LENGTH lfd_length
                             EDIT MASK     lfd_mask.
  IF lfd_mask IS INITIAL.
 
* This statement is included because its not necessary that all field
* will be having mask associated with it, for example: ATNAM. To get the
* conversion exit of ATNAM the exporting parameter will be holding the 
* respective mask
 
    DESCRIBE FIELD output_value OUTPUT-LENGTH lfd_length
                                EDIT MASK     lfd_mask.
    IF lfd_mask IS INITIAL.
      IF output_value IS INITIAL.
        output_value = input_value.
      ENDIF.
      EXIT.
    ENDIF.
  ENDIF.
 
  REPLACE '==' IN lfd_mask WITH space.
  CONDENSE lfd_mask NO-GAPS.
 
  CREATE DATA wf_data TYPE c LENGTH lfd_length.
  IF wf_data IS BOUND.
    ASSIGN wf_data->* TO <fs_t>.
    IF  IS NOT ASSIGNED.
      IF output_value IS INITIAL.
        output_value = input_value.
      ENDIF.
      EXIT.
    ENDIF.
  ELSE.
    IF output_value IS INITIAL.
      output_value = input_value.
    ENDIF.
    EXIT.
  ENDIF.
 
  IF input_output = 1.
    CONCATENATE 'CONVERSION_EXIT_' lfd_mask '_INPUT' INTO lfd_convexit.
  ELSEIF input_output = 2.
    CONCATENATE 'CONVERSION_EXIT_' lfd_mask '_OUTPUT' INTO lfd_convexit.
  ENDIF.
 
  CALL FUNCTION lfd_convexit
    EXPORTING
      input  = input_value
    IMPORTING
      output = <fs_t>.
  IF sy-subrc NE 0.
    output_value = input_value.
  ELSE.
    output_value = <fs_t>.
  ENDIF.
 
ENDFUNCTION.
 
 

Generalize Conversion Exit Usage

Now, try calling this function from a program like below:

Generalize Conversion Exit Usage

 
Report YCONVERSION.
 
DATA:l_atnam TYPE cabn-atnam,
     l_ebeln type ekko-ebeln.
 
PARAMETERS:pa_ebeln TYPE ebeln,
           pa_atinn TYPE atinn.
 
CALL FUNCTION 'YGENERALIZE_CONVERSION'
  EXPORTING
    input_value  = pa_atinn
    input_output = 2
  IMPORTING
    output_value = l_atnam.
 
CALL FUNCTION 'YGENERALIZE_CONVERSION'
  EXPORTING
    input_value  = l_atnam
    input_output = 1
  IMPORTING
    output_value = pa_atinn.
* Check the values in l_atnam , pa_atinn
break-point.
 
CALL FUNCTION 'YGENERALIZE_CONVERSION'
  EXPORTING
    input_value  = pa_ebeln
    input_output = 2
  IMPORTING
    output_value = l_ebeln.
 
CALL FUNCTION 'YGENERALIZE_CONVERSION'
  EXPORTING
    input_value  = l_ebeln
    input_output = 1
  IMPORTING
    output_value = pa_ebeln.
* Check the values in l_atnam , pa_atinn
break-point.
 
 

Any comments & suggestions are welcome.

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Kesavadas Thekkillath{3 articles}

Working with AtoS as Systems Analyst. I love coding in ABAP & OO ABAP. Top Contributor in SCN Community Network

Explore all of his 3 articles.

Load comments

1 Comment

  • Kjetil Kilhavn

    Nice idea, but I’d remove that parameter for direction (input/output). There’s no real point in it, and it adds an unnecessary point of failure (invalid input to that parameter). I would rather create two functions Y_CONVERSION_EXIT_INPUT and Y_CONVERSION_EXIT_OUTPUT. The common parts can be delegated to a FORM in the function group.

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.