Comments

Execute ABAP code from text file using ABAP READ INSERT to create program dynamically




Using ABAP code you are able to manipulate programs dynamically during execution. The code below demonstrates how to read, delete and insert new code into an ABAP report program. It uploads a file containing ABAP code which is inserted into a temporary program. This is then executed using the submit statement and deleted once execution is complete.

Basic ABAP syntax for manipulating reports dynamically: INSERT REPORT <prog> FROM <itab>. READ REPORT <prog> INTO <itab>. DELETE REPORT <prog>.


*Dynamic manipulation of report programs
*............................................................
*: Report  ZDYNAMICCODE                                     :
*:...........................................................
*:   Execute ABAP from a PC file(.txt)                      :
*:   Therefor allowing any abap code to be executed on any  :
*:   clientwhich contains this program                      :
*:                                                          :
*...........................................................:
REPORT  zdynamiccode.

TABLES rlgrap.

*selection screen creation
PARAMETER:       p_file(200) TYPE c,
                 p_temp(30)  TYPE c DEFAULT 'Z123TEMP_REPORT_FOR_CODE'.

*data declaration
DATA: it_tab TYPE filetable,
      gd_subrc TYPE i,
      answer TYPE c.

TYPES: BEGIN OF t_abapcode,
  row(72) TYPE c,
 END OF t_abapcode.
DATA: it_abapcode TYPE STANDARD TABLE OF t_abapcode INITIAL SIZE 0,
      it_store    TYPE STANDARD TABLE OF t_abapcode INITIAL SIZE 0.

DATA: filename TYPE  string.


************************************************************************
*AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
*Selecting a File, plus inserting default file extension
  REFRESH: it_tab.
  CALL METHOD cl_gui_frontend_services=>file_open_dialog
    EXPORTING
      window_title     = 'Select File'
      default_filename = '*.txt'
      multiselection   = ' '
    CHANGING
      file_table       = it_tab
      rc               = gd_subrc.

  LOOP AT it_tab INTO p_file.
  ENDLOOP.


************************************************************************
*START-OF-SELECTION.
START-OF-SELECTION.

*move file name into a field with type compatable with FM
  filename = p_file.

*upload file from PC
  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename                = filename
    TABLES
      data_tab                = it_abapcode
    EXCEPTIONS
      file_open_error         = 1
      file_read_error         = 2
      no_batch                = 3
      gui_refuse_filetransfer = 4
      invalid_type            = 5
      no_authority            = 6
      unknown_error           = 7
      bad_data_format         = 8
      header_not_allowed      = 9
      separator_not_allowed   = 10
      header_too_long         = 11
      unknown_dp_error        = 12
      access_denied           = 13
      dp_out_of_memory        = 14
      disk_full               = 15
      dp_timeout              = 16
      OTHERS                  = 17.
  IF sy-subrc EQ 0.
*   Check if report name being used fro temorary code already exists as
*   any code will be over written and lost
    READ REPORT p_temp INTO it_store.
    IF sy-subrc NE 0.
*     Please note any existing code in the program will be lost!!!!
      INSERT REPORT p_temp FROM it_abapcode.
      SUBMIT (p_temp) AND RETURN.
      DELETE REPORT p_temp.
    ELSE.
      CALL FUNCTION 'POPUP_TO_CONFIRM'
        EXPORTING
       text_question  = 'Report used to store temporary code already
exists!!! Do you want to overwrite it?'
       IMPORTING
         answer                      = answer.
      IF sy-subrc EQ 0.
        IF answer EQ '1'. "yes
*         Please note any existing code in the program will be lost!!!!
          INSERT REPORT p_temp FROM it_abapcode.
          SUBMIT (p_temp) AND RETURN.
          DELETE REPORT p_temp.
        ENDIF.
      ENDIF.
    ENDIF.
  ENDIF.





sapdev logo background
sapdev logo sapdev logo


Can't find something on ERPWorkbench? A quick search should fix that: