Application Development 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: 

GOS File Corrupted

Former Member
0 Kudos

Hi,

I use GOS to create attachment on FB03. But when I try to open the pdf attached from FB03, i get message 'There was an error opening this document. The file is damaged and could not be repaired.' I tried to use FM SO_CONVERT_CONTENTS_BIN, but the problem persists. Any idea how could I resolve this?

Following is my code:


FORM ATTACH_FILE
  USING    P_BOTYPE   TYPE borident-OBJTYPE
           P_BO_ID    TYPE borident-OBJKEY
           P_MSGTYP   TYPE SOFM-DOCTP
           P_DOCTY    TYPE borident-OBJTYPE
           P_RELTYP   TYPE BRELTYP-RELTYPE
           P_FILELOC  TYPE any
           P_TITLE    TYPE SOOD-OBJDES
           P_INTERCO  TYPE any
           P_INV      TYPE any
           P_FILE     TYPE any                             
  CHANGING P_MSG      TYPE any.

  TYPES: BEGIN OF TY_MESSAGE_KEY,
        FOLTP TYPE SO_FOL_TP,
        FOLYR TYPE SO_FOL_YR,
        FOLNO TYPE SO_FOL_NO,
        DOCTP TYPE SO_DOC_TP,
        DOCYR TYPE SO_DOC_YR,
        DOCNO TYPE SO_DOC_NO,
        FORTP TYPE SO_FOR_TP,
        FORYR TYPE SO_FOR_YR,
        FORNO TYPE SO_FOR_NO,
       END OF TY_MESSAGE_KEY.
  DATA : LV_MESSAGE_KEY type TY_MESSAGE_KEY.
  DATA : LO_MESSAGE type SWC_OBJECT.
  DATA : LT_DOC_CONTENT like standard table of SOLI
                             with header line.
  DATA: lv_msg(100).
  DATA: lv_inv1 TYPE bkpf-belnr,
        lv_inv2 TYPE bkpf-belnr,
        lv_invcp TYPE string.

  REFRESH LT_DOC_CONTENT.
  SPLIT p_inv AT '|' INTO lv_inv1 lv_inv2.
* Create an initial instance of BO 'MESSAGE' - to call the
* instance-independent method 'Create'.
  swc_create_object LO_MESSAGE 'MESSAGE' LV_MESSAGE_KEY.

* define container to pass the parameter values to the method call
* in next step.
  swc_container LT_MESSAGE_CONTAINER.

* Populate container with parameters for method
*  swc_set_element LT_MESSAGE_CONTAINER 'DOCUMENTTITLE' 'TestDocument'.
  swc_set_element LT_MESSAGE_CONTAINER 'DOCUMENTTITLE' P_TITLE.
  swc_set_element LT_MESSAGE_CONTAINER 'DOCUMENTLANGU' 'E'.
  swc_set_element LT_MESSAGE_CONTAINER 'NO_DIALOG'     'X'.
  swc_set_element LT_MESSAGE_CONTAINER 'DOCUMENTNAME'   P_DOCTY.
  swc_set_element LT_MESSAGE_CONTAINER 'DOCUMENTTYPE'   P_MSGTYP.

* File attachment
  OPEN DATASET P_FILELOC FOR INPUT MESSAGE lv_msg IN BINARY MODE.

  IF SY-subrc EQ 0.
    DO.
      READ DATASET P_FILELOC INTO LT_DOC_CONTENT.
      IF SY-subrc EQ 0.
        append LT_DOC_CONTENT.
      ELSE.
        EXIT.
      ENDIF.
    ENDDO.
    CLOSE DATASET P_FILELOC.
  ENDIF.

  CALL FUNCTION 'SO_CONVERT_CONTENTS_BIN'
    EXPORTING
      it_contents_bin = LT_DOC_CONTENT[]
    IMPORTING
      et_contents_bin = LT_DOC_CONTENT[].


* 'DocumentContent' is a multi-line element ( itab ).
  swc_set_table LT_MESSAGE_CONTAINER 'DocumentContent' LT_DOC_CONTENT.

* Size is required in case of File attachments
  data : LV_DOC_SIZE type i.
  data : L_FILE_LINES type i.

  DESCRIBE TABLE LT_DOC_CONTENT LINES L_FILE_LINES.

  READ TABLE LT_DOC_CONTENT INDEX L_FILE_LINES.

  LV_DOC_SIZE = ( 255 * ( L_FILE_LINES - 1 ) ) +
              STRLEN( LT_DOC_CONTENT ).

  swc_set_element LT_MESSAGE_CONTAINER 'DOCUMENTSIZE'   LV_DOC_SIZE .

* Refresh to get the reference of create 'MESSAGE' object for attachment
  swc_refresh_object LO_MESSAGE.
  swc_call_method LO_MESSAGE 'CREATE' LT_MESSAGE_CONTAINER.

* Get Key of new object
  swc_get_object_key LO_MESSAGE LV_MESSAGE_KEY.

* Create main BO object_a
  data: LO_IS_OBJECT_A type BORIDENT.

  LO_IS_OBJECT_A-OBJKEY = P_BO_ID.
  LO_IS_OBJECT_A-OBJTYPE = P_BOTYPE.

* Create attachment BO object_b
  data: LO_IS_OBJECT_B type BORIDENT.

  LO_IS_OBJECT_B-OBJKEY = LV_MESSAGE_KEY.
  LO_IS_OBJECT_B-OBJTYPE = P_DOCTY.

  call function 'BINARY_RELATION_CREATE'
    EXPORTING
      obj_rolea    = LO_IS_OBJECT_A
      obj_roleb    = LO_IS_OBJECT_B
      relationtype = P_RELTYP
    EXCEPTIONS
      others       = 1.

  IF sy-subrc = 0.    "return success msg - successfully attached

    commit work.
    CONCATENATE c_asterix lv_inv1 c_asterix INTO lv_invcp.

    IF P_INTERCO = c_x.   "interco
      IF p_bo_id CP lv_invcp.    "to only append 1 time of message
        exit.
      ELSE.
        CONCATENATE text-032 text-027 p_file text-028 lv_inv1 text-029
        lv_inv2 INTO p_msg SEPARATED BY space.
        MOVE p_msg TO gs_txt-msg.                         
        APPEND gs_txt TO gt_txt.                          
      ENDIF.
    ELSE.                 "non interco
      CONCATENATE text-032 text-027 p_file text-028 lv_inv1
      INTO p_msg SEPARATED BY space.
      MOVE p_msg TO gs_txt-msg.                            
      APPEND gs_txt TO gt_txt.                            
    ENDIF.

  ELSE.                 "return error msg - failed attachment

    CONCATENATE c_asterix lv_inv1 c_asterix INTO lv_invcp.

    IF P_INTERCO = c_x.   "interco
      IF p_bo_id CP lv_invcp.    
        exit.
      ELSE.
        CONCATENATE text-030 text-027 p_file text-031 lv_inv1 text-029 lv_inv2
        INTO p_msg
        SEPARATED BY space.
        MOVE p_msg TO gs_txt-msg.                           
        APPEND gs_txt TO gt_txt.                            
      ENDIF.
    ELSE.                 "non interco
      CONCATENATE text-030 text-027 p_file text-031 lv_inv1
      INTO p_msg
      SEPARATED BY space.
      MOVE p_msg TO gs_txt-msg.                          
      APPEND gs_txt TO gt_txt.                             
    ENDIF.
  ENDIF.

ENDFORM.                    " ATTACH_FILE

Edited by: big mug on Jul 14, 2010 7:02 AM

2 REPLIES 2

Former Member
0 Kudos

check the following link

**

Mr. a®s suggest following example program.

1. BDSTESTOO

2. SAPRDEMO_DOI_BDS

0 Kudos

Hi,

My problem is solved by adding APPEND lt_doc_content after the ELSE statement like below:


DO.
READ DATASET p_fname INTO lt_doc_content.
IF sy-subrc EQ 0.
APPEND lt_doc_content.
ELSE.
APPEND lt_doc_content.          "Added to get the last line when end of file is encountered.
EXIT.
ENDIF.

For more helpful information, please refer to /people/rammanohar.tiwari/blog/2006/03/25/generic-object-services-gos--in-background--part-ii