ABAP SALV Table with Checkbox Column, Select/Deselect All, and Email Export Functionality

Estimated read time 12 min read

Hi all,

I have developed an ABAP report that displays employee data in a SALV table with a checkbox column. Users can select/deselect rows using custom toolbar buttons, and send the selected data as an Excel attachment via email.

The SALV table includes clickable checkboxes that toggle selection, and I handle user commands for selecting all, deselecting all, and sending email.

Please review the code below. Any suggestions on improving performance, best practices, or adding additional features would be appreciated!

Thank you!
Tcode :SE41

Create a PF Status

REPORT zsalv_factory_disply_checkbox.

” Type definition for internal table representing employee data with a checkbox
TYPES: BEGIN OF t_zemployee,
checkbox TYPE c LENGTH 1, ” Checkbox for selection
employee_id TYPE zemploye-employee_id, ” Employee ID
company_code TYPE zemploye-company_code, ” Company code
bankid TYPE zemploye-bankid, ” Bank ID
insurance_id TYPE zemploye-insurance_id, ” Insurance ID
empname TYPE zemploye-empname, ” Employee Name
remarks TYPE zemploye-remarks, ” Remarks
date_of_join TYPE zemploye-date_of_join, ” Date of Joining
date_of_resign TYPE zemploye-date_of_resign, ” Date of Resignation
salary TYPE zemploye-salary, ” Salary
currency TYPE zemploye-currency, ” Currency
commission TYPE zemploye-commission, ” Commission
currency23 TYPE zemploye-currency23, ” Additional Currency
END OF t_zemployee.

” Global internal tables to hold employee data and selected records
DATA: gt_data TYPE STANDARD TABLE OF t_zemployee,
seleted TYPE STANDARD TABLE OF t_zemployee,
gv_repid TYPE sy-repid VALUE sy-repid.

CLASS lcl_app DEFINITION.
PUBLIC SECTION.
CLASS-METHODS:
get_data, ” Fetch data from DB into internal table
display_data, ” Display SALV table with checkbox
refresh_display, ” Refresh the SALV table display
send_email, ” Send selected data as email attachment
convert_itab_to_xls_xstring ” Convert internal table to XLS XSTRING
RETURNING VALUE(rv_xstring) TYPE xstring,
on_link_click FOR EVENT link_click OF cl_salv_events_table
IMPORTING row column, ” Event handler for checkbox click
handle_user_command FOR EVENT added_function OF cl_salv_events_table
IMPORTING e_salv_function. ” Handler for custom toolbar functions

PRIVATE SECTION.
CLASS-DATA: gr_table TYPE REF TO cl_salv_table. ” Reference to SALV table object
ENDCLASS.

CLASS lcl_app IMPLEMENTATION.

” Method to fetch employee data from database and initialize checkbox field
METHOD get_data.
IF gt_data IS INITIAL.
SELECT * FROM zemploye INTO CORRESPONDING FIELDS OF TABLE gt_data.
LOOP AT gt_data ASSIGNING FIELD-SYMBOL(<fs>).
<fs>-checkbox = ”. ” Initialize checkbox as empty (unchecked)
ENDLOOP.
ENDIF.
ENDMETHOD.

” Method to display the SALV table with checkbox and event handlers
METHOD display_data.
lcl_app=>get_data( ). ” Load data

IF gr_table IS INITIAL.
TRY.
cl_salv_table=>factory(
IMPORTING r_salv_table = gr_table
CHANGING t_table = gt_data ).
CATCH cx_salv_msg INTO DATA(lx).
MESSAGE lx->get_text( ) TYPE ‘E’.
RETURN.
ENDTRY.

” Enable all standard functions in SALV toolbar
gr_table->get_functions( )->set_all( ).

