Recently I was looking for a pointer on SDN for one of my requirement. The requirement was to read the sales order attachment and send that attachment in an e-mail in the same format it is stored in the sales order.
This started my quest to find how documents attached to any application objects like sales order, accounting documents, service order can be extracted.
I thought to blog this and hope it will give a clue to those who are still looking to crack this problem.* </p><p> </p><p>This blog describes how sales order attachment is read and sent in an e-mail as document attachment. Similar way documents attached to other application object can be read.</p><p> </p><p>To attach documents you need to do few settings, add a new parameter SD_SWU_ACTIVE, value X in parameter tab of SU01 transaction. Save and come out of the transaction. This will enable attachment icon in VA02 transaction.</p><p> </p><p> !https://weblogs.sdn.sap.com/weblogs/images/251791491/POattachmentsu01parameter.jpg|height=125|alt=im...!</p><p> </p><p> </p><p> </p><p> !https://weblogs.sdn.sap.com/weblogs/images/251791491/POattachmentdocument.jpg|height=182|alt=image|w...!</p><p> </p><p> !https://weblogs.sdn.sap.com/weblogs/images/251791491/POattachmentlist.jpg|height=174|alt=image|width...! </p><p> </p><p> </p><p>As you can see in the above picture that a word document is attached to a sales order, now we get into the code snippet to see how attachments are read.</p><p> </p><p style="margin: 0cm 0cm 10pt; text-indent: 36pt" class="MsoNormal">"Prepare Data for attachment” Read Generic Object Services</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> </p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal">is_role_options-sign = 'I'.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal">is_role_options-option = 'EQ'.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal">is_role_options-low = 'GOSAPPLOBJ'.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal">append is_role_options to it_role_options.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal">clear is_role_options.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> </p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> </p><p style="margin: 0cm 0cm 10pt; text-indent: 36pt" class="MsoNormal">”document relation
is_relation_options-sign = 'I'.
is_relation_options-option = 'EQ'.
is_relation_options-low = 'ATTA'.
append is_relation_options to it_relation_options.
clear is_relation_options .
"Prepare the email content & header</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal">ls_document_data-obj_descr = ‘Sales Order attachement’.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal">ls_document_data-obj_langu = sy-langu.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> </p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal">ls_object_header-line = ‘Sales order Attachment’.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal">append ls_object_header to lt_object_header.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal">clear ls_object_header.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> </p><p style="margin: 0cm 0cm 0pt 36pt; text-align: justify" class="MsoNormal">ls_contents_txt-line = 'Please find the document attached in the sales order’.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal">Append ls_contents_txt to lt_contents_txt.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal">Clear ls_contents_txt.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> </p><p style="margin: 0cm 0cm 10pt; text-indent: 36pt" class="MsoNormal">Describe table lt_contents_txt lines lv_lines.</p><p style="margin: 0cm 0cm 10pt; text-indent: 36pt" class="MsoNormal"> </p><p style="margin: 0cm 0cm 10pt; text-indent: 36pt" class="MsoNormal">Clear ls_packing_list-transf_bin.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal">ls_packing_list-head_start = 1.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal">ls_packing_list-head_num = 0.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal">ls_packing_list-body_start = 1.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal">ls_packing_list-body_num = lv_lines.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal">ls_packing_list-doc_type = 'RAW'.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal">append ls_packing_list to lt_packing_list.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal">clear ls_packing_list.</p><p style="margin: 0cm 0cm 10pt; text-indent: 36pt" class="MsoNormal"> </p><p style="margin: 0cm 0cm 10pt; text-indent: 36pt" class="MsoNormal">"Get the binary releationship of the document
TRY.
CALL METHOD cl_binary_relation=>read_links
EXPORTING
is_object = is_object
ip_logsys = ip_logsys
it_role_options = it_role_options
it_relation_options = it_relation_options
ip_no_buffer = ip_no_buffer
IMPORTING
et_links = gt_links.
CATCH cx_obl_parameter_error.
CATCH cx_obl_internal_error.
CATCH cx_obl_model_error.
ENDTRY.
*"Read the each Ref. links and convert in hex. content
LOOP AT gt_links INTO gs_links.
lv_doc_id = gs_links-instid_b.
CALL FUNCTION 'SO_DOCUMENT_READ_API1'
EXPORTING
document_id = lv_doc_id
IMPORTING
document_data = ls_documdata
TABLES
object_content = lt_objcont
contents_hex = lt_objconthex
EXCEPTIONS
document_id_not_exist = 1
operation_no_authorization = 2
x_error = 3
OTHERS = 4.
IF sy-subrc = 0.
"Convert the ref. links to Attachment in e-mail</p><p style="margin: 0cm 0cm 10pt; text-indent: 36pt" class="MsoNormal"> DESCRIBE TABLE lt_objcont LINES lv_lines.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> </p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> ls_packing_list-transf_bin = abap_true.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> ls_packing_list-body_num = lv_lines.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> ls_packing_list-doc_type = ls_documdata-obj_type.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> ls_packing_list-obj_name = 'ATTACHMENT'.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> ls_packing_list-obj_descr = ls_documdata-obj_descr.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> ls_packing_list-doc_size = ls_documdata-doc_size.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> </p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> APPEND ls_packing_list TO packing_list.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> CLEAR ls_packing_list.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> </p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> LOOP AT lt_objcont INTO ls_objcont.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> ls_contents_bin-line = ls_objcont-line.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> APPEND ls_contents_bin TO contents_bin.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> CLEAR: ls_contents_bin, ls_objcont.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> ENDLOOP.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> </p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> </p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> LOOP AT lt_objconthex INTO ls_objconthex.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> ls_contents_hex-line = ls_objconthex-line.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> APPEND ls_contents_hex TO contents_hex.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> CLEAR: ls_contents_hex, ls_objconthex.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> ENDLOOP.</p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> </p><p style="margin: 0cm 0cm 0pt; text-indent: 36pt" class="MsoNormal"> ENDIF.</p><p style="margin: 0cm 0cm 10pt; text-indent: 36pt" class="MsoNormal"> ENDLOOP.</p><p style="margin: 0cm 0cm 10pt; text-indent: 36pt" class="MsoNormal"> </p><p style="margin: 0cm 0cm 10pt; text-indent: 36pt" class="MsoNormal">"Send attachment in the mail
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
EXPORTING
document_data = document_data
put_in_outbox = put_in_outbox
commit_work = commit_work
IMPORTING
sent_to_all = sent_to_all
new_object_id = new_object_id
TABLES
packing_list = packing_list
object_header = object_header
contents_bin = contents_bin
contents_txt = contents_txt
contents_hex = contents_hex
object_para = object_para
object_parb = object_parb
receivers = receivers
EXCEPTIONS
too_many_receivers = 1
document_not_sent = 2
document_type_not_exist = 3
operation_no_authorization = 4
parameter_error = 5
x_error = 6
enqueue_error = 7
OTHERS = 8.
CASE sy-subrc.
WHEN ok.
WHEN 1.
RAISE too_many_receivers.
WHEN 2.
RAISE document_not_sent .
WHEN 3.
RAISE document_type_not_exist.
WHEN 4.
RAISE operation_no_authorization.
WHEN 5.
RAISE parameter_error.
WHEN 7.
RAISE enqueue_error .
WHEN OTHERS.
RAISE x_error.
ENDCASE.
To read service order(IW32) attachment use business object BUS2088.