Skip to Content
avatar image
Former Member

write BAPI return messages into text

Dear experts,

              I want to write error messages into a text file. i tried in many ways like, using GUI_DOWNLOAD i converted messages to non sap but it creates only empty text file. Is there any other function module in sap?

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

8 Answers

  • May 28, 2014 at 10:40 AM

    Hi,

    did you try function RPY_MESSAGE_COMPOSE  ?

    regards

    Fred

    Add comment
    10|10000 characters needed characters exceeded

  • May 28, 2014 at 10:44 AM

    Hi Remya,

    First you have to store the BAPI messages to an internal table of type TDLINE. Then you use the GUI_DOWNLOAD with the file name & location.

    CALL FUNCTION 'GUI_DOWNLOAD'

               EXPORTING

                 filename                = lv_dfile

                 write_field_separator   = 'X'

               TABLES

                 data_tab                = t_download

               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.


    Here LV_FILE contains the file name & location to download and T_DOWNLOAD contains the messages. This works fine for me.


    Regards,

    Abijith

    Add comment
    10|10000 characters needed characters exceeded

  • May 28, 2014 at 11:17 AM

    And where exactly you have problem? Getting message text or saving text to file? If your code does not work, we can not tell why if you don't show it to us.

    Anyway, message text is usually in field: BAPIRET2-MESSAGE. If not, you can save it to string using syntax MESSAGE ..... INTO l_string. Then it is just simplest file download.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    May 28, 2014 at 12:00 PM

    it_return like bapiret2 OCCURS 0 WITH HEADER LINE


    CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'

       EXPORTING

         headdata                  = BAPIMATHEAD

        CLIENTDATA                 = bapi_mara

        CLIENTDATAX                = bapi_marax

        PLANTDATA                  = BAPI_MARC

        PLANTDATAX                = BAPI_MARCX

        IMPORTING

        RETURN                     = it_RETURN

      TABLES

        MATERIALDESCRIPTION        = IT_BAPI .

    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'  .

    WRITE : /1 IT_RETURN-ID, 10 IT_RETURN-TYPE, 20 IT_RETURN-NUMBER, 25 IT_RETURN-MESSAGE.

    it shows error messages, when am trying to download in text file, it doesn't write anything.

    am simply trying to download error messages in text file.

    Add comment
    10|10000 characters needed characters exceeded

  • May 28, 2014 at 01:39 PM

    Hi remya,

                   You can try with following FM,

    Formatting error messages by below FM

    LOOP AT it_messtab.

      CALL FUNCTION 'FORMAT_MESSAGE'

        EXPORTING

          id     = it_messtab-msgid

          lang   = it_messtab-msgspra

          no     = it_messtab-msgnr

          v1     = it_messtab-msgv1

          v2     = it_messtab-msgv2

        IMPORTING

        msg    =  g_msg

      EXCEPTIONS

          OTHERS = 0.

     

      IF it_messtab-msgtyp = 'E'.

        it_error-error_rec = g_msg.

        it_error-lifnr         = it_header-lifnr.

        it_error-tabix       = v_lines.

        APPEND it_error.

     

    ENDLOOP.

    and then use following FM

               CALL FUNCTION 'GUI_DOWNLOAD'

               EXPORTING

                 filename                = text_file

                 write_field_separator   = 'X'

               TABLES

                 data_tab                = it_error

               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.



    Regards,

    Shahezad

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    May 29, 2014 at 06:57 AM

    Thanks Guys, here is my sample code...


    tables:bapimathead,

            bapi_makt,

            bapi_mara,

            bapi_marax,

            bapi_marc,

            BAPI_MARCX.

    data:begin of itab occurs 0,

           MATERIAL type BAPIMATHEAD-MATERIAL,

           IND_SECTOR TYPE BAPIMATHEAD-IND_SECTOR,

           MATL_TYPE type bapimathead-MATL_TYPE,

           PLANT type BAPI_MARC-PLANT,

           MATL_DESC TYPE BAPI_MAKT-MATL_DESC,

           BASE_UOM TYPE bapi_mara-BASE_UOM,

           MATL_GROUP type bapi_mara-MATL_GROUP,

    *      OLD_MAT_NO TYPE BAPI_MARA-OLD_MAT_NO,

    *      DIVISION TYPE BAPI_MARA-DIVISION,

    *      UNIT_OF_WT TYPE BAPI_MARA-UNIT_OF_WT,

    *      NET_WEIGHT TYPE BAPI_MARA-NET_WEIGHT,

    *      DOCUMENT TYPE BAPI_MARA-DOCUMENT,

    *      DOC_TYPE TYPE BAPI_MARA-DOC_TYPE,

           LANGU TYPE BAPI_MAKT-LANGU,

           PUR_GROUP TYPE BAPI_MARC-PUR_GROUP,

           MRP_TYPE TYPE BAPI_MARC-MRP_TYPE,

           MRP_CTRLER TYPE BAPI_MARC-MRP_CTRLER,

           LOTSIZEKEY TYPE BAPI_MARC-LOTSIZEKEY,

           PLND_DELRY TYPE BAPI_MARC-PLND_DELRY,

           SM_KEY TYPE BAPI_MARC-SM_KEY,

           end of itab.

    DATA:    it_RETURN like bapiret2 OCCURS 0 WITH HEADER LINE,

              IT_BAPI LIKE BAPI_MAKT OCCURS 0 WITH HEADER LINE,

              IT_MSG LIKE TLINE OCCURS 0 WITH HEADER LINE.

    parameters:p_file TYPE IBIPPARMS-PATH obligatory.

    at selection-screen on value-request for p_file.

    perform get_f4help.

    start-of-selection.

    perform upload_file_itab.

    perform call_bapi.

    *END-OF-SELECTION.

    *PERFORM ERROR.

    *&---------------------------------------------------------------------*

    *&      Form  get_f4help

    *&---------------------------------------------------------------------*

    form get_f4help .

    CALL FUNCTION 'F4_FILENAME'

      EXPORTING

        PROGRAM_NAME        = SYST-CPROG

        DYNPRO_NUMBER       = SYST-DYNNR

      IMPORTING

        FILE_NAME           = P_FILE  .

    endform.                    " get_f4help

    *&---------------------------------------------------------------------*

    *&      Form  upload_file_itab

    *&---------------------------------------------------------------------*

    form upload_file_itab .

    DATA:V_FILE TYPE STRING.

    MOVE P_FILE TO V_FILE.

    CALL FUNCTION 'GUI_UPLOAD'

       EXPORTING

        filename                      = V_FILE

        FILETYPE                      = 'ASC'

        HAS_FIELD_SEPARATOR           = 'X'

       TABLES

         data_tab                      = ITAB .

    endform.                    " upload_file_itab

    *&---------------------------------------------------------------------*

    *&      Form  call_bapi

    *&---------------------------------------------------------------------*

    form call_bapi .

    loop at itab.

    BAPIMATHEAD-material = itab-MATERIAL.

    bapimathead-IND_SECTOR = itab-IND_SECTOR.

    BAPIMATHEAD-MATL_TYPE = itab-MATL_TYPE.

    BAPIMATHEAD-BASIC_VIEW = 'X'.

    BAPIMATHEAD-PURCHASE_VIEW = 'X'.

    BAPIMATHEAD-MRP_VIEW = 'X'.

    bapi_mara-MATL_GROUP = itab-MATL_GROUP.

    bapi_mara-base_uom = itab-base_uom.

    *BAPI_MARA-OLD_MAT_NO = ITAB-OLD_MAT_NO.

    bapi_marax-MATL_GROUP = 'X'.

    bapi_marax-BASE_UOM = 'X'.

    *BAPI_MARA-DIVISION = ITAB-DIVISION.

    *BAPI_MARA-UNIT_OF_WT = ITAB-UNIT_OF_WT.

    *BAPI_MARA-NET_WEIGHT = ITAB-NET_WEIGHT.

    *BAPI_MARA-DOCUMENT = ITAB-DOCUMENT.

    *BAPI_MARA-DOC_TYPE = ITAB-DOC_TYPE.

    IT_BAPI-LANGU = ITAB-LANGU.

    IT_BAPI-LANGU_ISO = 'EN'.

    IT_BAPI-MATL_DESC = ITAB-MATL_DESC.

    BAPI_MARC-PLANT = ITAB-PLANT.

    BAPI_MARC-PUR_GROUP = ITAB-PUR_GROUP.

    BAPI_MARC-MRP_TYPE = ITAB-MRP_TYPE.

    BAPI_MARC-MRP_CTRLER = ITAB-MRP_CTRLER.

    BAPI_MARC-LOTSIZEKEY = ITAB-LOTSIZEKEY.

    BAPI_MARC-PLND_DELRY = ITAB-PLND_DELRY.

    BAPI_MARC-SM_KEY = ITAB-SM_KEY.

    BAPI_MARCX-PLANT = 'X'.

    BAPI_MARCX-PUR_GROUP = 'X'.

    BAPI_MARCX-MRP_TYPE = 'X'.

    BAPI_MARCX-MRP_CTRLER = 'X'.

    BAPI_MARCX-LOTSIZEKEY = 'X'.

    BAPI_MARCX-PLND_DELRY = 'X'.

    BAPI_MARCX-SM_KEY = 'X'.

    APPEND IT_BAPI.

    clear it_bapi.

    CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'

       EXPORTING

         headdata                  = BAPIMATHEAD

        CLIENTDATA                 = bapi_mara

        CLIENTDATAX                = bapi_marax

        PLANTDATA                  = BAPI_MARC

        PLANTDATAX                = BAPI_MARCX

        IMPORTING

        RETURN                     = it_RETURN

      TABLES

        MATERIALDESCRIPTION        = IT_BAPI .

    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'  .

    LOOP AT IT_RETURN.

    CALL FUNCTION 'RPY_MESSAGE_COMPOSE'

       EXPORTING

        LANGUAGE                = SY-LANGU

         MESSAGE_ID              = IT_RETURN-ID

         MESSAGE_NUMBER          = IT_RETURN-NUMBER

      IMPORTING

        MESSAGE_TEXT            = IT_RETURN-MESSAGE

      TABLES

        LONGTEXT                = IT_MSG

               .

    IF SY-SUBRC <> 0.

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

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

    ENDIF.

    .

    ENDLOOP.

    CALL FUNCTION 'GUI_DOWNLOAD'

       EXPORTING

    *   BIN_FILESIZE                    =

        FILENAME                        = 'D:/DEMO.TXT'

        FILETYPE                        = 'ASC'

    *   APPEND                          = ' '

    *   WRITE_FIELD_SEPARATOR           = ' '

    * IMPORTING

    *   FILELENGTH                      =

       TABLES

         DATA_TAB                        = IT_RETURN

    *   FIELDNAMES                      =

      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.

    ENDIF.

    WRITE : /1 IT_RETURN-ID, 10 IT_RETURN-TYPE, 20 IT_RETURN-NUMBER, 25 IT_RETURN-MESSAGE.

    ENDLOOP.

    ENDFORM.

    Text file generated and getting message like 0 bytes transferred.

    Add comment
    10|10000 characters needed characters exceeded

    • Sorry, I don't understand.

      Message "0 bytes transferred" indicates that your data_tab might be empty. Check if you have data in it_return table => breakpoint on CALL FUNCTION 'GUI_DOWNLOAD' and check it_return[] content (not header line)...

      This should work without problem:

      DATA: lt_data TYPE TABLE OF bapiret2,

                ls_data LIKE LINE OF lt_data.

      ls_data-message = 'abc test'.

      APPEND ls_data to LT_data.

      CALL FUNCTION 'GUI_DOWNLOAD'

         EXPORTING

          FILENAME                        = 'D:/DEMO.TXT'

         TABLES

           DATA_TAB                        = lt_data

        EXCEPTIONS

          OTHERS                          = 22.

  • May 29, 2014 at 10:49 AM

    Hi Remya ,

                       Please see execute attached code.. it may give cleared idea to you

    REPORT Z_SALESORDERMIKE.

    data:begin of it occurs 0,
           grp type c,
           val type i,
          end of it.
    data:begin of heading occurs 0,
           TEXT(10) type c,
          end of heading.
    DATA  fullpath type string.
    DATA  path type string.
    DATA  filename type string.
          it-grp = 'A'.
          it-val = 100.
          append it.
          it-grp = 'B'.
          it-val = 200.
          append it.
          heading-text = 'GROUP'.
          append heading.
          heading-text = 'VALUE'.
          append heading.

    cl_gui_frontend_services=>file_save_dialog(
            exporting
             window_title          = 'SAVE PDF'

              file_filter          = 'txt|*.txt|(All file)|*.*'
            changing
              filename             = filename
             path                  = path
              fullpath             = fullpath
            exceptions
             cntl_error           = 1
             error_no_gui         = 2
             not_supported_by_gui = 3
             others               = 4
                         ) .
          call function 'GUI_DOWNLOAD'
            exporting
              filename                        = fullpath
              FILETYPE                        = 'DAT'
              WRITE_FIELD_SEPARATOR           = '#'
              SHOW_TRANSFER_STATUS            = 'X'
            tables
              data_tab                        = it[]
             FIELDNAMES                      = heading[]
           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.
          endif.




    Regards,


       Thangam.P

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jun 09, 2014 at 07:59 AM

    thanks to all. its resolved now.

    Add comment
    10|10000 characters needed characters exceeded