Skip to Content
0
Aug 09, 2011 at 11:49 AM

Service Object Attachment in Workitem

38 Views

Hi Experts!!

We have a requirement to attach the Service Object attachments to Workitem. After searching in SDN, I have almost implemented everything. But when executed, the attachment is not working. It's throwing an error as 'The file has been damaged'. PFB my code:

FUNCTION ztest_service_atta.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(IV_WORKITEMID) TYPE  SWW_WIID OPTIONAL
*"     REFERENCE(IV_PERNR) TYPE  PERSNO
*"     REFERENCE(IV_REINR) TYPE  REINR
*"  EXPORTING
*"     REFERENCE(ES_ATT_ID) TYPE  SWR_ATT_ID
*"  EXCEPTIONS
*"      ATTACH_FAILED
*"----------------------------------------------------------------------

  DATA: gs_att_header TYPE swr_att_header.

  DATA: ls_lpor TYPE sibflporb,
  lt_lpor LIKE TABLE OF ls_lpor,
  ls_option TYPE obl_s_relt,
  lt_option TYPE obl_t_relt,
  ls_rol_op TYPE obl_s_rolt,
  lt_rol_op TYPE obl_t_rolt,
  ls_links TYPE obl_s_link,
  lt_links TYPE obl_t_link,
  ls_folder TYPE soodk,
  ls_object TYPE soodk,
  ls_obj_hd TYPE sood2,
  lt_object_content_l TYPE TABLE OF solisti1,
  ls_object_content_l TYPE solisti1,
  lt_obj_cont TYPE TABLE OF soli.

* Work areas
  DATA: lwa_doc_data        LIKE sodocchgi1,
        lwa_document_data   LIKE sofolenti1,
        lv_document_id      TYPE sofolenti1-doc_id,
        lwa_links           LIKE LINE OF lt_links,
        lwa_object          TYPE borident,
        ev_binfile TYPE xstring.

  FIELD-SYMBOLS <p> TYPE x.

  ls_lpor-instid = '000011110000000123'.
  ls_lpor-typeid = 'BUS2089'.
  ls_lpor-catid = 'BO'.
  APPEND ls_lpor TO lt_lpor.

  ls_option-sign = 'I'.
  ls_option-option = 'EQ'.
  ls_option-low = 'ATTA'.
  APPEND ls_option TO lt_option.
 
  ls_rol_op-sign = 'I'.
  ls_rol_op-option = 'EQ'.
  ls_rol_op-low = 'GOSAPPLOBJ'.
  APPEND ls_rol_op TO lt_rol_op.
*
  CALL METHOD cl_binary_relation=>read_links
    EXPORTING
      is_object           = ls_lpor
      it_relation_options = lt_option
      it_role_options     = lt_rol_op
    IMPORTING
      et_links            = lt_links.

* Process the attachment list
  LOOP AT lt_links INTO ls_links.
    lv_document_id = ls_links-instid_b.
* Read the data
    CALL FUNCTION 'SO_DOCUMENT_READ_API1'
      EXPORTING
        document_id                = lv_document_id
      IMPORTING
        document_data              = lwa_document_data
      TABLES
        object_content             = lt_object_content_l
      EXCEPTIONS
        document_id_not_exist      = 1
        operation_no_authorization = 2
        x_error                    = 3
        OTHERS                     = 4.
  ENDLOOP.

  LOOP AT lt_object_content_l INTO ls_object_content_l.
    ASSIGN ls_object_content_l TO <p> CASTING.
    CONCATENATE ev_binfile <p> INTO ev_binfile IN BYTE MODE.
  ENDLOOP.

  IF ev_binfile IS NOT INITIAL AND iv_workitemid IS NOT INITIAL.
    gs_att_header-file_type       = 'B'.
    gs_att_header-file_extension  = 'PDF'.
    gs_att_header-language        = 'EN'.
    gs_att_header-file_name       = 'Scanned Atta.PDF'.
    CALL FUNCTION 'SAP_WAPI_ATTACHMENT_ADD'
      EXPORTING
        workitem_id = iv_workitemid
        att_header  = gs_att_header
        att_bin     = ev_binfile
        do_commit   = 'X'
      IMPORTING
        att_id      = es_att_id.

    IF es_att_id IS INITIAL.
      RAISE attach_failed.
    ENDIF.
  ENDIF.

ENDFUNCTION.

Can somebody please tell me if I went somewhere wrong. What I feel is, it's because of the PDF file being 255 chars. As I have got the PDF file but not OTF, how can I convert that ot be of 132 chars. Whatever, is the problem due to this reason only?

Please help me out. Your help is really appreciable.