hexattachmentabapsap-basis

Create HEX attachment for Quality Notification


I have this piece of code to download the hex content of a file with import parameter file id. I want to insert a new attachment for a notification, but I don't know how to get started.

  METHOD GET_SINGLE_ATTACHMENT_CONTENT.

    " VARIABLES
    DATA: HEXCONT       TYPE TABLE OF SOLIX.
    DATA: DOCDATA       TYPE SOFOLENTI1.
    DATA: LV_LENGTH     TYPE I.

    " CHECK TO CONTINUE FUNCTION MODULE
    IF FILE_ID IS INITIAL. "type = SOFOLENTI1-DOC_ID.
      MESSAGE 'Document ID is empty.' TYPE 'E' RAISING DOC_ID_EMPTY.
    ENDIF.

    " GET BINARY CONTENT OF FILE
    CALL FUNCTION 'SO_DOCUMENT_READ_API1'
      EXPORTING
        DOCUMENT_ID    = FILE_ID
      IMPORTING
        DOCUMENT_DATA  = DOCDATA
      TABLES
        CONTENTS_HEX   = HEXCONT.

    IF SY-SUBRC <> 0.
      MESSAGE 'Error downloading file.' TYPE 'E' RAISING FILE_DOWNLOAD_ERROR.
    ENDIF.

    " CONVERT TO XSTRING
    LV_LENGTH = DOCDATA-DOC_SIZE.

    CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
      EXPORTING
        INPUT_LENGTH = LV_LENGTH
      IMPORTING
        BUFFER       = EV_RETURN "type XSTRING
      TABLES
        BINARY_TAB   = HEXCONT.

     IF SY-SUBRC <> 0.
      MESSAGE 'Error downloading file.' TYPE 'E' RAISING FILE_DOWNLOAD_ERROR.
    ENDIF.
  ENDMETHOD.

I've read about function modules like 'SO_DOCUMENT_INSERT_API1' and it has the file information but not the file content (preferably hex content). Any idea on how to get started with this?


Solution

  • Maybe you can do something with the approach I used. Check this example below.

    1. Start with parsing your data to hex
    2. Insert the document with so_document_insert_api1
    3. Link it to your notification with the fm binary_relation_create


      " TYPES
      TYPES: BEGIN OF TYPE_FILE,
               NAME    TYPE STRING,
               TYPE    TYPE STRING,
               QMNUM   TYPE QMNUM,
               USER    TYPE CHAR64,
               HEXCONT TYPE XSTRING,
             END OF TYPE_FILE.
      
      
      " VARIABLES AND OBJECTS 
      DATA: LT_RETURN             TYPE TABLE OF BAPIRET2.
      DATA: LT_HEXCONT            TYPE TABLE OF SOLIX.
      DATA: LS_DOCDATA            TYPE SODOCCHGI1.
      DATA: LS_DOCINFO            TYPE SOFOLENTI1.
      DATA: LS_FILE               TYPE TYPE_FILE.
      DATA: LV_DOCTYPE            TYPE SOODK-OBJTP.
      DATA: LV_FOLDER_ID          TYPE SOOBJINFI1-OBJECT_ID.
      DATA: LV_LENGTH             TYPE I.
      
      DATA: OBJ_NOTIF             TYPE BORIDENT.
      DATA: OBJ_ATTACH            TYPE BORIDENT.
      
      DATA: P_QMNUM               TYPE QMNUM.
      
      LV_FOLDER_ID = 'FOL40000000000004'.
      
      " CONVERT HEX CONTENT TO BINARY  
      CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
        EXPORTING
          BUFFER        = LS_FILE-HEXCONT
        IMPORTING
          OUTPUT_LENGTH = LV_LENGTH
        TABLES
          BINARY_TAB    = LT_HEXCONT.
      
      " SET VALUES  
      LS_DOCDATA-OBJ_DESCR = LS_FILE-NAME.
      LS_DOCDATA-OBJ_LANGU = 'E'.
      LS_DOCDATA-OBJ_NAME  = 'MESSAGE'.
      LS_DOCDATA-DOC_SIZE = XSTRLEN( LS_FILE-HEXCONT ).
      
      LV_DOCTYPE = LS_FILE-TYPE.
      
      UNPACK LS_FILE-QMNUM TO P_QMNUM.
      
      
      " CREATE ATTACHMENT  
      CALL FUNCTION 'SO_DOCUMENT_INSERT_API1'
        EXPORTING
          FOLDER_ID     = LV_FOLDER_ID
          DOCUMENT_DATA = LS_DOCDATA
          DOCUMENT_TYPE = LV_DOCTYPE
        IMPORTING
          DOCUMENT_INFO = LS_DOCINFO
        TABLES
          CONTENTS_HEX  = LT_HEXCONT.
      
      
      " CREATE LINK TO OBJECT (Notification)
      OBJ_NOTIF-OBJKEY   = P_QMNUM.
      OBJ_NOTIF-OBJTYPE  = 'BUS2038'.
      
      OBJ_ATTACH-OBJKEY  = LS_DOCINFO-DOC_ID.
      OBJ_ATTACH-OBJTYPE = 'MESSAGE'.
      
      CALL FUNCTION 'BINARY_RELATION_CREATE'
        EXPORTING
          OBJ_ROLEA    = OBJ_NOTIF
          OBJ_ROLEB    = OBJ_ATTACH
          RELATIONTYPE = 'ATTA'.
      
      
      " COMMIT 
      COMMIT WORK.