Skip to Content

Purpose: Download all Function Module source code belonging to a Function Group to local file system

Usage Scenario:

– you are working a new function group which doesn’t have a version history and you want to take a backup of the source code

– backup source code in between releasing the code

Please note that the following code will only download FM. Feel free to extend or modify the code to your needs.

REPORT  Z_UTIL_FG_DOWNLOAD.

TYPES: BEGIN OF ty_enlfdir,

        funcname TYPE enlfdir-funcname,

        END OF ty_enlfdir,

        BEGIN OF ty_tfdir,

          funcname TYPE tfdir-funcname,

          pname    TYPE tfdir-pname,

          include  TYPE tfdir-include,

        END OF ty_tfdir,

        BEGIN OF ty_text,

          txt(1000) TYPE c,

        END OF ty_text.

PARAMETERS: p_fg TYPE tlibg-area, “Function group

            p_dir TYPE string,    “Directory

            p_ext(3) TYPE c LOWER CASE.      “File extension

DATA: t_fm TYPE STANDARD TABLE OF tfdir,

      wa_fm LIKE LINE OF t_fm,

      t_enlfdir TYPE STANDARD TABLE OF ty_enlfdir,

      wa_enlfdir LIKE LINE OF t_enlfdir,

      r_enlfdir TYPE RANGE OF enlfdir-area,

      wa_r_enlfdir LIKE LINE OF r_enlfdir,

      t_tfdir TYPE STANDARD TABLE OF ty_tfdir,

      wa_tfdir LIKE LINE OF t_tfdir.

DATA: t_s TYPE STANDARD TABLE OF ty_text.

* Check that Function group exists

SELECT SINGLE area INTO p_fg

  FROM tlibg

  WHERE area = p_fg.

IF sy-subrc = 0.

* Get all function module names

  SELECT DISTINCT funcname

    FROM enlfdir

    INTO CORRESPONDING FIELDS OF TABLE t_enlfdir

    WHERE area = p_fg.

  IF sy-subrc = 0.

    LOOP AT t_enlfdir INTO wa_enlfdir.

      CLEAR: wa_r_enlfdir.

      wa_r_enlfdir-sign = ‘I’.

      wa_r_enlfdir-option = ‘EQ’.

      wa_r_enlfdir-low = wa_enlfdir-funcname.

      APPEND wa_r_enlfdir TO r_enlfdir.

    ENDLOOP.

*   Get INCLUDE number

    SELECT funcname pname include

      FROM tfdir

      INTO CORRESPONDING FIELDS OF TABLE t_tfdir

      WHERE funcname IN r_enlfdir.

    DATA: l_include TYPE RS38L-INCLUDE,

          l_filename TYPE string.

    LOOP AT t_tfdir INTO wa_tfdir.

      CLEAR: l_include.

*     Get INCLUDE name of the corresponding FM

      CALL FUNCTION ‘FUNCTION_INCLUDE_CONCATENATE’

        EXPORTING

          INCLUDE_NUMBER           = wa_tfdir-include

        IMPORTING

          INCLUDE                  = l_include

        CHANGING

          PROGRAM                  = wa_tfdir-pname

        EXCEPTIONS

          NOT_ENOUGH_INPUT         = 1

          NO_FUNCTION_POOL         = 2

          DELIMITER_WRONG_POSITION = 3

          OTHERS                   = 4.

      IF SY-SUBRC = 0.

        CLEAR: t_s[].

* Get the FM source code

        READ REPORT l_include INTO t_s.

        CONDENSE p_dir.

        CONCATENATE p_dir wa_tfdir-funcname ‘.’ p_ext INTO l_filename.

* Download the source to client file system

        CALL FUNCTION ‘GUI_DOWNLOAD’

          EXPORTING

            FILENAME                = l_filename

            FILETYPE                = ‘ASC’

            CONFIRM_OVERWRITE       = ‘X’

            SHOW_TRANSFER_STATUS    = ABAP_FALSE

          TABLES

            DATA_TAB                = t_s

          EXCEPTIONS

            FILE_WRITE_ERROR        = 1

            NO_BATCH                = 2

            GUI_REFUSE_FILETRANSFER = 3

            INVALID_TYPE            = 4

            NO_AUTHORITY            = 5

            UNKNOWN_ERROR           = 6

            HEADER_NOT_ALLOWED      = 7

            SEPARATOR_NOT_ALLOWED   = 8

            FILESIZE_NOT_ALLOWED    = 9

            HEADER_TOO_LONG         = 10

            DP_ERROR_CREATE         = 11

            DP_ERROR_SEND           = 12

            DP_ERROR_WRITE          = 13

            UNKNOWN_DP_ERROR        = 14

            ACCESS_DENIED           = 15

            DP_OUT_OF_MEMORY        = 16

            DISK_FULL               = 17

            DP_TIMEOUT              = 18

            FILE_NOT_FOUND          = 19

            DATAPROVIDER_EXCEPTION  = 20

            CONTROL_FLUSH_ERROR     = 21

            OTHERS                  = 22.

        IF SY-SUBRC <> 0.

          MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

          RETURN.

        ENDIF.

      ENDIF.

    ENDLOOP.

  ENDIF.

ELSE.

  WRITE:/ ‘Function group not found’.

ENDIF.

FG_download_screenshot.png

To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

  1. Paul Hardy

    Unless I am very much mistaken, you want to be able to take a copy of a function module, and then upload it again at a later date.

    If this is the case, I would advise you to look up SAPLINK, where you can save any type of SAP object on your local drive.

    I am really lazy, so whenevr I have a problem to solve I look up the internet to see if anyone has solved it before me. I think that is what the “design patterns” business is all about also.

    If I have misread your intent I aplogise, otherwise I hope you can benefit from this wonderful open source tool.

    Cheersy Cheers

    Paul

    (0) 
    1. Stevanic Artana Post author

      Hi Paul,

      I have never used SAPLink and I will check it out. Thank you for pointing it out to me. I appreciate it.

      I wrote the program above as a tool to help to take a backup of the code during programming so it helps me refer to capture older version of the code that cannot be captured in version history. Usually I just take a backup of the current code, modify the code and I will use the backup to refer to the old code before releasing the change in a transport. With a new FM, the original code is not available in version history since the object has not been released.

      I will have a look at SAPLink if it has similar feature or even more features that can assist me with day-to-day task in writing code.

      Thanks,

      Stev

      (0) 

Leave a Reply