Skip to Content
1
Former Member
Jan 21, 2011 at 03:36 PM

Issues extracting attachments to purchase req. using SO_DOCUMENT_READ_API1

182 Views

I am trying to download the attachments of a purchase requisition to a folder in the application server. The file downloads correctly but cannot be read when extracted to the PC using transaction CG3Y. The error tells me that the file has been damaged beyond repair. Could you help me correct my code so as to avoid damaging the file?

LOOP AT t_links INTO w_links.

    ADD 1 TO: w_count ,
              w_attach.

    w_document_id = w_links-instid_b.

    CLEAR w_filter.

    REFRESH: t_header  ,
             t_contents.

    CALL FUNCTION 'SO_DOCUMENT_READ_API1'
      EXPORTING
        document_id                = w_document_id
        filter                     = w_filter
      TABLES
        object_header              = t_header
        object_content             = t_contents
      EXCEPTIONS
        document_id_not_exist      = 1
        operation_no_authorization = 2
        x_error                    = 3
        OTHERS                     = 4.

    CASE sy-subrc.
      WHEN 0.

        CLEAR: w_filename ,
               w_extension.

        LOOP AT t_header INTO w_header.

          CASE w_header-line+0(12).
            WHEN '&SO_FILENAME'.

              SHIFT w_header-line LEFT BY 13 PLACES IN CHARACTER MODE.

              SPLIT w_header-line AT '.' INTO w_filename
                                              w_extension
                                              IN CHARACTER MODE.

              CONCATENATE wp_banfn
                          w_filename
                INTO w_filename.

              CONCATENATE wc_file
                          w_filename
                          '.'
                          w_extension
                INTO w_file.

              EXIT.

          ENDCASE.

        ENDLOOP.

        OPEN DATASET w_file FOR OUTPUT IN BINARY MODE.

        CASE sy-subrc.
          WHEN 0.

            LOOP AT t_contents INTO w_contents.

              TRANSFER w_contents TO w_file.

            ENDLOOP.

            CLOSE DATASET w_file.

            SELECT COUNT(*)
              FROM zmm_mro_dash_att
              WHERE banfn EQ wp_banfn
              AND   links EQ w_file.

            CASE sy-dbcnt.
              WHEN 0.

                w_zmm_mro_dash_att-mandt = sy-mandt.

                w_zmm_mro_dash_att-banfn = wp_banfn.

                w_zmm_mro_dash_att-links = w_file.

                INSERT zmm_mro_dash_att FROM w_zmm_mro_dash_att.

                CASE sy-subrc.
                  WHEN 0.

                    COMMIT WORK.

                  WHEN OTHERS.

                    ROLLBACK WORK.

                ENDCASE.

            ENDCASE.

        ENDCASE.

    ENDCASE.

  ENDLOOP.