How to execute an external OS commands in SAP ABAP?

April 01, 2024

Code: 

DATA: obj TYPE REF TO zcl_call_script.
DATA : lv_command      TYPE sxpglogcmd,
         lv_targetsystem TYPE rfchost_ext.

    CREATE OBJECT obj.
    lv_command = 'ZCOMMAND'.

    IF sy-sysid EQ 'GWD'.
      lv_targetsystem = 'sapr3d'.

      CALL METHOD obj->zcall_shell_script
        EXPORTING
          im_commandname  = lv_command
          im_targetsystem = lv_targetsystem.

    ELSEIF sy-sysid EQ 'GWP'.
      lv_targetsystem = 'sapr3p'.

      CALL METHOD obj->zcall_shell_script
        EXPORTING
          im_commandname  = lv_command
          im_targetsystem = lv_targetsystem.

    ENDIF.

--------------------------------------------------------
Class Name  - zcl_call_script
Method Name - zcall_shell_script

Code:
METHOD zcall_shell_script.

    DATA: lv_status TYPE btcxpgstat,
          lv_txt    TYPE string.

    CALL FUNCTION 'SXPG_COMMAND_EXECUTE'
      EXPORTING
        commandname                   = im_commandname
        operatingsystem               = sy-opsys
        targetsystem                  = im_targetsystem
      IMPORTING
        status                        = lv_status
      EXCEPTIONS
        no_permission                 = 1
        command_not_found             = 2
        parameters_too_long           = 3
        security_risk                 = 4
        wrong_check_call_interface    = 5
        program_start_error           = 6
        program_termination_error     = 7
        x_error                       = 8
        parameter_expected            = 9
        too_many_parameters           = 10
        illegal_command               = 11
        wrong_asynchronous_parameters = 12
        cant_enq_tbtco_entry          = 13
        jobcount_generation_error     = 14
        OTHERS                        = 15.
    IF sy-subrc <> 0.
      CONCATENATE sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 INTO lv_txt.
      WRITE: lv_txt.
    else.
      write: 'success'.
    ENDIF.

  ENDMETHOD.

IDE Used To Test This Code : SAP ABAP Editor.

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us. We will reply you as soon as possible.

How to check a file exist or not using cl_gui_frontend_services Class

February 28, 2024

Code :
REPORT zvp_file_exist.

DATAlv_file TYPE string.
DATAlv_result TYPE string.

lv_file 'C:\Users\CODETHETA\Desktop\demo.txt'.

CALL METHOD cl_gui_frontend_services=>file_exist
  EXPORTING
    file                 lv_file
  RECEIVING
    result               lv_result
  EXCEPTIONS
    cntl_error           1
    error_no_gui         2
    wrong_parameter      3
    not_supported_by_gui 4
    OTHERS               5.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

IF lv_result 'X'.
  WRITE'File Exist'.
ELSE.
  WRITE'File Not Exist'.
ENDIF.

IDE Used To Test This Code : ABAP Editor.

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us. We will reply you as soon as possible.

How to access one program data to another without EXPORT IMPORT and GET SET in SAP ABAP

February 25, 2024

ZVP_A and ZVP_B there are two programs. ZVP_A program data will be transfer to ZVP_B program using PERFORM display subroutine.

ZVP_A Program Code - 
REPORT zvp_a.

TYPESBEGIN OF ty_mara,
         matnr TYPE mara-matnr,
         mtart TYPE mara-mtart,
         matkl TYPE mara-matkl,
       END OF ty_mara.

DATAit TYPE STANDARD TABLE OF ty_mara.

SELECT matnr
       mtart
       matkl
  FROM mara INTO TABLE it
  UP TO 10 ROWS.

PERFORM display IN PROGRAM zvp_b IF FOUND.

ZVP_B Program Code - 
REPORT zvp_b.

TYPESBEGIN OF ty_mara,
         matnr TYPE mara-matnr,
         mtart TYPE mara-mtart,
         matkl TYPE mara-matkl,
       END OF ty_mara.

DATAwa TYPE ty_mara.

PERFORM display.

