Technical Articles
ABAP 7.4- Use of VALUE & FOR statements instead of LOOP also to avoid using Conversion Exit Alpha FM
The ABAPers journey is incomplete without where we need to process Excel file data for further processing. In my experience, it is mostly for BAPI development i.e. getting the data from an Excel file and converting it to a suitable form.
Earlier I was using Loop, Endloop statements, and CONVERSION_EXIT_ALPHA_INPUT function module for the same. It was long and messy coding.
And now using ABAP7.4 using VALUE & FOR statements code started looking literally beautiful.
Below is a code snippet using ABAP 7.4
CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
EXPORTING
i_line_header = 'X'
i_tab_raw_data = gt_file
i_filename = p_file
TABLES
i_tab_converted_data = gt_data
EXCEPTIONS
conversion_failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
IF gt_data[] IS NOT INITIAL.
DATA(gt_final) = VALUE tt_final( FOR ls_final IN gt_data ( bukrs = ls_final-bukrs
ebeln = |{ ls_final-ebeln ALPHA = IN }|
bsart = ls_final-bsart
ernam = ls_final-ernam
aedat = ls_final-aedat
lifnr = |{ ls_final-lifnr ALPHA = IN }|
) ).
ENDIF.
The data declaration as per below.
TYPES: BEGIN OF gty_final,
bukrs TYPE bukrs,
ebeln TYPE ebeln,
bsart TYPE esart,
ernam TYPE ernam,
aedat TYPE erdat,
lifnr TYPE lifnr,
END OF gty_final,
tt_final TYPE STANDARD TABLE OF gty_final WITH DEFAULT KEY.
DATA: gt_file TYPE truxs_t_text_data,
gt_data TYPE STANDARD TABLE OF gty_final.
Actually, I am still learning ABAP 7.4 and OOPs concepts while coding.
Your inputs are welcome & if you have any blogs to add more, please feel free to comment.
Also, FYI this is my first blog, so if you find anything unusual please say it so I can improve myself.
Thanks,
Shrikant Patil
To be clear, using VALUE and FOR in this example has nothing to do with avoiding using the conversion FM. That part is handled by addition of ALPHA = IN in string template, which has been available before 7.4 (from 7.31, I think?). Here is just one of the blog posts about string templates (from 2013!). This could have been used in LOOP... ENDLOOP just as well.
Personally, I don't find VALUE and FOR syntax beautiful at all. Even in this short example, the code is so wide that a scroll bar has to be added. It's certainly efficient but I wouldn't be gushing about its appeal. But as they say, "beauty is in the eye of the beholder", so I guess it's a matter of opinion. 🙂
Hi Jelena,
Thank you for your valuable feedback. I really appreciate it and I am committed to using it to improve my blog writing in the future.