TRY.
” Set custom GUI status with custom functions (PF-STATUS ‘ZTOOLBAR’)
gr_table->set_screen_status(
EXPORTING
report = gv_repid
pfstatus = ‘ZTOOLBAR’
set_functions = gr_table->c_functions_all ).
CATCH cx_salv_msg.
ENDTRY.

” Set checkbox column as hotspot to allow click events
DATA lr_columns TYPE REF TO cl_salv_columns_table.
DATA lr_column TYPE REF TO cl_salv_column_table.
lr_columns = gr_table->get_columns( ).
lr_columns->set_optimize( abap_true ).

TRY.
lr_column ?= lr_columns->get_column( ‘CHECKBOX’ ).
lr_column->set_cell_type( if_salv_c_cell_type=>checkbox_hotspot ).
lr_column->set_short_text( ‘Select’ ).
CATCH cx_salv_not_found.
ENDTRY.

” Register event handlers for checkbox clicks and user commands
DATA lo_events TYPE REF TO cl_salv_events_table.
lo_events = gr_table->get_event( ).
SET HANDLER lcl_app=>on_link_click FOR lo_events.
SET HANDLER lcl_app=>handle_user_command FOR lo_events.

ELSE.
” Refresh SALV table if already created
gr_table->refresh( ).
ENDIF.

gr_table->display( ).
ENDMETHOD.

” Refresh display wrapper method
METHOD refresh_display.
lcl_app=>display_data( ).
ENDMETHOD.

” Event handler to toggle checkbox on clicking checkbox cell
METHOD on_link_click.
FIELD-SYMBOLS: <fs> TYPE t_zemployee.
READ TABLE gt_data ASSIGNING <fs> INDEX row.
IF sy-subrc = 0.
IF <fs>-checkbox = ‘X’.
<fs>-checkbox = ”.
ELSE.
<fs>-checkbox = ‘X’.
ENDIF.
ENDIF.
lcl_app=>refresh_display( ).
ENDMETHOD.

” Convert selected internal table data to XLS XSTRING format for attachment
METHOD convert_itab_to_xls_xstring.
TRY.
cl_salv_table=>factory(
EXPORTING
list_display = abap_false
IMPORTING
r_salv_table = DATA(lo_salv_table)
CHANGING
t_table = seleted ).
CATCH cx_salv_msg INTO DATA(lx_mail).
MESSAGE lx_mail TYPE ‘E’.
ENDTRY.

rv_xstring = lo_salv_table->to_xml( xml_type = if_salv_bs_xml=>c_type_xlsx ).
ENDMETHOD.

” Send email with the selected employee data as an Excel attachment
METHOD send_email.
TRY.
” Create persistent email object
DATA(lo_mail) = cl_bcs=>create_persistent( ).

” Sender email address
DATA(lo_sender_address) = cl_cam_address_bcs=>create_internet_address(
i_address_string = ‘maniyadav2585@gmail.com’ ).
lo_mail->set_sender( lo_sender_address ).

” Recipient email address
DATA(recipient_address) = cl_cam_address_bcs=>create_internet_address(
i_address_string = ‘maniyadav2585@gmail.com’ ).
lo_mail->add_recipient( i_recipient = recipient_address ).

” Create email document with subject and body text
DATA(lo_document) = cl_document_bcs=>create_document(
i_type = ‘htm’
i_subject = `Excel Attachment`
i_text = VALUE #( ( CONV #( ‘Excel Attachment’ ) ) )
i_sender = lo_sender_address ).

” Convert selected data to XLS format for attachment
DATA(lv_excel_data) = convert_itab_to_xls_xstring( ).

” Add the Excel attachment
lo_document->add_attachment(
i_attachment_type = ‘XLS’
i_attachment_subject = ‘Employee Information’
i_attachment_size = xstrlen( lv_excel_data )
i_att_content_hex = cl_bcs_convert=>xstring_to_solix( lv_excel_data ) ).

lo_mail->set_document( lo_document ).
lo_mail->set_send_immediately( abap_true ).

” Send the email
DATA(lv_send_success) = lo_mail->send( ).
COMMIT WORK.

CATCH cx_send_req_bcs cx_address_bcs cx_document_bcs INTO DATA(lx_mail).
MESSAGE lx_mail TYPE ‘E’.
ENDTRY.
ENDMETHOD.

” Handle custom toolbar commands (Select All, Deselect All, Send, Back)
METHOD handle_user_command.
FIELD-SYMBOLS: <fs> TYPE t_zemployee.
DATA lv_count TYPE i VALUE 0.

CASE e_salv_function.
WHEN ‘SELALL’.
LOOP AT gt_data ASSIGNING <fs>.
<fs>-checkbox = ‘X’.
ENDLOOP.
lcl_app=>refresh_display( ).

WHEN ‘DESALL’.
LOOP AT gt_data ASSIGNING <fs>.
<fs>-checkbox = ”.
ENDLOOP.
lcl_app=>refresh_display( ).

WHEN ‘SEND’.
” Collect selected records for sending
LOOP AT gt_data ASSIGNING <fs> WHERE checkbox = ‘X’.
lv_count = lv_count + 1.
APPEND <fs> TO seleted.
ENDLOOP.

IF lv_count = 0.
MESSAGE ‘Please select at least one record before sending.’ TYPE ‘E’.
RETURN.
ENDIF.

lcl_app=>convert_itab_to_xls_xstring( ).
lcl_app=>send_email( ).

IF sy-subrc = 0.
MESSAGE |{ lv_count } row(s) selected and sent.| TYPE ‘S’.
ENDIF.

WHEN ‘BACK’.
LEAVE TO SCREEN 0.
ENDCASE.
ENDMETHOD.

ENDCLASS.

” Program starts here
START-OF-SELECTION.
SET PF-STATUS ‘ZTOOLBAR’. ” Set custom toolbar with your custom functions
lcl_app=>display_data( ).

 

 

OutPut:

 

 

 

​ Hi all,I have developed an ABAP report that displays employee data in a SALV table with a checkbox column. Users can select/deselect rows using custom toolbar buttons, and send the selected data as an Excel attachment via email.The SALV table includes clickable checkboxes that toggle selection, and I handle user commands for selecting all, deselecting all, and sending email.Please review the code below. Any suggestions on improving performance, best practices, or adding additional features would be appreciated!Thank you!Tcode :SE41Create a PF StatusREPORT zsalv_factory_disply_checkbox.

” Type definition for internal table representing employee data with a checkbox
TYPES: BEGIN OF t_zemployee,
checkbox TYPE c LENGTH 1, ” Checkbox for selection
employee_id TYPE zemploye-employee_id, ” Employee ID
company_code TYPE zemploye-company_code, ” Company code
bankid TYPE zemploye-bankid, ” Bank ID
insurance_id TYPE zemploye-insurance_id, ” Insurance ID
empname TYPE zemploye-empname, ” Employee Name
remarks TYPE zemploye-remarks, ” Remarks
date_of_join TYPE zemploye-date_of_join, ” Date of Joining
date_of_resign TYPE zemploye-date_of_resign, ” Date of Resignation
salary TYPE zemploye-salary, ” Salary
currency TYPE zemploye-currency, ” Currency
commission TYPE zemploye-commission, ” Commission
currency23 TYPE zemploye-currency23, ” Additional Currency
END OF t_zemployee.

” Global internal tables to hold employee data and selected records
DATA: gt_data TYPE STANDARD TABLE OF t_zemployee,
seleted TYPE STANDARD TABLE OF t_zemployee,
gv_repid TYPE sy-repid VALUE sy-repid.

CLASS lcl_app DEFINITION.
PUBLIC SECTION.
CLASS-METHODS:
get_data, ” Fetch data from DB into internal table
display_data, ” Display SALV table with checkbox
refresh_display, ” Refresh the SALV table display
send_email, ” Send selected data as email attachment
convert_itab_to_xls_xstring ” Convert internal table to XLS XSTRING
RETURNING VALUE(rv_xstring) TYPE xstring,
on_link_click FOR EVENT link_click OF cl_salv_events_table
IMPORTING row column, ” Event handler for checkbox click
handle_user_command FOR EVENT added_function OF cl_salv_events_table
IMPORTING e_salv_function. ” Handler for custom toolbar functions

PRIVATE SECTION.
CLASS-DATA: gr_table TYPE REF TO cl_salv_table. ” Reference to SALV table object
ENDCLASS.

CLASS lcl_app IMPLEMENTATION.

” Method to fetch employee data from database and initialize checkbox field
METHOD get_data.
IF gt_data IS INITIAL.
SELECT * FROM zemploye INTO CORRESPONDING FIELDS OF TABLE gt_data.
LOOP AT gt_data ASSIGNING FIELD-SYMBOL(<fs>).
<fs>-checkbox = ”. ” Initialize checkbox as empty (unchecked)
ENDLOOP.
ENDIF.
ENDMETHOD.

” Method to display the SALV table with checkbox and event handlers
METHOD display_data.
lcl_app=>get_data( ). ” Load data

IF gr_table IS INITIAL.
TRY.
cl_salv_table=>factory(
IMPORTING r_salv_table = gr_table
CHANGING t_table = gt_data ).
CATCH cx_salv_msg INTO DATA(lx).
MESSAGE lx->get_text( ) TYPE ‘E’.
RETURN.
ENDTRY.

” Enable all standard functions in SALV toolbar
gr_table->get_functions( )->set_all( ).

TRY.
” Set custom GUI status with custom functions (PF-STATUS ‘ZTOOLBAR’)
gr_table->set_screen_status(
EXPORTING
report = gv_repid
pfstatus = ‘ZTOOLBAR’
set_functions = gr_table->c_functions_all ).
CATCH cx_salv_msg.
ENDTRY.

” Set checkbox column as hotspot to allow click events
DATA lr_columns TYPE REF TO cl_salv_columns_table.
DATA lr_column TYPE REF TO cl_salv_column_table.
lr_columns = gr_table->get_columns( ).
lr_columns->set_optimize( abap_true ).

TRY.
lr_column ?= lr_columns->get_column( ‘CHECKBOX’ ).
lr_column->set_cell_type( if_salv_c_cell_type=>checkbox_hotspot ).
lr_column->set_short_text( ‘Select’ ).
CATCH cx_salv_not_found.
ENDTRY.

” Register event handlers for checkbox clicks and user commands
DATA lo_events TYPE REF TO cl_salv_events_table.
lo_events = gr_table->get_event( ).
SET HANDLER lcl_app=>on_link_click FOR lo_events.
SET HANDLER lcl_app=>handle_user_command FOR lo_events.

ELSE.
” Refresh SALV table if already created
gr_table->refresh( ).
ENDIF.

gr_table->display( ).
ENDMETHOD.

” Refresh display wrapper method
METHOD refresh_display.
lcl_app=>display_data( ).
ENDMETHOD.

” Event handler to toggle checkbox on clicking checkbox cell
METHOD on_link_click.
FIELD-SYMBOLS: <fs> TYPE t_zemployee.
READ TABLE gt_data ASSIGNING <fs> INDEX row.
IF sy-subrc = 0.
IF <fs>-checkbox = ‘X’.
<fs>-checkbox = ”.
ELSE.
<fs>-checkbox = ‘X’.
ENDIF.
ENDIF.
lcl_app=>refresh_display( ).
ENDMETHOD.

” Convert selected internal table data to XLS XSTRING format for attachment
METHOD convert_itab_to_xls_xstring.
TRY.
cl_salv_table=>factory(
EXPORTING
list_display = abap_false
IMPORTING
r_salv_table = DATA(lo_salv_table)
CHANGING
t_table = seleted ).
CATCH cx_salv_msg INTO DATA(lx_mail).
MESSAGE lx_mail TYPE ‘E’.
ENDTRY.

rv_xstring = lo_salv_table->to_xml( xml_type = if_salv_bs_xml=>c_type_xlsx ).
ENDMETHOD.

” Send email with the selected employee data as an Excel attachment
METHOD send_email.
TRY.
” Create persistent email object
DATA(lo_mail) = cl_bcs=>create_persistent( ).

” Sender email address
DATA(lo_sender_address) = cl_cam_address_bcs=>create_internet_address(
i_address_string = ‘maniyadav2585@gmail.com’ ).
lo_mail->set_sender( lo_sender_address ).

” Recipient email address
DATA(recipient_address) = cl_cam_address_bcs=>create_internet_address(
i_address_string = ‘maniyadav2585@gmail.com’ ).
lo_mail->add_recipient( i_recipient = recipient_address ).

” Create email document with subject and body text
DATA(lo_document) = cl_document_bcs=>create_document(
i_type = ‘htm’
i_subject = `Excel Attachment`
i_text = VALUE #( ( CONV #( ‘Excel Attachment’ ) ) )
i_sender = lo_sender_address ).

” Convert selected data to XLS format for attachment
DATA(lv_excel_data) = convert_itab_to_xls_xstring( ).

” Add the Excel attachment
lo_document->add_attachment(
i_attachment_type = ‘XLS’
i_attachment_subject = ‘Employee Information’
i_attachment_size = xstrlen( lv_excel_data )
i_att_content_hex = cl_bcs_convert=>xstring_to_solix( lv_excel_data ) ).

lo_mail->set_document( lo_document ).
lo_mail->set_send_immediately( abap_true ).

” Send the email
DATA(lv_send_success) = lo_mail->send( ).
COMMIT WORK.

CATCH cx_send_req_bcs cx_address_bcs cx_document_bcs INTO DATA(lx_mail).
MESSAGE lx_mail TYPE ‘E’.
ENDTRY.
ENDMETHOD.

” Handle custom toolbar commands (Select All, Deselect All, Send, Back)
METHOD handle_user_command.
FIELD-SYMBOLS: <fs> TYPE t_zemployee.
DATA lv_count TYPE i VALUE 0.

CASE e_salv_function.
WHEN ‘SELALL’.
LOOP AT gt_data ASSIGNING <fs>.
<fs>-checkbox = ‘X’.
ENDLOOP.
lcl_app=>refresh_display( ).

WHEN ‘DESALL’.
LOOP AT gt_data ASSIGNING <fs>.
<fs>-checkbox = ”.
ENDLOOP.
lcl_app=>refresh_display( ).

WHEN ‘SEND’.
” Collect selected records for sending
LOOP AT gt_data ASSIGNING <fs> WHERE checkbox = ‘X’.
lv_count = lv_count + 1.
APPEND <fs> TO seleted.
ENDLOOP.

IF lv_count = 0.
MESSAGE ‘Please select at least one record before sending.’ TYPE ‘E’.
RETURN.
ENDIF.

lcl_app=>convert_itab_to_xls_xstring( ).
lcl_app=>send_email( ).

IF sy-subrc = 0.
MESSAGE |{ lv_count } row(s) selected and sent.| TYPE ‘S’.
ENDIF.

WHEN ‘BACK’.
LEAVE TO SCREEN 0.
ENDCASE.
ENDMETHOD.

ENDCLASS.

” Program starts here
START-OF-SELECTION.
SET PF-STATUS ‘ZTOOLBAR’. ” Set custom toolbar with your custom functions
lcl_app=>display_data( ).  OutPut:     Read More Technology Blog Posts by Members articles 

#SAP

#SAPTechnologyblog

You May Also Like

More From Author