FORM display.
  DATAlv_tname TYPE LENGTH 30.
  FIELD-SYMBOLS <lt> TYPE ANY TABLE.
  MOVE '(ZVP_A)IT' TO lv_tname.
  ASSIGN (lv_tnameTO <lt>.

  LOOP AT <lt> INTO wa.
    write:/ wa-matnr,
            wa-mtart,
            wa-matkl.
  ENDLOOP.

ENDFORM.

IDE Used To Test This Code : SAP ABAP Editor.

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us. We will reply you as soon as possible.

How to comment in Selection Screen in SAP ABAP

February 13, 2024

Code : 

REPORT ZSELECTION_SCREEN_COMMENT.

PARAMETERS: p_name TYPE char20.

SELECTION-SCREEN ULINE /1(50).
SELECTION-SCREEN COMMENT /1(50) comm1.

INITIALIZATION.
comm1 = 'selection screen comment'.

IDE Used To Test This Code : SAP ABAP Editor.

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us. We will reply you as soon as possible.

How to know that ABAP program is running in background or not?

December 26, 2023

We can achieve this by using SY-BATCH system variables.

If an ABAP program running in background then value of sy-batch will be X.

If an ABAP program running in foreground then value of sy-batch will be blank.

So this is how web can differentiate the if an ABAP program running in background or not.

How to transfer internal table data to excel file using SAP_CONVERT_TO_XLS_FORMAT in SAP ABAP

October 24, 2023

In below program I have used SAP_CONVERT_TO_XLS_FORMAT function module to convert internal table data to excel file and save file to user computer.

Code :

DATALV_MATNR TYPE MARA-MATNR.

SELECT-OPTIONSS_MATNR FOR LV_MATNR.

START-OF-SELECTION.
SELECT FROM MARA INTO TABLE @DATA(IT_MARAWHERE matnr IN @S_MATNR.
      IF sy-subrc eq 0.
        CALL FUNCTION 'SAP_CONVERT_TO_XLS_FORMAT'
          EXPORTING
           I_FIELD_SEPERATOR          '#'
           I_LINE_HEADER              'X'
            i_filename                 =
'C:\Users\CODETHETA\Desktop\xyz.xls'
           I_APPL_KEEP                ' '
          tables
            i_tab_sap_data             IT_MARA
*         CHANGING
*           I_TAB_CONVERTED_DATA       =
         EXCEPTIONS
           CONVERSION_FAILED          1
           OTHERS                     2
                  .
        IF sy-subrc <> 0.
          write:'error'.
        ENDIF.

        IF sy-subrc eq 0.
          write:'created'.
        ENDIF.


      ENDIF.

IDE Used To Test This Code : SAP ABAP Editor.

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us. We will reply you as soon as possible.

How to delete a file using CL_GUI_FRONTEND_SERVICES class in SAP ABAP

October 24, 2023

Here I have used CL_GUI_FRONTEND_SERVICES class to copy TXT file content.

In below program demo.txt file must exist in your desktop with some content in it.

Code : 

DATAobj TYPE REF TO cl_gui_frontend_services.
DATAlv_rc TYPE i.

CALL METHOD cl_gui_frontend_services=>file_delete
  EXPORTING
    filename             'C:\Users\CODETHETA\Desktop\demo.txt'
  CHANGING
    rc                   lv_rc
  EXCEPTIONS
    file_delete_failed   1
    cntl_error           2
    error_no_gui         3
    file_not_found       4
    access_denied        5
    unknown_error        6
    not_supported_by_gui 7
    wrong_parameter      8
    OTHERS               9.
IF sy-subrc <> 0.
  WRITE'File Not Deleted'.
ELSE.
  WRITE'File Deleted'.
ENDIF.

IDE Used To Test This Code : SAP ABAP Editor.

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us. We will reply you as soon as possible.

How to copy a file using CL_GUI_FRONTEND_SERVICES class in SAP ABAP

October 16, 2023

Here I have used CL_GUI_FRONTEND_SERVICES class to copy TXT file content.

In below program demo.txt file must exist in your desktop with some content in it.

Code :

DATAlv_source TYPE string.
DATAlv_dest TYPE string.

lv_source 'C:\Users\CODETHETA\Desktop\demo.txt'.
lv_dest 'C:\Users\CODETHETA\Desktop\dest.txt'.

CALL METHOD cl_gui_frontend_services=>file_copy
  EXPORTING
    source               lv_source
    destination          lv_dest
    overwrite            SPACE
  EXCEPTIONS
    cntl_error           1
    error_no_gui         2
    wrong_parameter      3
    disk_full            4
    access_denied        5
    file_not_found       6
    destination_exists   7
    unknown_error        8
    path_not_found       9
    disk_write_protect   10
    drive_not_ready      11
    not_supported_by_gui 12
    OTHERS               13.
IF sy-subrc <> 0.
* Implement suitable error handling here
ELSE.
  WRITE'file copied successfully'.
ENDIF.

IDE Used To Test This Code : SAP ABAP Editor.

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us. We will reply you as soon as possible.

PRODUCTS

LISTS OF PRODUCTS

SERVICES

SKILLS
SOFTWARE DEVELOPMENT
WEBSITE DEVELOPMENT
WEB HOSTING
BULK SMS SERVICE
SEO SERVICE
ANDROID APPS
QR CODE / BARCODE
HARDWARE SERVICE
OUR WORK AREA