July 25, 2015

Convert data to specific type in ABAP

There are occasions where we need to make specific conversions in our data. That scenario happened to me with a log table whose data needed to be used to make summaries. There was a column of CHAR type which had the values to be used.

Fortunately this table also had the table name and the field name as fields and they happened to be very useful for my purposes.

I found a function module named 'RS_CONV_EX_2_IN' where you send as parameters the value to be converted, the referenced table name and referenced field name and the result is the value converted to the specified format. The only caveat here is that you have to specify the appropriate table and the field name as parameters.

Let's code ABAP.

Copy and paste the following code and run:

report  zconversion.

start-of-selection.
  perform test.

form test.
  data: oref      type ref to cx_root,
        lv_value  type char255,
        lv_result type datum,
        lv_kbetr  type bwert,
        lv_error  type string.

  try.
    lv_value = '01012015'.
    perform convert_to_date using lv_value changing lv_result.
    write: / 'Date:', lv_result.
  catch cx_root into oref.
    lv_error = oref->get_text( ).
    write / lv_error.
  endtry.

  try.
    lv_value = '1033.12'.
    perform convert_to_curr using lv_value changing lv_kbetr.
    write: / 'Value:', lv_kbetr.
  catch cx_root into oref.
    lv_error = oref->get_text( ).
    write / lv_error.
  endtry.
endform.

form convert_to_date using p_value type char255 changing r_value type datum.
  perform convert using p_value 'SYST' 'DATUM' changing r_value.
endform.

form convert_to_curr using p_value type char255 changing r_value type bwert.
  perform convert using p_value 'KONP' 'KBETR' changing r_value.
endform.

form convert using p_value type char255 p_tabname type tabname p_fieldname type fieldname changing r_value type data.
  data wa_tabfield type tabfield.

  check p_value is not initial.

  wa_tabfield-tabname    = p_tabname.
  wa_tabfield-fieldname  = p_fieldname.

  call function 'RS_CONV_EX_2_IN'
    exporting
      input_external  = p_value
      table_field     = wa_tabfield
*     currency                           =
    importing
      output_internal = r_value
    exceptions
      input_not_numerical                = 1
      too_many_decimals                  = 2
      more_than_one_sign                 = 3
      ill_thousand_separator_dist        = 4
      too_many_digits                    = 5
      sign_for_unsigned                  = 6
      too_large                          = 7
      too_small                          = 8
      invalid_date_format                = 9
      invalid_date                       = 10
      invalid_time_format                = 11
      invalid_time                       = 12
      invalid_hex_digit                  = 13
      unexpected_error                   = 14
      invalid_fieldname                  = 15
      field_and_descr_incompatible       = 16
      input_too_long                     = 17
      no_decimals                        = 18
      invalid_float                      = 19
      conversion_exit_error              = 20
      others                             = 21.

  if sy-subrc <> 0.
* message id sy-msgid type sy-msgty number sy-msgno
*         with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  endif.
endform.

The most important part of this code is the 'convert' form which makes all the conversion. In case the function fails then result is going to be the initial value of the variable. Check the SY-SUBRC value in the latter case.

Another important part of the code is 'r_value' parameter which is of  'data' value that is a generic type that will return the appropriate variable.

Hope it helps.

July 14, 2015

Text elements in ABAP

When programming in ABAP there are features that are intrinsical to SAP. One of those features are 'Text elements', which purpose is to be used as 'constants' or 'labels' for titles and messages and are defined according to the language of your session.

Text elements are everywhere in SAP, in fact every time you log in, there's a default language which indicates to SAP to load the corresponding titles in all the application. If you choose in your login to use another language then all the screens are translated to that language.

We, as abapers, can make use of those text elements but also can make our own. Tipically when we create a report or a module pool we define text elements to show titles and messages in the screens.

There are three types of text elements;
  1. List headings
  2. Selection texts
  3. Text symbols
Text symbols are out of the scope of this article however this type of text element si used to print a report in the screen when we are not using an ALV.

Selection texts are labels used for input. When we define a parameter, a select-options or checkboxes, etc.

Text symbols are used inside the program to set header for screeens or messages. This kind is the most used in ABAP.

Ok, let's code ABAP;

1. Let's create a report with transaction 'SE38' and paste the following code.

report  ztext_element.

tables mara.

data wa_mara type mara.

selection-screen: begin of block b1 with frame title text-001.
  select-options so_matnr for mara-matnr.
selection-screen: end of block b1.

start-of-selection.

select * into wa_mara from mara up to 10 rows where matnr in so_matnr.
  write / wa_mara-matnr.
endselect.

if sy-subrc ne 0.
  write / 'No data'(002).
endif.

2. Ok, let make a pause here. There are two text symbols in the code above. The first one is in the selection screen and it's name is 'text-001' and the second is inside de if statetement at the bottom and is identified as '(002)'.

We have also a 'SELECT-OPTIONS' which is used for inputs to be used later. However we can set 'Selection texts' to it. This feature also work with 'PARAMETER'.

Activate and run the program.

You'll see the following screen.


3. Return to the source code and go to the menu Goto / Text elements / Text symbols.


4. Type the following data in the grid.


5. In the same screen click on the 'Selection texts' tab. You'll see the following:


6. Click on the 'Dictionary' check and press enter and automatically the word 'Material' is shown because you set the check to extract the text from 'Dictionary'. If you don't want to set that check, you have the freedom to type your own name. At the same time a message at the bottom will show the operation of getting the name from the dictionary.



7. Activate the text elements and then return to source code.

8. Activate the program and then run. Check the image.


Now the box has the name 'Parameters' and instead of 'SO_MATNR' we have the name 'Material' for the SELECT-OPTION.

9. In the same page execute the program or press 'F8' and check the results.

Finally, I want to write about the following code:

write / 'No data'(002).

How does this works? Well, actually that's also a text symbol, however what this means is that unless the text symbol '002' is defined then the program in this case will print 'No data'. If the text symbol '002' is defined then the program will the value of text-002.

This gives you the ability to have a default value in your program and a flexible way to work with different languages in SAP.

Well, that's it for now.

Hope it helps.