Skip to Content
0
Former Member
Jun 28, 2006 at 10:57 AM

howto email documents attached to product

163 Views

i'm trying to email a PDF document that is attached to a product (commpr01). From the GUI this is easy but how to do it from code?

I'm using

cl_crm_documents=>get_info

and

cl_crm_documents=>get_document

To access the binary data of the document attached to the product.

I'm using

SO_NEW_DOCUMENT_ATT_SEND_API1

to send an email with the attachment

Before sending i'm using function

TABLE_COMPRESS

to mold the binary data from

cl_crm_documents=>get_document

into binary data for

SO_NEW_DOCUMENT_ATT_SEND_API1

.

Somewhere along in the whole process something get garbled because, although i do receive an email WITH attachment, the document is unreadable.

I realy could use some help right about now..

regards, Marcel

my test program:

REPORT  ztest_documents_display                                     .

DATA:
  ls_business_object TYPE  sibflporb,
  lt_loios TYPE skwf_ios,
  lt_phios TYPE skwf_ios.


* product id invullen
ls_business_object-instid = 'D0CB9644CAEE2030E100000089780315'.
ls_business_object-typeid = 'BUS1178'.
ls_business_object-catid = 'BO'.

break makkerman.

CALL METHOD cl_crm_documents=>get_info
  EXPORTING
    business_object       = ls_business_object
*    PROPERTIES_REQUEST    =
*    NEWEST_ONLY           =
*    PROPERTIES_QUERY      =
*    PROPERTIES_QUERY_RA   =
  IMPORTING
*    PHIOLOIOS             =
*    IOS_PROPERTIES_RESULT =
    loios                 = lt_loios
    phios                 = lt_phios
    .


data:
  ls_io type skwf_io,
  lt_properties type sdokproptys,
  lt_file_access_info type sdokfilacis,
  ls_file_access_info type sdokfilaci,
  lt_content_ascii type sdokcntascs,
  lt_content_bin type sdokcntbins,
  lt_business_objects type sibflporbt,
  ls_loio 	type skwf_io,
  lv_io_does_not_exist 	type xflag.



DATA: docdata    LIKE sodocchgi1,
      objpack    LIKE sopcklsti1 OCCURS  1 WITH HEADER LINE,
      objhead    LIKE solisti1   OCCURS  1 WITH HEADER LINE,
      objtxt     LIKE solisti1   OCCURS 10 WITH HEADER LINE,
      objbin     LIKE solisti1   OCCURS 10 WITH HEADER LINE,
      objhex     LIKE solix      OCCURS 10 WITH HEADER LINE,
      reclist    LIKE somlreci1  OCCURS  1 WITH HEADER LINE.

DATA: tab_lines  TYPE i,
      doc_size   TYPE i,
      att_type   LIKE soodk-objtp.

objbin = '  |  '.
APPEND objbin.

break makkerman.

LOOP AT lt_loios INTO ls_io.

  CALL METHOD cl_crm_documents=>get_document
    EXPORTING
      io                = ls_io
    IMPORTING
      properties        = lt_properties
      phios             = lt_phios
      file_access_info  = lt_file_access_info
      content_ascii     = lt_content_ascii
      content_bin       = lt_content_bin
      business_objects  = lt_business_objects
      loio              = ls_loio
      io_does_not_exist = lv_io_does_not_exist.

  IF lv_io_does_not_exist IS INITIAL.
    LOOP AT lt_file_access_info INTO ls_file_access_info.
      NEW-LINE.
      NEW-LINE.
      WRITE: / 'Filename: ', ls_file_access_info-file_name, 'Mime-Type: ', ls_file_access_info-mimetype .
    ENDLOOP.

  ENDIF.


ENDLOOP.

*   Because listobject is of size RAW(1000)
*       and objbin     is of size CHAR(255) we make this table copy
CALL function 'TABLE_COMPRESS'
*     IMPORTING
*       COMPRESSED_SIZE       =
tables
IN                    = lt_content_bin
out                   = objbin
EXCEPTIONS
others                = 1
    .
IF sy-subrc <> 0.
  MESSAGE ID '61' TYPE 'E' NUMBER '731'
  WITH 'TABLE_COMPRESS'.
ENDIF.


* NOTE: Creation of attachment is finished yet.
*   For your report, the attachment should be placed into table
*     objtxt for plain text or
*     objbin for binary content.
*   Now create the message and send the document. 'of recipients'.
* Create Message Body
*   Title and Description
docdata-obj_name  = 'Your Order'.
docdata-obj_descr = 'Marcel stuurt PDFje ?'.
*   Main Text
objtxt = 'Test Document.'.
APPEND objtxt.
objtxt = 'Pompie dom hier is een pdf...'.
APPEND objtxt.
objtxt = 'Have a nice day.'.
APPEND objtxt.
*   Write Packing List (Main)
DESCRIBE TABLE objtxt LINES tab_lines.
READ     TABLE objtxt INDEX tab_lines.
docdata-doc_size = ( tab_lines - 1 ) * 255 + STRLEN( objtxt ).
CLEAR objpack-transf_bin.
objpack-head_start = 1.
objpack-head_num   = 0.
objpack-body_start = 1.
objpack-body_num   = tab_lines.
objpack-doc_type   = 'RAW'.
APPEND objpack.
* Create Message Attachment
*   Write Packing List (Attachment)
DESCRIBE TABLE objbin LINES tab_lines.
READ     TABLE objbin INDEX tab_lines.
objpack-doc_size = ( tab_lines - 1 ) * 255 + STRLEN( objbin ).
objpack-transf_bin = 'X'.
objpack-head_start = 1.
objpack-head_num   = 0.
objpack-body_start = 1.
objpack-body_num   = tab_lines.
objpack-doc_type   = 'BIN'.
objpack-obj_name   = 'ATTACHMENT'.
objpack-obj_descr  = ls_file_access_info-file_name.
APPEND objpack.

* Create receiver list
reclist-receiver = '_____@gmail.com'.  "<-- change address
reclist-rec_type = 'U'.
APPEND reclist.
* Send Message
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
  EXPORTING
    document_data                     = docdata
     put_in_outbox                     = ' '
     commit_work                       = 'X'     "used from rel.6.10
*   IMPORTING
*     SENT_TO_ALL                        =
*     NEW_OBJECT_ID                     =
  TABLES
    packing_list                      = objpack
    object_header                     = objhead
*    contents_bin                      = objbin
    contents_txt                      = objtxt
*    CONTENTS_HEX                      = objhex
*     OBJECT_PARA                        =
*     OBJECT_PARB                        =
    receivers                         = reclist

        .
IF sy-subrc <> 0.
  MESSAGE ID 'SO' TYPE 'S' NUMBER '023'
          WITH docdata-obj_name.
ENDIF.
WRITE: / 'End of Program'.