Adding leading Zero:

Well, this is a pretty common requirement in the ABAP world. Adding leading zero in the customer number(KUNNR), Material/Article number(MATNR), sales order number, Delivery number, etc. There are multiple ways to achieve the same. So let’s begin and dig into it.

  • Using Conversion Exit Function Module:
REPORT ztest_conv_exit.

DATA: lv_orderno TYPE char10 VALUE 875545.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
  EXPORTING
    input  = lv_orderno
  IMPORTING
    output = lv_orderno.

WRITE lv_orderno.
Conversion Exit
  • Conversion Exit using the ABAP 7.4 Syntax
REPORT ztest_conv_exit.

DATA: lv_orderno TYPE char10 VALUE 875545.

lv_orderno = |{ lv_orderno ALPHA = IN }|.
WRITE lv_orderno.
  • Using the Variable Type N
REPORT ztest_conv_exit.

DATA: lv_orderno TYPE char10 VALUE 875545.
DATA: lv_orderno_new TYPE n LENGTH 10.

lv_orderno_new = lv_orderno.

WRITE lv_orderno_new.

Note: The number of leading zero is always dependent upon the assigned variable length. And above will work only for the numeric values, not the type having mixed char & integer.

Removing Leading Zero

Similarly for Removing Leading Zero below is the code for the same.

REPORT ztest_conv_exit.

DATA: lv_orderno TYPE char10 VALUE '0000012345'.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
  EXPORTING
    input  = lv_orderno
  IMPORTING
    output = lv_orderno.

WRITE lv_orderno.

The same can be achieved in the ABAP 7.4 more momentarily.

REPORT ztest_conv_exit.

DATA: lv_orderno TYPE char10 VALUE '0000012345'.

lv_orderno = |{ lv_orderno ALPHA = OUT }|.

WRITE lv_orderno.

Result: 12345

Additional Information

Let’s jump into the conversion Exit 😉. Conversion exit is also called the conversion routine which is a special type of five characters named function module like CONVERSION_EXIT_XXXXX_<TYPE>.

where Type holds the keyword of INPUT or OUTPUT.  INPUT represents converting external number to internal number, and OUTPUT means converting internal number to external number

These Conversion routines are assigned to the domain. Let’s take an example of MARA-MATNR

Routine in Domain
Domain in SE11

What does it means internal/external number 😏 – Well Internal number which used inside programming like variable comparison, Select statement where clause, etc. for e.g 

SE16N Mara Table
Material view in SE16N transaction
conversion exit in abap
During Debugging if we fetch the matnr(’10’) using select query then it will fail

If we go for select query with material 10 then it won’t find any record as in the database maternal number is stored in internal number format. So before that, the format which is in the external format like 10 here, should be converted to internal format

Conversion exit matn1 input

Interestingly if we are using the value from the input selection screen and the screen is attached with data element(domain) having conversion routine then it automatically does the job of conversion. Let’s take one example – in the above, we are forcefully converting the number to an internal number. but let’s takes material from the input screen

Selection screen auto conversion exit
Selection screen in SE38 Tcode Execution
Conversion exit in debugging

If you want to test how conversion happened in case of selection screen input – go-ahead set breakpoint on the Function Module CONVERSION_EXIT_MATN1_INPUT.😋

You can create your custom conversion exit also, Try it…

Hmm, Hopefully, it clear the concept of Conversion exit/routine. Comment below if you feel anything needs to be taken care of or any suggestion/question… 

Note:

SAP Link: https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-us/abenconversion_exits.htm

Check out our OData series : OData – ABAP Skill


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *