Menu

How To Save Screenshots in SAP ABAP

2020-10-19       

This short blog post contains an ABAP snippet that allows saving a screenshot to the SAP ERP user's hard drive, using the well-known class CL_GUI_FRONTEND_SERVICES.

Recommended Now

Fire TV Stick Lite Essentials Bundle

This bundle contains Amazon Fire TV Stick Lite and Mission USB Power Cable. The USB power cable eliminates the need to find an AC outlet near your TV by powering Amazon Fire TV directly from your TV's USB port. Includes special power management circuitry that enhances the peak power capability of the USB port by storing excess energy and then releasing it as needed.

Check it out on amazon.com →

The ABAP class CL_GUI_FRONTEND_SERVICES is quite a versatile tool that every SAP ABAP developer should know. It contains a lot of extremely helpful tools that help dealing with basic user interface requirements, such as showing confirmation dialogs, downloading data, or, obviously, taking screenshots.

Here is a short ABAP snippet that demonstrates how to save a screenshot in ABAP. While using the cl_gui_frontend_services=>get_screenshot method seems obvoius enough, knowing how to convert the raw image data into the SOLIX format is something that may require help. This code piece demonstrates how to do it.

ABAP Program: How to Take and Download a Screenshot in ABAP

DATA lv_imagebytes TYPE xstring.
DATA lv_mimetype TYPE string.
DATA lv_user_action TYPE i.
DATA lv_file_name TYPE string.
DATA lv_full_path TYPE string.
DATA lv_filepath TYPE string.

* Wrap everything in a try/catch block to capture exceptions
TRY.
    * Capture a screenshot, using CL_GUI_FRONTEND_SERVICES
    * After this operation, lv_mimetype will contain the MIME type of the image, such as "image/jpg"
    cl_gui_frontend_services=>get_screenshot(
        IMPORTING
            mime_type_str = lv_mimetype
            image         = lv_imagebytes
    ).

    * Show the default "save file" dialog
    cl_gui_frontend_services=>file_save_dialog( 
        EXPORTING
            default_file_name = 'screenshot.jpg'
            default_extension = 'jpg'
            file_filter       = '(*.jpg)|*.jpg|'
        CHANGING
            filename          = lv_file_name
            path              = lv_filepath
            fullpath          = lv_full_path
            user_action       = lv_user_action
    ).

    * Check if the user confirmed the dialog.
    * TODO: implement the cancel action.
    IF lv_user_action = cl_gui_frontend_services=>action_ok.

        * Create an internal table, converting the image bytestring to SOLIX.
        DATA(it_solix_data) = cl_bcs_convert=>xstring_to_solix(
            EXPORTING
                iv_xstring = lv_image_bytes
        ).

        * Download the screenshot to the user's file system.
        cl_gui_frontend_services=>gui_download(
            EXPORTING
                filename     = lv_full_path
                filetype     = 'BIN'
                bin_filesize = xstrlen(lv_image_bytes)
            CHANGING
                data_tab     = it_solix_data
        ).

    ENDIF.

    CATCH cx_root INTO DATA(e_error_message).
ENDTRY.