如何为质量通知单创建HEX格式附件?已有下载代码求指导
Great question! You're already halfway there with your existing code to fetch HEX content—let's adapt that to attach the file to a quality notification using SAP's Office APIs. Here's a step-by-step breakdown and working example:
Key Background
First, a quick recap: Quality notifications (QMNUM) in SAP are tied to the business object BUS2078. Attaching a file requires two core steps:
- Create the document in SAP Office using
SO_DOCUMENT_INSERT_API1 - Link that SAP Office document to the quality notification business object with
SO_OBJECT_ATTACH_API1
Your existing HEXCONT (a SOLIX type table) is exactly the format SO_DOCUMENT_INSERT_API1 expects for binary content—no extra conversion is needed if you retain this table from your download logic.
Working Code Example
Here's how to extend your current logic to insert and link the attachment:
METHOD INSERT_ATTACHMENT_TO_QNOTIFICATION. DATA: ls_docdata TYPE sofolenti1, lt_contents TYPE TABLE OF solix, ls_doc_key TYPE soodk, ls_bor TYPE sosubi, lv_qmnum TYPE qmnum VALUE 'Q1000001', " Replace with your target QMNUM lv_filename TYPE string VALUE 'invoice.pdf', " Your file name lv_filetype TYPE string VALUE 'PDF'. " File extension " Step 1: Fetch the HEX content (reuse your existing method) CALL METHOD me->get_single_attachment_content EXPORTING file_id = 'YOUR_SOURCE_FILE_ID' " Replace with the file ID you're fetching from IMPORTING ev_return = DATA(lv_xstring). " Optional: If you only have the XSTRING, convert it back to SOLIX table " Skip this if you retained the lt_contents table from your download method CALL FUNCTION 'SCMS_XSTRING_TO_BINARY' EXPORTING buffer = lv_xstring TABLES binary_tab = lt_contents EXCEPTIONS failed = 1 OTHERS = 2. IF sy-subrc <> 0. MESSAGE 'Failed to convert XSTRING to binary format' TYPE 'E' RAISING CONVERSION_ERROR. ENDIF. " Step 2: Populate document metadata for SAP Office ls_docdata-obj_name = 'QM_NOTIF_ATTACHMENT'. " Internal name (can be any string) ls_docdata-obj_descr = lv_filename. " Visible description in attachments ls_docdata-file_ext = lv_filetype. " File extension ls_docdata-doc_size = lines( lt_contents ) * 255. " Calculate total size (SOLIX line = 255 bytes) " Step 3: Insert the document into SAP Office CALL FUNCTION 'SO_DOCUMENT_INSERT_API1' EXPORTING folder_id = CONV soodk( 'INBOX' ) " Use INBOX or a custom folder ID document_data = ls_docdata document_type = 'BINARY' " Use 'TEXT' for plain text files IMPORTING document_id = ls_doc_key TABLES contents_hex = lt_contents EXCEPTIONS folder_not_exist = 1 document_type_not_exist = 2 operation_no_authorization = 3 parameter_error = 4 x_error = 5 OTHERS = 6. IF sy-subrc <> 0. MESSAGE 'Failed to create SAP Office document' TYPE 'E' RAISING DOCUMENT_INSERT_ERROR. ENDIF. " Step 4: Link the document to the Quality Notification ls_bor-objtype = 'BUS2078'. " Business Object Type for Quality Notifications ls_bor-objkey = lv_qmnum. " Your target Quality Notification Number ls_bor-objcat = 'BO'. CALL FUNCTION 'SO_OBJECT_ATTACH_API1' EXPORTING object_id = ls_doc_key bor_object = ls_bor EXCEPTIONS object_not_exist = 1 bor_object_not_exist = 2 operation_no_authorization = 3 parameter_error = 4 x_error = 5 OTHERS = 6. IF sy-subrc <> 0. MESSAGE 'Failed to attach document to Quality Notification' TYPE 'E' RAISING ATTACH_LINK_ERROR. ENDIF. ENDMETHOD.
Critical Notes
- Reuse the SOLIX table: If you modify your
GET_SINGLE_ATTACHMENT_CONTENTmethod to return theHEXCONTtable directly, you can skip the XSTRING-to-SOLIX conversion step entirely—this is more efficient. - Folder ID:
INBOXworks for most cases, but if you need to store attachments in a specific folder, useSO_FOLDER_LIST_API1to fetch the folder ID. - Permissions: Ensure your user has authorization for:
- Creating SAP Office documents (transaction
SO01and authorization objectsS_O_DOC/S_O_FOLD) - Modifying quality notifications (authorization object
QMAUTH)
- Creating SAP Office documents (transaction
- File Types: Use
document_type = 'TEXT'for plain text files (like TXT, CSV) and'BINARY'for all other formats (PDF, Excel, images).
内容的提问来源于stack exchange,提问作者Yanana
相关产品推荐
相关产品推荐

