Introduction:
Hello Everyone!!
When working on real projects, I often had to export ALV data manually every time someone needed it for an email. It was time-consuming and repetitive. So I created a small ABAP utility that automatically converts the ALV output into a TXT, CSV, or XLSX file and sends it directly by email. This makes the whole process much faster and easier for both users and developers.
Manually exporting the report every time is both repetitive and error-prone.
So I created a simple yet powerful ABAP program that automates this entire process.
With one execution, the program:
✔ Fetches Sales Order data from VBAK
✔ Displays the results in a Factory Method (CL_SALV_TABLE)
✔ Converts the data into TXT, CSV, or XLSX
✔ Attaches the file
✔ Emails it automatically using CL_BCS
How the Program Works
Data Retrieval – Sales order header data (VBAK) is read based on the input range.
ALV Display – The results are shown using CL_SALV_TABLE.
File Preparation
TXT: Fixed-width formatting
CSV: Comma-separated lines
XLSX: Using SALV → XML export
Binary Conversion – Converts string or XML to SOLIX_TAB for email attachment.
Email Construction – Creates a BCS email object with subject, body, and attachment.
Sending – Dispatches the email instantly.
This approach is modular, clean, and easily extendable to other tables, formats, or business requirements.
PROGRAM FLOW
+——————————————————+
| START OF SELECTION |
+—————————–+————————+
|
v
+————————+
| Fetch Data (VBAK) |
+————————+
|
v
+————————+
| Display ALV Output |
| (CL_SALV_TABLE) |
+————————+
|
v
+————————————————+
| User selects output format (TXT/CSV/XLSX) |
+————————————————+
|
v
+————————————————————-+
| Generate content: |
| – TXT → String formatting |
| – CSV → CSV concatenation |
| – XLSX → ALV->TO_XML (SALV XML Export) |
+————————————————————-+
|
v
+————————————————————-+
| Convert content to binary attachment: |
| – SCMS_STRING_TO_XSTRING (if TXT/CSV) |
| – SCMS_XSTRING_TO_BINARY (all formats) |
+————————————————————-+
|
v
+————————————————————-+
| Build Email (BCS Framework): |
| – CREATE_PERSISTENT |
| – CREATE_DOCUMENT |
| – ADD_ATTACHMENT |
| – SET_DOCUMENT |
| – CREATE_SENDER / CREATE_INTERNET_ADDRESS |
| – ADD_RECIPIENT |
+————————————————————-+
|
v
+—————————-+
| SEND EMAIL |
| (LO_SEND->SEND) |
+—————————-+
|
v
+—————————-+
| Email Sent Successfully |
+—————————-+
Function Modules & Classes Used in the Program
Function ModulePurpose / UsageSCMS_STRING_TO_XSTRINGConverts plain text (string) into XSTRING format. Used for TXT and CSV attachments.SCMS_XSTRING_TO_BINARYConverts XSTRING into SOLIX_TAB (binary table). Required for sending attachments (TXT, CSV, XLSX).
Class / MethodPurpose / UsageCL_SALV_TABLE=>FACTORYCreates ALV object instance for display.LO_ALV->DISPLAYDisplays the ALV grid on the screen.LO_ALV->TO_XMLConverts ALV data into XML. Used for generating XLSX output.
Class / MethodPurpose / UsageCL_BCS=>CREATE_PERSISTENTInitializes an email send request.CL_DOCUMENT_BCS=>CREATE_DOCUMENTCreates email body and subject.LO_DOCUMENT->ADD_ATTACHMENTAdds attachment (TXT/CSV/XLSX) to email.LO_SEND_REQUEST->SET_DOCUMENTAttaches the document to the send request.CL_SAPUSER_BCS=>CREATEDefines email sender (current SAP user).CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESSCreates external email recipient.LO_SEND_REQUEST->ADD_RECIPIENTAdds a recipient to the email request.LO_SEND_REQUEST->SENDSends the final email.
SOURCE CODE FOR REFERENCE :
*&———————————————————————*
*& Report ZEMAIL_ATTACH
*&———————————————————————*
*&
*&———————————————————————*
REPORT zemail_attach.
TABLES:vbak.
*————Selection screen parameters——————————-*
SELECT-OPTIONS: s_vbeln FOR vbak-vbeln.
PARAMETERS: p_txt RADIOBUTTON GROUP rg1 DEFAULT ‘X’,
p_csv RADIOBUTTON GROUP rg1,
p_xlsx RADIOBUTTON GROUP rg1.
PARAMETERS: p_email TYPE adr6-smtp_addr.
*————-Start-of-selection—————————————*
START-OF-SELECTION.
*Fetch sales order header data from VBAK
SELECT vbeln,
erdat,
vbtyp,
auart,
vkorg,
kunnr FROM vbak
INTO TABLE (lt_vbak)
WHERE vbeln IN @s_vbeln.
SORT lt_vbak BY vbeln.
*————-Display ALV———————————————-*
DATA: lo_alv TYPE REF TO cl_salv_table.
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = lo_alv
CHANGING
t_table = lt_vbak ).
lo_alv->display( ).
CATCH cx_salv_msg INTO DATA(lx_msg).
MESSAGE lx_msg->get_text( ) TYPE ‘E’.
ENDTRY.
*————-TXT Format———————————————-*
IF p_txt =’X’.
DATA(lv_txt) = |{ ‘VBELN’ WIDTH = 12 }| &&
|{ ‘ERDAT’ WIDTH = 10 }| &&
|{ ‘VBTYP’ WIDTH = 6 } | &&
|{ ‘AUART’ WIDTH = 6 } | &&
|{ ‘VKORG’ WIDTH = 6 } | &&
|{ ‘KUNNR’ WIDTH = 12 }| &&
cl_abap_char_utilities=>cr_lf.
LOOP AT lt_vbak ASSIGNING FIELD-SYMBOL(<fs_row>).
lv_txt = lv_txt &&
|{ <fs_row>-vbeln WIDTH = 12 } | &&
|{ <fs_row>-erdat WIDTH = 10 } | &&
|{ <fs_row>-vbtyp WIDTH = 6 } | &&
|{ <fs_row>-auart WIDTH = 6 } | &&
|{ <fs_row>-vkorg WIDTH = 6 } | &&
|{ <fs_row>-kunnr WIDTH = 12 } | &&
cl_abap_char_utilities=>cr_lf.
ENDLOOP.
*————-CSV Format————————————————*
ENDIF.
IF p_csv = ‘X’.
DATA(lv_csv) = |VBELN,ERDAT,VBTYP,AUART,VKORG,KUNNR| &&
cl_abap_char_utilities=>cr_lf.
LOOP AT lt_vbak ASSIGNING <fs_row>.
lv_csv = lv_csv &&
|{ <fs_row>-vbeln },{ <fs_row>-erdat },{ <fs_row>-vbtyp },| &&
|{ <fs_row>-auart },{ <fs_row>-vkorg },{ <fs_row>-kunnr } | &&
cl_abap_char_utilities=>cr_lf.
ENDLOOP.
ENDIF.
*————-XLSX Format————————————————*
IF p_xlsx = ‘X’.
DATA(lv_xml) = lo_alv->to_xml( xml_type = if_salv_bs_xml=>c_type_xlsx ).
ENDIF.
*————-Convert content to binary for attachment——————-*
DATA: lv_xstring TYPE xstring,
lt_bin TYPE solix_tab,
lv_size TYPE i,
lv_filename TYPE so_obj_des.
IF p_txt = ‘X’.
lv_filename = ‘report.txt’.
CALL FUNCTION ‘SCMS_STRING_TO_XSTRING’
EXPORTING
text = lv_txt
IMPORTING
buffer = lv_xstring.
CALL FUNCTION ‘SCMS_XSTRING_TO_BINARY’
EXPORTING
buffer = lv_xstring
TABLES
binary_tab = lt_bin.
ELSEIF p_csv = ‘X’.
lv_filename = ‘report.csv’.
CALL FUNCTION ‘SCMS_STRING_TO_XSTRING’
EXPORTING text = lv_csv
IMPORTING buffer = lv_xstring.
CALL FUNCTION ‘SCMS_XSTRING_TO_BINARY’
EXPORTING buffer = lv_xstring
TABLES binary_tab = lt_bin.
ELSEIF p_xlsx = ‘X’.
lv_filename = ‘report.xlsx’.
CALL FUNCTION ‘SCMS_XSTRING_TO_BINARY’
EXPORTING
buffer = lv_xml
IMPORTING
output_length = lv_size
TABLES
binary_tab = lt_bin.
ENDIF.
*————— Create and send email————————————*
DATA: lo_send_request TYPE REF TO cl_bcs,
lo_document TYPE REF TO cl_document_bcs,
lo_sender TYPE REF TO cl_sapuser_bcs,
lo_recipient TYPE REF TO if_recipient_bcs,
lv_subject TYPE so_obj_des.
*Create send request
lo_send_request = cl_bcs=>create_persistent( ).
*Subject line
lv_subject = ‘Sales Order Report’.
*Create email body text
DATA(lt_body) = VALUE soli_tab( ( line = ‘Sales Order Report Attached.’ ) ).
* Create document with attachment
lo_document = cl_document_bcs=>create_document(
i_type = ‘RAW’
i_text = lt_body
i_subject = lv_subject ).
lo_document->add_attachment(
i_attachment_type = ‘BIN’
i_attachment_subject = lv_filename
i_attachment_language = sy-langu
i_att_content_hex = lt_bin ).
*Add document to send request
lo_send_request->set_document( lo_document ).
*Set sender
lo_sender = cl_sapuser_bcs=>create( sy-uname ).
lo_send_request->set_sender( lo_sender ).
*Add recipient
lo_recipient = cl_cam_address_bcs=>create_internet_address( p_email ).
lo_send_request->add_recipient(
i_recipient = lo_recipient
i_express = ‘X’ ).
* Send email
TRY.
lo_send_request->send( i_with_error_screen = ‘X’ ).
COMMIT WORK.
MESSAGE ‘Email sent successfully’ TYPE ‘S’.
CATCH cx_bcs INTO DATA(lx_bcs).
MESSAGE lx_bcs->get_text( ) TYPE ‘E’.
ENDTRY.
These images will help you better understand how the program behaves during execution.
You can check the mail delivery status in transaction SOST.
Conclusion:
Automating ALV export and email delivery may seem like a small enhancement, but it brings significant value to daily business operations. Instead of manually downloading and sending files each time a user requests data, this ABAP utility streamlines the entire workflow—fetching data, generating TXT/CSV/XLSX files, and sending them by email in one seamless execution.
Introduction:Hello Everyone!!When working on real projects, I often had to export ALV data manually every time someone needed it for an email. It was time-consuming and repetitive. So I created a small ABAP utility that automatically converts the ALV output into a TXT, CSV, or XLSX file and sends it directly by email. This makes the whole process much faster and easier for both users and developers.Manually exporting the report every time is both repetitive and error-prone.So I created a simple yet powerful ABAP program that automates this entire process. With one execution, the program: ✔ Fetches Sales Order data from VBAK ✔ Displays the results in a Factory Method (CL_SALV_TABLE) ✔ Converts the data into TXT, CSV, or XLSX ✔ Attaches the file ✔ Emails it automatically using CL_BCSHow the Program WorksData Retrieval – Sales order header data (VBAK) is read based on the input range.ALV Display – The results are shown using CL_SALV_TABLE.File PreparationTXT: Fixed-width formattingCSV: Comma-separated linesXLSX: Using SALV → XML exportBinary Conversion – Converts string or XML to SOLIX_TAB for email attachment.Email Construction – Creates a BCS email object with subject, body, and attachment.Sending – Dispatches the email instantly.This approach is modular, clean, and easily extendable to other tables, formats, or business requirements.PROGRAM FLOW +——————————————————+
| START OF SELECTION |
+—————————–+————————+
|
v
+————————+
| Fetch Data (VBAK) |
+————————+
|
v
+————————+
| Display ALV Output |
| (CL_SALV_TABLE) |
+————————+
|
v
+————————————————+
| User selects output format (TXT/CSV/XLSX) |
+————————————————+
|
v
+————————————————————-+
| Generate content: |
| – TXT → String formatting |
| – CSV → CSV concatenation |
| – XLSX → ALV->TO_XML (SALV XML Export) |
+————————————————————-+
|
v
+————————————————————-+
| Convert content to binary attachment: |
| – SCMS_STRING_TO_XSTRING (if TXT/CSV) |
| – SCMS_XSTRING_TO_BINARY (all formats) |
+————————————————————-+
|
v
+————————————————————-+
| Build Email (BCS Framework): |
| – CREATE_PERSISTENT |
| – CREATE_DOCUMENT |
| – ADD_ATTACHMENT |
| – SET_DOCUMENT |
| – CREATE_SENDER / CREATE_INTERNET_ADDRESS |
| – ADD_RECIPIENT |
+————————————————————-+
|
v
+—————————-+
| SEND EMAIL |
| (LO_SEND->SEND) |
+—————————-+
|
v
+—————————-+
| Email Sent Successfully |
+—————————-+Function Modules & Classes Used in the ProgramFunction ModulePurpose / UsageSCMS_STRING_TO_XSTRINGConverts plain text (string) into XSTRING format. Used for TXT and CSV attachments.SCMS_XSTRING_TO_BINARYConverts XSTRING into SOLIX_TAB (binary table). Required for sending attachments (TXT, CSV, XLSX). Class / MethodPurpose / UsageCL_SALV_TABLE=>FACTORYCreates ALV object instance for display.LO_ALV->DISPLAYDisplays the ALV grid on the screen.LO_ALV->TO_XMLConverts ALV data into XML. Used for generating XLSX output. Class / MethodPurpose / UsageCL_BCS=>CREATE_PERSISTENTInitializes an email send request.CL_DOCUMENT_BCS=>CREATE_DOCUMENTCreates email body and subject.LO_DOCUMENT->ADD_ATTACHMENTAdds attachment (TXT/CSV/XLSX) to email.LO_SEND_REQUEST->SET_DOCUMENTAttaches the document to the send request.CL_SAPUSER_BCS=>CREATEDefines email sender (current SAP user).CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESSCreates external email recipient.LO_SEND_REQUEST->ADD_RECIPIENTAdds a recipient to the email request.LO_SEND_REQUEST->SENDSends the final email.SOURCE CODE FOR REFERENCE :*&———————————————————————*
*& Report ZEMAIL_ATTACH
*&———————————————————————*
*&
*&———————————————————————*
REPORT zemail_attach.
TABLES:vbak.
*————Selection screen parameters——————————-*
SELECT-OPTIONS: s_vbeln FOR vbak-vbeln.
PARAMETERS: p_txt RADIOBUTTON GROUP rg1 DEFAULT ‘X’,
p_csv RADIOBUTTON GROUP rg1,
p_xlsx RADIOBUTTON GROUP rg1.
PARAMETERS: p_email TYPE adr6-smtp_addr.
*————-Start-of-selection—————————————*
START-OF-SELECTION.
*Fetch sales order header data from VBAK
SELECT vbeln,
erdat,
vbtyp,
auart,
vkorg,
kunnr FROM vbak
INTO TABLE (lt_vbak)
WHERE vbeln IN @s_vbeln.
SORT lt_vbak BY vbeln.
*————-Display ALV———————————————-*
DATA: lo_alv TYPE REF TO cl_salv_table.
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = lo_alv
CHANGING
t_table = lt_vbak ).
lo_alv->display( ).
CATCH cx_salv_msg INTO DATA(lx_msg).
MESSAGE lx_msg->get_text( ) TYPE ‘E’.
ENDTRY.
*————-TXT Format———————————————-*
IF p_txt =’X’.
DATA(lv_txt) = |{ ‘VBELN’ WIDTH = 12 }| &&
|{ ‘ERDAT’ WIDTH = 10 }| &&
|{ ‘VBTYP’ WIDTH = 6 } | &&
|{ ‘AUART’ WIDTH = 6 } | &&
|{ ‘VKORG’ WIDTH = 6 } | &&
|{ ‘KUNNR’ WIDTH = 12 }| &&
cl_abap_char_utilities=>cr_lf.
LOOP AT lt_vbak ASSIGNING FIELD-SYMBOL(<fs_row>).
lv_txt = lv_txt &&
|{ <fs_row>-vbeln WIDTH = 12 } | &&
|{ <fs_row>-erdat WIDTH = 10 } | &&
|{ <fs_row>-vbtyp WIDTH = 6 } | &&
|{ <fs_row>-auart WIDTH = 6 } | &&
|{ <fs_row>-vkorg WIDTH = 6 } | &&
|{ <fs_row>-kunnr WIDTH = 12 } | &&
cl_abap_char_utilities=>cr_lf.
ENDLOOP.
*————-CSV Format————————————————*
ENDIF.
IF p_csv = ‘X’.
DATA(lv_csv) = |VBELN,ERDAT,VBTYP,AUART,VKORG,KUNNR| &&
cl_abap_char_utilities=>cr_lf.
LOOP AT lt_vbak ASSIGNING <fs_row>.
lv_csv = lv_csv &&
|{ <fs_row>-vbeln },{ <fs_row>-erdat },{ <fs_row>-vbtyp },| &&
|{ <fs_row>-auart },{ <fs_row>-vkorg },{ <fs_row>-kunnr } | &&
cl_abap_char_utilities=>cr_lf.
ENDLOOP.
ENDIF.
*————-XLSX Format————————————————*
IF p_xlsx = ‘X’.
DATA(lv_xml) = lo_alv->to_xml( xml_type = if_salv_bs_xml=>c_type_xlsx ).
ENDIF.
*————-Convert content to binary for attachment——————-*
DATA: lv_xstring TYPE xstring,
lt_bin TYPE solix_tab,
lv_size TYPE i,
lv_filename TYPE so_obj_des.
IF p_txt = ‘X’.
lv_filename = ‘report.txt’.
CALL FUNCTION ‘SCMS_STRING_TO_XSTRING’
EXPORTING
text = lv_txt
IMPORTING
buffer = lv_xstring.
CALL FUNCTION ‘SCMS_XSTRING_TO_BINARY’
EXPORTING
buffer = lv_xstring
TABLES
binary_tab = lt_bin.
ELSEIF p_csv = ‘X’.
lv_filename = ‘report.csv’.
CALL FUNCTION ‘SCMS_STRING_TO_XSTRING’
EXPORTING text = lv_csv
IMPORTING buffer = lv_xstring.
CALL FUNCTION ‘SCMS_XSTRING_TO_BINARY’
EXPORTING buffer = lv_xstring
TABLES binary_tab = lt_bin.
ELSEIF p_xlsx = ‘X’.
lv_filename = ‘report.xlsx’.
CALL FUNCTION ‘SCMS_XSTRING_TO_BINARY’
EXPORTING
buffer = lv_xml
IMPORTING
output_length = lv_size
TABLES
binary_tab = lt_bin.
ENDIF.
*————— Create and send email————————————*
DATA: lo_send_request TYPE REF TO cl_bcs,
lo_document TYPE REF TO cl_document_bcs,
lo_sender TYPE REF TO cl_sapuser_bcs,
lo_recipient TYPE REF TO if_recipient_bcs,
lv_subject TYPE so_obj_des.
*Create send request
lo_send_request = cl_bcs=>create_persistent( ).
*Subject line
lv_subject = ‘Sales Order Report’.
*Create email body text
DATA(lt_body) = VALUE soli_tab( ( line = ‘Sales Order Report Attached.’ ) ).
* Create document with attachment
lo_document = cl_document_bcs=>create_document(
i_type = ‘RAW’
i_text = lt_body
i_subject = lv_subject ).
lo_document->add_attachment(
i_attachment_type = ‘BIN’
i_attachment_subject = lv_filename
i_attachment_language = sy-langu
i_att_content_hex = lt_bin ).
*Add document to send request
lo_send_request->set_document( lo_document ).
*Set sender
lo_sender = cl_sapuser_bcs=>create( sy-uname ).
lo_send_request->set_sender( lo_sender ).
*Add recipient
lo_recipient = cl_cam_address_bcs=>create_internet_address( p_email ).
lo_send_request->add_recipient(
i_recipient = lo_recipient
i_express = ‘X’ ).
* Send email
TRY.
lo_send_request->send( i_with_error_screen = ‘X’ ).
COMMIT WORK.
MESSAGE ‘Email sent successfully’ TYPE ‘S’.
CATCH cx_bcs INTO DATA(lx_bcs).
MESSAGE lx_bcs->get_text( ) TYPE ‘E’.
ENDTRY.These images will help you better understand how the program behaves during execution.You can check the mail delivery status in transaction SOST.Conclusion:Automating ALV export and email delivery may seem like a small enhancement, but it brings significant value to daily business operations. Instead of manually downloading and sending files each time a user requests data, this ABAP utility streamlines the entire workflow—fetching data, generating TXT/CSV/XLSX files, and sending them by email in one seamless execution. Read More Technology Blog Posts by Members articles
#SAP
#SAPTechnologyblog