Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Issues extracting attachments to purchase req. using SO_DOCUMENT_READ_API1

Former Member
0 Likes
1,226

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.

3 REPLIES 3
Read only

Former Member
0 Likes
709

For the benefit of everybody else who might face this issue I would like to share my solution.

DATA: w_is_object_a  TYPE          sibflporb        ,
        w_rel          TYPE          obl_s_relt       ,
        w_links        TYPE          obl_s_link       ,
        w_folder_id    TYPE          soodk            ,
        w_object_id    TYPE          soodk            ,
        w_objcont      TYPE          soli             ,
        w_objhead      TYPE          soli             ,
        w_loio_object  TYPE          sdokobject       ,
        w_phio_object  TYPE          sdokobject       ,
        w_comp_id(100) TYPE          c                ,
        w_comp_size    TYPE          i                ,
        w_filename     TYPE          rlgrap-filename  ,

        t_rel          TYPE          obl_t_relt       ,
        t_links        TYPE          obl_t_link       ,
        t_objcont      TYPE TABLE OF soli             ,
        t_objhead      TYPE TABLE OF soli             ,
        t_data_txt     TYPE TABLE OF sdokcntasc       ,
        t_data_bin     TYPE TABLE OF sdokcntbin       .


  w_is_object_a-instid = wp_banfn.

  w_is_object_a-typeid = 'BUS2105'.

  w_is_object_a-catid  = 'BO'.

  w_rel-sign           = 'I'.

  w_rel-option         = 'EQ'.

  w_rel-low            = 'ATTA'.

  APPEND w_rel TO t_rel.

  CLEAR  w_rel.

  TRY.

      CALL METHOD cl_binary_relation=>read_links
        EXPORTING
          is_object           = w_is_object_a
          it_relation_options = t_rel
        IMPORTING
          et_links            = t_links.

    CATCH cx_obl_parameter_error .
    CATCH cx_obl_internal_error .
    CATCH cx_obl_model_error .

  ENDTRY.

  DELETE t_links WHERE typeid_b NE 'MESSAGE'.

The rest of the code follows in the next post.

Read only

709
LOOP AT t_links INTO w_links.

    w_folder_id-objtp = w_links-instid_b+0(3).

    w_folder_id-objyr = w_links-instid_b+3(2).

    w_folder_id-objno = w_links-instid_b+5(12).

    w_object_id-objtp = w_links-instid_b+17(3).

    w_object_id-objyr = w_links-instid_b+20(2).

    w_object_id-objno = w_links-instid_b+22(12).

    REFRESH: t_objcont,
             t_objhead.

    CALL FUNCTION 'SO_OBJECT_READ'
      EXPORTING
        folder_id                  = w_folder_id
        object_id                  = w_object_id
      TABLES
        objcont                    = t_objcont
        objhead                    = t_objhead
      EXCEPTIONS
        active_user_not_exist      = 1
        communication_failure      = 2
        component_not_available    = 3
        folder_not_exist           = 4
        folder_no_authorization    = 5
        object_not_exist           = 6
        object_no_authorization    = 7
        operation_no_authorization = 8
        owner_not_exist            = 9
        parameter_error            = 10
        substitute_not_active      = 11
        substitute_not_defined     = 12
        system_failure             = 13
        x_error                    = 14
        OTHERS                     = 15.

    CASE sy-subrc.
      WHEN 0.

        READ TABLE t_objcont INTO w_objcont INDEX 1.

        CASE sy-subrc.
          WHEN 0.

            CASE w_objcont-line+0(17).
              WHEN '&SO_KProObjectID='.

                w_loio_object = w_objcont-line+17.

            ENDCASE.

        ENDCASE.

        CLEAR w_phio_object.

        CALL FUNCTION 'SO_LOIO_PHIO_GET'
          EXPORTING
            loio_object        = w_loio_object
          IMPORTING
            phio_object        = w_phio_object
          EXCEPTIONS
            kpro_inconsistency = 1
            x_error            = 2
            OTHERS             = 3.

        CASE sy-subrc.
          WHEN 0.

            CLEAR w_comp_id.

            LOOP AT t_objhead INTO w_objhead.

              CASE w_objhead-line+0(13).
                WHEN '&SO_FILENAME='.

                  w_comp_id = w_objhead-line+13.

              ENDCASE.

            ENDLOOP.

            REFRESH: t_data_txt,
                     t_data_bin.

            CLEAR w_comp_size.

            CALL FUNCTION 'SCMS_R3DB_GET'
              EXPORTING
                crep_id      = 'SOFFDB'
                doc_id       = w_phio_object-objid
                comp_id      = w_comp_id
              IMPORTING
                comp_size    = w_comp_size
              TABLES
                data_txt     = t_data_txt
                data_bin     = t_data_bin
              EXCEPTIONS
                error_import = 1
                error_config = 2
                OTHERS       = 3.

            CASE sy-subrc.
              WHEN 0.

                CONCATENATE wc_file
                            wp_banfn
                            '_'
                            w_comp_id
                  INTO w_filename.

                CALL FUNCTION 'HR_CA_DOWNLOAD_TO_APPSERVER'
                  EXPORTING
                    filename           = w_filename
                    filesize           = w_comp_size
                  TABLES
                    data_tab           = t_data_bin
                  EXCEPTIONS
                    invalid_filesize   = 1
                    no_authority       = 2
                    dataset_open_error = 3
                    OTHERS             = 4.


                CASE sy-subrc.
                  WHEN 0.

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

                    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_filename.

                        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.

        ENDCASE.

    ENDCASE.

  ENDLOOP.

Hope this helps.

Read only

0 Likes
709

It works perfectly, thanks. Thanks for sharing.