Skip to Content

Just kidding 😉

I saw a few un-answered questions on SDN on how to attach documents and URLs with the Business Objects, in background and then it should appear in GOS attachment list. So I am trying to understand the basics inside GOS.

[Those un-answered question are years old so I am not sure if GOS is obsolete & now it is only used in Waldorf 😉 ]

However, I did manage to write a program using the inside- logic of GOS, to attach Notes and URLs in background. [ Wow…finally I discovered that the earth is flat & it’s standstill ;-)…any doubts.. ]

Anyway …here we go..

Attaching documents to Business Objects in background / providing a custom GOS like functionality for BSPs:
[ Actually, I pretend that I know BSPs just because I have some experience with JSPs/HTML and..ABAP is…well no… It’s not my mother tongue ;-). So I confidently used to pass suggestions about BSPs as long as I don’t have to write it by myself. ]

The Problem:
Since GOS can only be used with SAPGui Front-end ( OK..may be with ITS as well) and only in foreground, mass-attachment of documents (in background) can’t be handled with GOS. However, the basic applications (Classes & Methods), used in GOS, can be utilized to create a custom program for this purpose.

Use: Since this new program will have the ability to run in background, irrespective of front-end, the same code can also be utilized to provide a GOS like facility in BSPs or while developing an upload program for attaching documents/URLs to Business Objects, in background.

Inside GOS:
Basically, inside the application, main business object and attachment, both are treated as Business Objects and then a link is maintained between both the object instances. The relationship type, while maintaining the link, describes whether the attached object is a URL or a file attachment, note and so on.

However, while the main business object, to which you are trying to attach the document / URL is already known e.g. for Purchase order the Object type (‘BUS2012’ )is known and instance (?) exists in database but the instance for the attachment has to be created first before the linking.

Attachment can be a URL / a Note / a File and so you need to first upload (data as well in case of file) the attahcment info, and in the process also get a business object instance generated of BO type ‘MESSAGE’.

The program
The program-processing will have following steps:

  • Upload the File to be attached or in case of URL just get the URL name. In case of mass upload, the input can be read from a data-file on application server, having info (e.g. URL / File Path ) against the Business Object key ( e.g. Purchase Order Number ).
  • Create an instance of BO type ‘MESSAGE’ using BO Method MESSAGE.Create. In case you are not comfortable with BO macros, simply call the main FM used within. The important part here is to pass the document type e.g. URL, EXT (for external files), and contents of file as well the file type ( e.g. TXT, PDF ) in case of File attachments. Here the document type can be derived from the relationship type of the link.
  • Now, the attachment is created in database as a ‘MESSAGE’ and instance is known. We’ll refer to this as object_b and main Business Object as objet_a.
  • Now, maintain the link.

Check the attached documents through GOS toolbar Attachment List, using main business objects’ transaction e.g. ME23N for Purchase Orders. Sample Code

*---------------------------------------------------------------------** Report  Z_RMTIWARI_ATTACH_DOC_TO_BO*---------------------------------------------------------------------** Written By : Ram Manohar Tiwari* Function   : We need to maintain links between Business Object and*              the attachment.Attachment document is basiclally a*              business object of type 'MESSAGE'.In order to maintain*              links, first the attachment will be crated as Business*              Object of type 'MESSAGE' using Message.Create method.*              Need to check if we can also use FM*              'SO_DOC_INSERT_WITH_ORIG_API1' or SO_OBJECT_INSERT rather*              than using Message.Create method.*---------------------------------------------------------------------*REPORT  Z_RMTIWARI_ATTACH_DOC_TO_BO             .* Include for BO macros  INCLUDE : &ltcntn01&gt.* Load class.  CLASS CL_BINARY_RELATION definition load.  CLASS CL_OBL_OBJECT      definition load.PARAMETERS:*  Object_a   P_BOTYPE LIKE obl_s_pbor-typeid DEFAULT 'ZFRIENDS', " e.g. 'BUS2012'   P_BO_ID  LIKE OBL_S_PBOR-INSTID DEFAULT '00007',    " Key e.g. PO No.*  Object_b   P_DOCTY  LIKE obl_s_pbor-typeid DEFAULT 'MESSAGE' NO-DISPLAY,   P_MSGTYP LIKE SOFM-DOCTP        DEFAULT 'URL'     NO-DISPLAY,*  Relationship   P_RELTYP  LIKE mdoblrel-reltype DEFAULT 'URL'.  types: BEGIN OF TY_MESSAGE_KEY,          FOLTP TYPE SO_FOL_TP,          FOLYR TYPE SO_FOL_YR,          FOLNO TYPE SO_FOL_NO,          DOCTP TYPE SO_DOC_TP,          DOCYR TYPE SO_DOC_YR,          DOCNO TYPE SO_DOC_NO,          FORTP TYPE SO_FOR_TP,          FORYR TYPE SO_FOR_YR,          FORNO TYPE SO_FOR_NO,         END OF TY_MESSAGE_KEY.  DATA : LV_MESSAGE_KEY type TY_MESSAGE_KEY.  DATA : LO_MESSAGE type SWC_OBJECT.  DATA : LT_DOC_CONTENT type standard table of SOLI-LINE with headerline.* First derive the Attachment's ( MESSAGE )document type.  P_DOCTY = 'MESSAGE'.  CASE P_RELTYP.*   In case of URls    WHEN 'URL'.       P_MSGTYP = 'URL'.*   In case of Notes / Private Notes    WHEN 'NOTE' OR 'PNOT'.       P_MSGTYP = 'RAW'.    WHEN 'ATTA'.       P_MSGTYP = 'EXT'.*   Not implemented as yet...exit     EXIT.    WHEN OTHERS.*    ....exit     EXIT.    ENDCASE.*----------------------------------------------------------------** Create an initial instance of BO 'MESSAGE' - to call the* instance-independent method 'Create'.  swc_create_object LO_MESSAGE 'MESSAGE' LV_MESSAGE_KEY.* define container to pass the parameter values to the method call* in next step.  swc_container LT_MESSAGE_CONTAINER.* Populate container with parameters for method  swc_set_element LT_MESSAGE_CONTAINER 'DOCUMENTTITLE' 'Title'.  swc_set_element LT_MESSAGE_CONTAINER 'DOCUMENTLANGU' 'E'.  swc_set_element LT_MESSAGE_CONTAINER 'NO_DIALOG'     'X'.  swc_set_element LT_MESSAGE_CONTAINER 'DOCUMENTNAME' P_DOCTY.  swc_set_element LT_MESSAGE_CONTAINER 'DOCUMENTTYPE'   P_MSGTYP.* 'DocumentContent' is a multi-line element ( itab ).* In case of URLs..it should be concatenated with &KEY& in the begining.  CASE P_MSGTYP.    WHEN 'URL'.      LT_DOC_CONTENT = '&KEY&http://www.rmtiwari.com' .      append LT_DOC_CONTENT.* In case of Notes or Private Notes, get the data from files on appl* server or from wherever(? - remember background).     WHEN 'RAW'.       LT_DOC_CONTENT = 'Hi How r u?' .       append LT_DOC_CONTENT.* In case of File attachments     WHEN 'EXT'.*       Upload the file contents using open dataset in lt_doc_content .*       Some conversion ( Compress ) might be required.*       Not sure at this point  ENDCASE.  swc_set_element LT_MESSAGE_CONTAINER 'DocumentContent' LT_DOC_CONTENT.  swc_call_method LO_MESSAGE 'CREATE' LT_MESSAGE_CONTAINER.* Refresh to get the reference of create 'MESSAGE' object for attachment  swc_refresh_object LO_MESSAGE.* Get Key of new object  swc_get_object_key LO_MESSAGE LV_MESSAGE_KEY.* Now we have attachment as a business object instance. We can now* attach it to our main business object instance.* Create main BO object_a  data: LO_IS_OBJECT_A type SIBFLPORB.  LO_IS_OBJECT_A-INSTID = P_BO_IDLO_IS_OBJECT_A-TYPEID = P_BOTYPELO_IS_OBJECT_A-CATID  = 'BO'.* Create attachment BO object_b  data: LO_IS_OBJECT_B type SIBFLPORB.  LO_IS_OBJECT_B-INSTID = LV_MESSAGE_KEYLO_IS_OBJECT_B-TYPEID = P_DOCTYLO_IS_OBJECT_B-CATID  = 'BO'.*TRY.CALL METHOD CL_BINARY_RELATION=&gtCREATE_LINK  EXPORTING    IS_OBJECT_A            = LO_IS_OBJECT_A*    IP_LOGSYS_A            =    IS_OBJECT_B            = LO_IS_OBJECT_B*    IP_LOGSYS_B            =    IP_RELTYPE             = P_RELTYP*    IP_PROPNAM             =*    I_PROPERTY             =*  IMPORTING*    EP_LINK_ID             =*    EO_PROPERTY            =    .*CATCH CX_OBL_PARAMETER_ERROR .*CATCH CX_OBL_MODEL_ERROR .*CATCH CX_OBL_INTERNAL_ERROR .*ENDTRY.* Check if everything OK...who cares!!  commit work.
To report this post you need to login first.

30 Comments

You must be Logged on to comment or reply to a post.

  1. Mark Briggs
    We had a requirement to attach binary images to maintenance orders using a background job. I managed to use your code and it worked a treat.

    The only small change that was required was to change the line:

    swc_set_element lt_message_container ‘DocumentContent’ lt_doc_content.

    to

    swc_set_table lt_message_container ‘DocumentContent’ lt_doc_content.

    as ‘DocumentContent’ is a multiline attribute.

    Thanks
    Mark Briggs

    (0) 
    1. Ray Ng

      I am trying to make it work by attaching to the business objects, but I have two problems, <br/>1.  The file attached does not carry the right documnet type so when I try to double click from the attachment list, I can only extract it not open the document.<br/>2.  the file attached seems to be correupted.  I could not figured what I did wrong.<br/><br/>Appreciate any help and suggestions.  the following is the test program.<br/><br/><br/>REPORT  ZGOS_BACKGROUD                                .<br/>—-


      <br/>* Function   : We need to maintain links between Business Object and<br/>*              the attachment.Attachment document is basiclally a<br/>*              business object of type ‘MESSAGE’.In order to maintain<br/>*              links, first the attachment will be crated as Business<br/>*              Object of type ‘MESSAGE’ using Message.Create method.<br/>*              Need to check if we can also use FM<br/>*              ‘SO_DOC_INSERT_WITH_ORIG_API1’ or SO_OBJECT_INSERT rather<br/>*              than using Message.Create method.<br/>—-


      <br/><br/>* Include for BO macros<br/>INCLUDE : <CNTN01>.<br/>* Load class.<br/>CLASS CL_BINARY_RELATION DEFINITION LOAD.<br/>CLASS CL_OBL_OBJECT      DEFINITION LOAD.<br/><br/><br/>PARAMETERS:<br/>**  Object_a<br/>   P_BOTYPE LIKE OBL_S_PBOR-TYPEID DEFAULT ‘BUS2080’, ” e.g. ‘BUS2012′<br/>   P_BO_ID  LIKE OBL_S_PBOR-INSTID DEFAULT ‘000300154116’,<br/>   PC_FILE(128) DEFAULT<br/>   ‘/usr/sap/tmp/lam/lamdata/test1.XLS’ LOWER CASE,<br/><br/>*  Object_b<br/>   P_DOCTY LIKE BORIDENT-OBJTYPE DEFAULT ‘MESSAGE’ NO-DISPLAY,<br/>   P_RELTYP LIKE BRELTYP-RELTYPE DEFAULT ‘ATTA’ NO-DISPLAY,<br/>   P_MSGTYP LIKE SOFM-DOCTP DEFAULT ‘EXT’ NO-DISPLAY.<br/><br/>TYPES: BEGIN OF TY_MESSAGE_KEY,<br/>        FOLTP     TYPE SO_FOL_TP,<br/>        FOLYR     TYPE SO_FOL_YR,<br/>        FOLNO     TYPE SO_FOL_NO,<br/>        DOCTP     TYPE SO_DOC_TP,<br/>        DOCYR     TYPE SO_DOC_YR,<br/>        DOCNO     TYPE SO_DOC_NO,<br/>        FORTP     TYPE SO_FOR_TP,<br/>        FORYR     TYPE SO_FOR_YR,<br/>        FORNO     TYPE SO_FOR_NO,<br/>       END OF TY_MESSAGE_KEY.<br/>TYPES : BEGIN OF TY_BINARY,<br/>          BINARY_FIELD(255) TYPE C,<br/>        END OF TY_BINARY.<br/>DATA : LT_BINARY TYPE TABLE OF TY_BINARY WITH HEADER LINE,<br/>       WA_BINARY TYPE TY_BINARY.<br/>DATA : LV_MESSAGE_KEY TYPE TY_MESSAGE_KEY.<br/>DATA : LO_MESSAGE TYPE SWC_OBJECT.<br/>DATA : LT_DOC_CONTENT TYPE STANDARD TABLE OF SOLI-LINE WITH HEADER LINE.<br/>DATA: LISTOBJECT LIKE ABAPLIST   OCCURS  1 WITH HEADER LINE.<br/>DATA: LV_DOC_SIZE TYPE I.<br/>DATA: FILESIZE TYPE SO_DOC_LEN.<br/>DATA: L_FILE_LINES TYPE I.<br/>DATA HELP_OBJCONT LIKE SOLI OCCURS 0 WITH HEADER LINE.<br/>DATA FILE_TYPE    LIKE RLGRAP-FILETYPE.<br/>DATA ACT_FILETYPE LIKE RLGRAP-FILETYPE.<br/>DATA ACT_FILENAME LIKE RLGRAP-FILENAME.<br/>DATA H_FILENAME   LIKE RLGRAP-FILENAME.<br/>DATA DOC_LENGTH   LIKE SOXWD-DOC_LENGTH.<br/>DATA DEF_FILENAME(12).<br/>DATA:FILESTRING TYPE XSTRING.<br/>DATA OBJECT_HD_DISPLAY LIKE SOOD2.<br/><br/>START-OF-SELECTION.<br/>* First derive the Attachment’s ( MESSAGE )document type.<br/>  P_DOCTY = ‘MESSAGE’.<br/>  CASE P_RELTYP.<br/>*   In case of URls<br/>    WHEN ‘URL’.<br/>      P_MSGTYP = ‘URL’.<br/>*   In case of Notes / Private Notes<br/>    WHEN ‘NOTE’ OR ‘PNOT’.<br/>      P_MSGTYP = ‘RAW’.<br/>    WHEN ‘ATTA’.<br/>      P_MSGTYP = ‘EXT’.<br/>    WHEN OTHERS.<br/>      EXIT.<br/>  ENDCASE.<br/>—-


      <br/><br/>* Create an initial instance of BO ‘MESSAGE’ – to call the<br/>* instance-independent method ‘Create’.<br/>  SWC_CREATE_OBJECT LO_MESSAGE ‘MESSAGE’ LV_MESSAGE_KEY.<br/>* define container to pass the parameter values to the method call<br/>* in next step.<br/>  SWC_CONTAINER LT_MESSAGE_CONTAINER.<br/>* Populate container with parameters for method<br/>  SWC_SET_ELEMENT LT_MESSAGE_CONTAINER ‘DOCUMENTTITLE’ ‘Ray Test title’.<br/>  SWC_SET_ELEMENT LT_MESSAGE_CONTAINER ‘DOCUMENTLANGU’ ‘E’.<br/>  SWC_SET_ELEMENT LT_MESSAGE_CONTAINER ‘NO_DIALOG’     ‘X’.<br/>  SWC_SET_ELEMENT LT_MESSAGE_CONTAINER ‘DOCUMENTNAME’   P_DOCTY.<br/>  SWC_SET_ELEMENT LT_MESSAGE_CONTAINER ‘DOCUMENTTYPE’   P_MSGTYP.<br/>  SWC_SET_ELEMENT LT_MESSAGE_CONTAINER ‘FILEEXTENSION’   ‘DOC’.<br/>* ‘DocumentContent’ is a multi-line element ( itab ).<br/><br/>* In case of URLs..it should be concatenated with &KEY& in the begining.<br/>  CASE P_MSGTYP.<br/>    WHEN ‘URL’.<br/>      LT_DOC_CONTENT = ‘&KEY&http://www.rmtiwari.com‘ .<br/>      APPEND LT_DOC_CONTENT.<br/><br/>* In case of Notes or Private Notes, get the data from files on appl<br/>* server or from wherever(? – remember background).<br/>    WHEN ‘RAW’.<br/>      LT_DOC_CONTENT = ‘Hi How r u?’ .<br/>      APPEND LT_DOC_CONTENT.<br/><br/>* In case of File attachments<br/>    WHEN ‘EXT’.<br/>*    open dataset p_fname for input in binarymode.<br/>      OPEN DATASET PC_FILE FOR INPUT IN BINARY MODE.<br/>      READ DATASET PC_FILE INTO FILESTRING.<br/>      CLOSE DATASET PC_FILE.<br/>  ENDCASE.<br/>  CALL FUNCTION ‘SCMS_XSTRING_TO_BINARY'<br/>    EXPORTING<br/>      BUFFER                = FILESTRING<br/>*   APPEND_TO_TABLE       = ‘ ‘<br/>   IMPORTING<br/>     OUTPUT_LENGTH         = LV_DOC_SIZE<br/>    TABLES<br/>      BINARY_TAB            = LT_BINARY<br/>            .<br/>  SWC_SET_TABLE LT_MESSAGE_CONTAINER ‘DocumentContent’ LT_BINARY.<br/>  SWC_SET_ELEMENT LT_MESSAGE_CONTAINER ‘DOCUMENTSIZE’ LV_DOC_SIZE.<br/>  SWC_REFRESH_OBJECT LO_MESSAGE.<br/>  SWC_CALL_METHOD LO_MESSAGE ‘CREATE’ LT_MESSAGE_CONTAINER.<br/>* Refresh to get the reference of create ‘MESSAGE’ object for attachment<br/>*  swc_refresh_object lo_message.<br/>* Get Key of new object<br/>  SWC_GET_OBJECT_KEY LO_MESSAGE LV_MESSAGE_KEY.<br/><br/>* Now we have attachment as a business object instance. We can now<br/>* attach it to our main business object instance.<br/>* Create main BO object_a<br/>  DATA: LO_IS_OBJECT_A TYPE BORIDENT.<br/>  LO_IS_OBJECT_A-OBJKEY = P_BO_ID.<br/>  LO_IS_OBJECT_A-OBJTYPE = P_BOTYPE.<br/>* Create attachment BO object_b<br/>  DATA: LO_IS_OBJECT_B TYPE BORIDENT.<br/>  LO_IS_OBJECT_B-OBJKEY = LV_MESSAGE_KEY.<br/>  LO_IS_OBJECT_B-OBJTYPE = P_DOCTY.<br/><br/>TRY.<br/>*  CALL METHOD cl_binary_relation=>create_link<br/>*    EXPORTING<br/>*      is_object_a = lo_is_object_a<br/>*      is_object_b = lo_is_object_b<br/>*      ip_reltype  = p_reltyp.<br/><br/>  CALL FUNCTION ‘BINARY_RELATION_CREATE'<br/>    EXPORTING<br/>      OBJ_ROLEA    = LO_IS_OBJECT_A<br/>      OBJ_ROLEB    = LO_IS_OBJECT_B<br/>      RELATIONTYPE = P_RELTYP<br/>    EXCEPTIONS<br/>      OTHERS       = 1.<br/><br/>* Check if everything OK…who cares!!<br/>  COMMIT WORK AND WAIT.

      (0) 
  2. Tom Christiaens
    Hello,

    I have been trying to find a BAPI that can read the link  between the OBJECT A (a PM notification for example) and the OBJECT B (the NOTE or URL).
    I have found one: BAPI_REL_GETRELATIONS
    Just enter in the import structure OBJECTID
    the following fields:
    – OBJKEY (e.g. 0001000009)
    – OBJTYPE (for PM notif BUS2038)

    The result is a internal table with the LISTOFRELATIONS where you find all the linked objects. In the same function group BAPI_REL you finbd another BAPI BAPI_REL_CREATERELATION which enables you to create a LINK.

    Conclusion:
    The GOS is using the BINARY RELATIONSHIP framework (whatever that may be) in order to link the NOTES and URL’s to the object (PM notif for example).

    Instead of using the _BINARY_RELATION=>CREATE_LINK,  one can use a BAPI to create a link.

    I tested:
    – Through BAPI_REL_GETRELATIONS I got a list of notes attached to NOTIF 1
    – Through BAPI_REL_CREATERELATION I could attach that same note to NOTIF 2 in background.

    It worked fine…

    (0) 
  3. Tom Christiaens
    Hello,

    I have been trying to find a BAPI that can read the link  between the OBJECT A (a PM notification for example) and the OBJECT B (the NOTE or URL).
    I have found one: BAPI_REL_GETRELATIONS
    Just enter in the import structure OBJECTID
    the following fields:
    – OBJKEY (e.g. 0001000009)
    – OBJTYPE (for PM notif BUS2038)

    The result is a internal table with the LISTOFRELATIONS where you find all the linked objects. In the same function group BAPI_REL you finbd another BAPI BAPI_REL_CREATERELATION which enables you to create a LINK.

    Conclusion:
    The GOS is using the BINARY RELATIONSHIP framework (whatever that may be) in order to link the NOTES and URL’s to the object (PM notif for example).

    Instead of using the _BINARY_RELATION=>CREATE_LINK,  one can use a BAPI to create a link.

    I tested:
    – Through BAPI_REL_GETRELATIONS I got a list of notes attached to NOTIF 1
    – Through BAPI_REL_CREATERELATION I could attach that same note to NOTIF 2 in background.

    It worked fine…

    (0) 
    1. Philippe Gauthier
      Hello,

      I try to understand how is working (I have to write a specification to link in background URL to FI document).

      I don’t arrive to understand which or how data I have to fill into the field
      OBJKEY_B
      OBJTYPE_B
      RELATION

      Could you help me ?

      Best Regards

      Philippe

      (0) 
    2. David Halitsky
      … how I can take a “.bmp” stored as an onject of type “MESSAGE” in the BOR and provide it to a picture control ??????

      My customer has a pressing need to have picture attachments brought into picture controls, rather than displayed in something like MS PicMgr, etc.

      Anyway, thanks very much for tqaking the time to point out that wonderful BAPI.  See this forum thread here:

      Moving a BOR “.bmp” attachment  (objtyp message) to a Picture Control ????

      for the way I’m using it … it’s a gift from the gods.

      Very best regards
      djh

      (0) 
    3. Dharmesh Rathod
      Hi TOM,

      When i create new attachment by reading existing attachment(BAPI_REL_GETRELATIONS ), does it create new document (BAPI_REL_CREATERELATION)on server or it just points to the existing attachment?

      Regards,
      Dharmesh

      (0) 
  4. Seema Chand
    Hello Mr Ram Manohar Tiwari,

    Actually i went through ur Blog in SDN named as ‘GOS'(47)…
    Iam using that code for the attachment…
    Iam getting an output something like-
    FOLTP FOL
    FOLYR 25
    FOLNO 000000000004
    DOCTP EXT
    DOCYR 31
    DOCNO 000000000075
    link id C5E5FBEDCB90B4488A7BF3C4EB870E95
    THIS IS THE LINK… IN WHICH TABLE DOES THIS LINK GETS STORED???
    So that i can have a link between the document attachment and the business object???
    also wht should b the approach if i use a Z-Business Object???
    Or u can even tell me the name of the Function Module through which i can relate them???
    Thank u so much!!!
    waiting…
    Seema Chand.
    U can also E-mail me at seema.chand@vikalpsolutions.com

    (0) 
      1. Seema Chand

        Hi again,        MASK             = ‘,All Files,..'<br/>        TITLE            = ‘Attachment'<br/>      IMPORTING<br/>        FILENAME         = P_FILE<br/>      EXCEPTIONS<br/>        INV_WINSYS       = 1<br/>        NO_BATCH         = 2<br/>        SELECTION_CANCEL = 3<br/>        SELECTION_ERROR  = 4<br/>        OTHERS           = 5.<br/>    IF SY-SUBRC <> 0.<br/>* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO<br/>*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.<br/>    ENDIF.<br/><br/>    CALL FUNCTION ‘GUI_UPLOAD'<br/>         EXPORTING<br/>      FILENAME         =         P_FILE<br/>         TABLES<br/>           DATA_TAB                   = LT_DOC_CONTENT.<br/><br/>    IF SY-SUBRC <> 0.<br/>* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO<br/>*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.<br/>    ENDIF.<br/>ENDCASE.<br/><br/>SWC_SET_ELEMENT LT_MESSAGE_CONTAINER ‘DocumentContent’ LT_DOC_CONTENT.<br/>SWC_SET_TABLE LT_MESSAGE_CONTAINER ‘DocumentContent’ LT_DOC_CONTENT.<br/>SWC_CALL_METHOD LO_MESSAGE ‘CREATE’ LT_MESSAGE_CONTAINER.<br/>SWC_REFRESH_OBJECT LO_MESSAGE.<br/>SWC_GET_OBJECT_KEY LO_MESSAGE LV_MESSAGE_KEY.<br/>DATA: LO_IS_OBJECT_A TYPE SIBFLPORB.<br/>LO_IS_OBJECT_A-INSTID = P_BO_ID.<br/>LO_IS_OBJECT_A-TYPEID = P_BOTYPE.<br/>LO_IS_OBJECT_A-CATID  = ‘BO’.<br/>DATA: LO_IS_OBJECT_B TYPE SIBFLPORB.<br/>LO_IS_OBJECT_B-INSTID = LV_MESSAGE_KEY.<br/>LO_IS_OBJECT_B-TYPEID = P_DOCTY.<br/>LO_IS_OBJECT_B-CATID  = ‘BO’.<br/>CALL METHOD CL_BINARY_RELATION=>CREATE_LINK<br/>  EXPORTING<br/>    IS_OBJECT_A            = LO_IS_OBJECT_A<br/>    IS_OBJECT_B            = LO_IS_OBJECT_B<br/>    IP_RELTYPE             = P_RELTYP.<br/>This is a table in which m storing links wit docno temporarily!!!<br/>ZREL_KEY-DOCNO = LV_MESSAGE_KEY-DOCNO.<br/>ZREL_KEY-LINKID = EP_LINK_ID.<br/>MODIFY ZREL_KEY.<br/>Well how can i allot rewards to the blogs???<br/>Thanks.<br/>Seema.

        (0) 
        1. Ram Manohar Tiwari Post author
          Use commit work at the end.

          Also this will work for Z objects as well.
          You can search “sap gos” in Google & Yahoo and that will help.

          Cheers,
          Ram

          (0) 
    1. Bhavani Somaraju LA
      Hi Seema,
      You can create an attachment by using Function modules also, Check this ocde below, it worked fine and I have done it for Warranty claim business object. You can do this for any other business object.

      Business Object is : BUS2222

      Import parameters

      DEFAULT_FILENAME : Any name for your file.
      FILETYPE : Type of the file cane be ppt, doc. Pdf , txt etc
      PATH_AND_FILE : Exact path like c:\text.txt
      TAR_FOL : This is user folder can be like this ‘FOL25000000000004’
      CLAIMNO : Claim No
      UPDATE_FLAG : Always Null can be removed
      LOGSYSTEM : Logical System

      Tables : has nothing to do here, that is for internal purpose

      function zgos_so_create_attachment .
      *”———————————————————————-
      *”*”Local interface:
      *” IMPORTING
      *” VALUE(DEFAULT_FILENAME) LIKE RLGRAP-FILENAME DEFAULT SPACE
      *” VALUE(FILETYPE) LIKE RLGRAP-FILETYPE DEFAULT ‘DOC’
      *” VALUE(PATH_AND_FILE) LIKE RLGRAP-FILENAME DEFAULT SPACE
      *” VALUE(TAR_FOL) LIKE SOFDK STRUCTURE SOFDK DEFAULT
      *” ‘FOL25000000000004’
      *” VALUE(CLAIMNO) LIKE PNWTYH-CLMNO
      *” VALUE(UPDATE_FLAG) LIKE SONV-FLAG OPTIONAL
      *” VALUE(LOGSYSTEM) LIKE BORIDENT-LOGSYS DEFAULT ‘DV3200’
      *” EXPORTING
      *” VALUE(FILELENGTH) LIKE SOXWD-DOC_LENGTH
      *” VALUE(F_CANCELLED) LIKE SONV-FLAG
      *” VALUE(ACT_FILETYPE) LIKE RLGRAP-FILETYPE
      *” VALUE(ACT_FILENAME) LIKE RLGRAP-FILENAME
      *” VALUE(ACT_OBJTYPE) LIKE SOODK-OBJTP
      *” VALUE(FILE_PUT_TO_KPRO) LIKE SONV-FLAG
      *” TABLES
      *” OBJECTCONT STRUCTURE SOLI OPTIONAL
      *” EXCEPTIONS
      *” FILE_READ_ERROR
      *” INVALID_TYPE
      *” X_ERROR
      *” OBJECT_TYPE_NOT_ALLOWED
      *” KPRO_INSERT_ERROR
      *” SUCCESS_MESSAGE
      *” LOGSYS_NOTDEFINED
      *” OBJECT_INSERT_ERROR
      *”———————————————————————-
      **=====================================================================
      * Program Name : ZGOS_SO_CREATE_ATTACHMENT
      *
      * Version : 1.00
      *
      * Author : S L A Bhavani
      *
      * Company : Infotech Enterprises Ltd, Hyderabad
      *
      * Date Written : 08/03/2004
      * Purpose : Create attachment to warranty claim object.
      * : Attachment can be any type (TXT, Doc etc)
      *
      * Input : File Name, File Type, Path an File Name,Claim No
      *
      *———————————————————————

      ******————–decalre constants ———————**********

      constants: c_objnam(7) value ‘MESSAGE’, ” for attachment type
      c_reltype(4) value ‘ATTA’, ” relation type
      c_objtype(3) value ‘EXT’, ” attachment object type
      c_busobjtype(7) value ‘BUS2222’, “Business object type
      c_tarfol(17) value ‘FOL25000000000004’,
      c_objla(2) value ‘EN’.

      ******————–End decalre constants ——————**********

      ******————–decalre variables ——————**********

      data: command_line like rlgrap-filename, “this is to get the path
      trunc_length type i, ” file lenght
      cancelled, “flag to know whther cancelled the operation
      stripped_name like rlgrap-filename, “final name of the file
      file_path like rlgrap-filename. ” total file path
      **** KPro variables
      data: filesize like sood-objlen,
      sizelimit like sood-objlen,
      loio_object like sdokobject,
      phio_object like sdokobject,
      new_object_id_phio like sdokobject,
      put_to_kpro like sonv-flag,
      context like sdokpropty occurs 0,
      existing_kpro_doc like sonv-flag,
      content_lines like sy-tabix.

      data screentype(30) type c.

      *————–decalre internal tables—————–**********

      *————- USED IN SO OBJECT INSERT—————-**********
      data: objhead like soli occurs 0 with header line.

      data: folder_id_api1 like soobjinfi1-object_id,
      doc_insert_api1 like sodocchgi1,
      document_type like soodk-objtp,
      document_info like sofolenti1.

      data: object_hd_change like sood1,
      object_type like soodk-objtp,
      objpara like selc occurs 0 with header line,
      objparb like soop1 occurs 0 with header line.
      data: objcont like soli occurs 1 with header line,
      document_id like tar_fol.
      .
      *—–used in create binary relation commit——**********

      data: v_pnguid like pnwtyh-pnguid.
      data: v_objtype like borident-objtype.
      data: obj_rolea like borident occurs 0 with header line,
      obj_roleb like borident occurs 0 with header line.
      ******—–used in create binary relation commit——**********

      ******—–Function Module calls———————-**********

      act_filename = path_and_file.
      if not act_filename is initial.
      * path and file is already known
      act_filetype = filetype.
      * commit work.
      * wait up to 5 seconds.
      ** get file extension from file name
      perform so_split_file_and_extension using act_filename
      stripped_name
      act_objtype.
      * commit work.
      * wait up to 5 seconds.
      *
      * check (and change) objtype and filetype
      perform check_object_type tables objcont
      using act_objtype
      act_filetype
      trunc_length.
      * commit work.
      * wait up to 5 seconds.
      *
      * Decide if a new document should be created
      describe table objectcont lines content_lines.
      if content_lines ne 0.
      call function ‘SO_KPRO_DATA_FROM_OBJCONT_GET’
      importing
      loio_object = loio_object
      tables
      objcont = objectcont
      context = context
      exceptions
      missing_kpro_data = 1
      others = 2.
      if sy-subrc = 0.
      existing_kpro_doc = on.
      else.
      existing_kpro_doc = off.
      endif.
      else.
      existing_kpro_doc = off.
      endif.
      if existing_kpro_doc = off.
      * * Create new document (in KPro or DB)
      * Decide about KPro “It’s on the PC so it’s an external document
      call function ‘SO_KPRO_DECIDE’
      exporting
      objtp = c_objtype
      importing
      put_into_kpro = put_to_kpro.
      if put_to_kpro = on.
      * commit work.
      * wait up to 5 seconds.
      * .

      * * Put file into KPro
      perform loio_content_insert(saplso25)
      using act_filename
      act_filetype
      loio_object
      filesize
      rcode.
      if rcode ne 0.
      if rcode = 8000.
      f_cancelled = on.
      exit.
      endif.
      raise kpro_insert_error.
      endif.
      *– Export file size
      filelength = filesize.
      *– Export extct
      file_put_to_kpro = on.
      * commit work.
      * wait up to 5 seconds.
      *
      *– Put new LOIO id to OBJCONT
      call function ‘SO_KPRO_DATA_INTO_OBJCONT_PUT’
      exporting
      loio_object = loio_object
      tables
      objcont = objcont.
      else.
      *– Put document into database in classical manner

      endif.
      if act_filetype = ‘TRU’.
      act_filetype = ‘ASC’.
      endif.

      else.
      endif.
      else.
      *– If cancelled, UPLOAD will return cancel = ‘x’.
      if not cancelled is initial.
      cancelled = on.
      endif.
      f_cancelled = cancelled.
      endif.
      commit work.
      * if update_flag ne ‘X’.
      *– Insert object into Sap Object Definition Database and create
      *– document as neighbor.

      *– Put new LOIO id to OBJCONT
      refresh objcont.
      append loio_object to objcont.
      object_type = c_objtype.

      *– Document attributes
      clear object_hd_change.
      *– IT IS ATTACHMENT ALWAYS SO.. OBJTYPE = MESSAGE
      object_hd_change-objnam = c_objnam.
      concatenate file_path sy-datum sy-uzeit
      into object_hd_change-objdes
      separated by space.
      object_hd_change-objla = c_objla.
      object_hd_change-objsns = ‘O’.
      *– DETERMINE FILE EXTENSION BASED ON THE FILE TYEP.
      object_hd_change-file_ext = filetype.
      object_hd_change-extct = ‘K’.

      object_hd_change-objlen = filesize.
      object_hd_change-objdes = default_filename.

      *** Retrieve the root target folder
      call function ‘SO_FOLDER_ROOT_ID_GET’
      exporting
      owner = sy-uname
      region = ‘B’
      importing
      folder_id = tar_fol.
      .
      if sy-subrc <> 0.
      * * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
      * * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      endif.
      if tar_fol = ” .
      tar_fol = c_tarfol.
      endif.

      call function ‘SO_OBJECT_INSERT’
      exporting
      folder_id = tar_fol
      object_hd_change = object_hd_change
      object_type = object_type
      owner = sy-uname
      importing
      object_id = document_id
      tables
      objcont = objcont
      objhead = objhead
      objpara = objpara
      objparb = objparb
      exceptions
      active_user_not_exist = 1
      communication_failure = 2
      component_not_available = 3
      dl_name_exist = 4
      folder_not_exist = 5
      folder_no_authorization = 6
      object_type_not_exist = 7
      operation_no_authorization = 8
      owner_not_exist = 9
      parameter_error = 10
      substitute_not_active = 11
      substitute_not_defined = 12
      system_failure = 13
      x_error = 14
      others = 15.
      if sy-subrc <> 0.
      raise object_insert_error.
      else.

      v_objtype = c_busobjtype.
      *– get the application object id from the claim no.
      select single pnguid from pnwtyh into v_pnguid where
      clmno = claimno and ( oldcn = claimno or oldcn = ” ).
      *– Preapre application object role
      obj_rolea-objtype = v_objtype.
      obj_rolea-objkey = v_pnguid.
      concatenate tar_fol document_id into obj_roleb-objkey.
      obj_roleb-objtype = c_objnam. “MESSAGE

      *– Get Logical system from claint
      call function ‘OWN_LOGICAL_SYSTEM_GET’
      importing
      own_logical_system = logsystem
      exceptions
      own_logical_system_not_defined = 1
      others = 2.
      if sy-subrc <> 0.
      raise logsys_notdefined.

      endif.

      obj_rolea-logsys = logsystem.
      append obj_rolea.

      *– preapare SAp Office object role.
      obj_roleb-logsys = logsystem. ” DV3200
      append obj_roleb.
      **– create binary relation for application object and sap office object
      call function ‘BINARY_RELATION_CREATE_COMMIT’
      exporting
      obj_rolea = obj_rolea
      obj_roleb = obj_roleb
      relationtype = c_reltype.

      endif.

      * endif. “Update flag
      endfunction.

      (0) 
  5. srushti srushti
    Steps Followed to Add attachment in Web dynpro Java + R/3 Code from web dynpro application passing file content as “xtring”  parameter to RFC then
    1)Convert Xstring to bin using 
                  SCMS_XSTRING_TO_BINARY
    2)BINARY_RELATION_CREATE Create relations as 
               attachment to network object
    This part of upload file works fine .This is   by refreing you block !!!!!!!!

    ++++++++++++++++++++++++++++++++++++++++++++++++
    Problem is when we do get attachment file contents get corrupted.
    1) CL_BINARY_RELATION=>READ_LINKS
    2) SO_DOCUMENT_READ_API1 gives document data
    3) convert document data using 
       SCMS_BINARY_TO_XSTRING
    4) pass that xstring as file data to front end

    While getting data file format gets corrupt .
    Is there any other way i can read attachment ?
    Is any step missing here in reading attachments?

    (0) 
  6. Anoop Dosi
    Hi Guys,
    I need to upload JPG images as attachment from presentation server using this program. Could you please help me in that.

    Thanks,
    Anoop

    (0) 
    1. Anoop Dosi
      Hi guys, sorted out the problem. This program was working fine in foreground also and like many others, I also faced the problem at the time of opening attachment it was saying file corrupted. I tried the ways which other used but those were not working in my case. My problem is been sorted out by this way.

      Just thought of sharing so that in future other guys can give the try to this solution if attachment doesn’t open.
      First change in program: I used structure SOLIX
      * Binary contents
      DATA : lt_doc TYPE STANDARD TABLE OF solix WITH HEADER LINE.
      Second Change: file from application server and doctype is the file extension.
        SPLIT lf_fname AT ‘.’ INTO xfname xdoctype.
            TRANSLATE xdoctype TO UPPER CASE.
            swc_set_element lt_message_container ‘DOCUMENTTYPE’ xdoctype.
            CALL FUNCTION ‘GUI_UPLOAD’
              EXPORTING
                filename                = lf_fname
                filetype                = ‘BIN’
              IMPORTING
                filelength              = length
              TABLES
                data_tab                = lt_doc.
      Third change: Instead of “DocumentContent” used “Content_hex” method parameter

      swc_set_table lt_message_container ‘Content_Hex’ lt_doc.

      And with this it worked fine for BMP JPG PDF and DOC all type of attachment.
      Thanks,
      Anoop

      (0) 
      1. Dadarao Sopan Padghane
        Hi Anoop,

        I was trying to upload the PDF file. I was able to saw the PDF file after reffering the code by you along with Manoj . But the problem i am not ableto dis the attachment.
        Can you pls. guide what might be the problem?

        thanks,
        Dadarao

        (0) 
      2. Subba Krishna
        Hello Anoop,

        i have the same requirement to upload gif on Local PCto invoice documents. could you please send me the latest code.

        Thanks,
        Subba

        (0) 
      3. ramanan rs
        Hi Anoop,

        I am working on attaching a file to nomination header in TSW. The code given by you is attaching the file to the header and am able to see it in the list of objects. But when i try to open the file from the list of objects the file is empty. The file which i am uploading is with the extension ‘mht’.  Do i need to make any conversion before uploading? Pls help..

        (0) 
  7. Pushkarinee Date
    Hello,

    Is it possible to attach URL more than 255 in length ? …I tried but it doesn’t seem to work ..I will be very thankful if you have any idea about it

    Thanks
    Pushkarinee

    (0) 
    1. Ram Manohar Tiwari Post author
      just in case you face the 255 char limit issue the code below will help..never tested it but I think it should work.

      data l_url_id   type so_url.
        l_url_id   = “assign the url id > 255 char

      WHEN ‘URL’.
        while not l_url_id is initial.
          concatenate ‘&KEY&’ l_url_id(250) into LT_DOC_CONTENT.
          append LT_DOC_CONTENT.
          shift l_url_id left by 250 places.
        endwhile.

      don’t forget to replace the line below

      swc_set_element LT_MESSAGE_CONTAINER ‘DocumentContent’ LT_DOC_CONTENT.

      with
      swc_set_table LT_MESSAGE_CONTAINER ‘DocumentContent’ LT_DOC_CONTENT.

      cheers,
      Ram

      (0) 
  8. Prabavathi Kalaimani
    Hi,

    I have attachment stored in sales order using GOS. I have a requirement to read the attachment and move it to an external file path. Please help me regarding this issue.

    Thanks
    Praba

    (0) 
  9. ahmet sevil
    Hi,

    I would like to find the attachment of a GOS(Generic Object Service) but I will not view attachment.

    Is there a function module like this?

    CALL FUNCTION ‘…..’
    ….
    Exporting
    attachmet = true / false

    If there is attachment, I will give it icon in the ALV report.

    Thanks, best regards.

    (0) 
  10. IBABAP IBABAP

    Dear Ram,

    First of all thank you very much for such a useful code. I tried the code and did the attachment for FI documents showing in FB03 where OBJTYPE = ‘BKPF’ and OBJKEY = ‘combination of company code, document no and fiscal year’.

    We are getting documents created from another .net application, which is creating .bmp, .jpg, .doc and .pdf documents. All documents are uploaded successfully except few PDF documents.

    The difference between the 2 PDFs, first one is a scanned images as PDF which is uploaded successfully, second one is the PDF having table format which was created by .net application with the help of HTML coding.

    I am facing the problem when uploading the second PDF file, it is uploading successfully but when I am opening it, it is corrupted. I took the PDF from AL11, when I saw the PDF after downloading it from AL11, PDF is opening finely.

    Is there two different ways to upload the PDF with scanned images and PDF with table format created by HTML format ?

    Please help.

    Regards,
    Prameet Gopal Verma

    (0) 
  11. Paulo Cabral

    Follow the same code indented.

    report  z_rmtiwari_attach_doc_to_bo.
    * Include for BO macros
    include : <cntn01>.
    * Load class
    class cl_binary_relation definition load.
    class cl_obl_object      definition load.
    parameters:
    *  Object_a
      p_botype like obl_s_pbor-typeid default 'ZFRIENDS', " e.g. 'BUS2012'
      p_bo_id  like obl_s_pbor-instid default '00007',    " Key e. g.  PO No.
    *  Object_b
      p_docty  like obl_s_pbor-typeid default 'MESSAGE' no-display,
      p_msgtyp like sofm-doctp default 'URL' no-display,
    *  Relationship
      p_reltyp like mdoblrel-reltype default 'URL'.
    types: begin of ty_message_key,
             foltp type so_fol_tp,
             folyr type so_fol_yr,
             folno type so_fol_no,
             doctp type so_doc_tp,
             docyr type so_doc_yr,
             docno type so_doc_no,
             fortp type so_for_tp,
             foryr type so_for_yr,
             forno type so_for_no,
           end of ty_message_key.
    data : lv_message_key type ty_message_key.
    data : lo_message type swc_object.
    data : lt_doc_content type standard table of soli-line with header line.
    * First derive the Attachment's ( MESSAGE )document type.
    
    p_docty = 'MESSAGE'.
    
    case p_reltyp.
    *   In case of URls
      when 'URL'.
        p_msgtyp = 'URL'.
    *   In case of Notes / Private Notes
      when 'NOTE' or 'PNOT'.
        p_msgtyp = 'RAW'.
      when 'ATTA'.
        p_msgtyp = 'EXT'.
    *   Not implemented as yet. . exit
        exit.
      when others.
    *    . .. . exit
        exit.
    endcase.
    *----------------------------------------------------------------*
    * Create an initial instance of BO 'MESSAGE' - to call the
    * instance-independent method 'Create'.
    swc_create_object lo_message 'MESSAGE' lv_message_key.
    * define container to pass the parameter values to the method call
    * in next step.
    swc_container lt_message_container.
    * Populate container with parameters for method
    swc_set_element lt_message_container 'DOCUMENTTITLE' 'Title'.
    swc_set_element lt_message_container 'DOCUMENTLANGU' 'E'.
    swc_set_element lt_message_container 'NO_DIALOG'     'X'.
    swc_set_element lt_message_container 'DOCUMENTNAME' p_docty.
    swc_set_element lt_message_container 'DOCUMENTTYPE'   p_msgtyp.
    * 'DocumentContent' is a multi-line element ( itab ).
    * In case of URLs. it should be concatenated with &KEY& in the begining.
    case p_msgtyp.
      when 'URL'.
        lt_doc_content = '&KEY&http://www.rmtiwari.com' .
        append lt_doc_content.
    * In case of Notes or Private Notes, get the data from files on appl* server or from wherever(? - remember background).
      when 'RAW'.
        lt_doc_content = 'Hi How r u?' .
        append lt_doc_content.
    * In case of File attachments
      when 'EXT'.
    *       Upload the file contents using open dataset in lt_doc_content .
    *       Some conversion ( Compress ) might be required.
    *       Not sure at this point
    ENDCASE.
        swc_set_element lt_message_container 'DocumentContent' lt_doc_content.
        swc_call_method lo_message 'CREATE' lt_message_container.
    * Refresh to get the reference of create 'MESSAGE' object for attachment
        swc_refresh_object lo_message.
    * Get Key of new object
        swc_get_object_key lo_message lv_message_key.
    * Now we have attachment as a business object instance.
    * We can now* attach it to our main business object instance.
    * Create main BO object_a
        data: lo_is_object_a type sibflporb.
        lo_is_object_a-instid = p_bo_id.
        lo_is_object_a-typeid = p_botype.
        lo_is_object_a-catid  = 'BO'.
    * Create attachment BO object_b
        data: lo_is_object_b type sibflporb.
        lo_is_object_b-instid = lv_message_key.
        lo_is_object_b-typeid = p_docty.
        lo_is_object_b-catid  = 'BO'.
        try.
            call method cl_binary_relation=>create_link
              exporting
                is_object_a = lo_is_object_a
    *           IP_LOGSYS_A =
                is_object_b = lo_is_object_b
    *           IP_LOGSYS_B =
                ip_reltype  = p_reltyp.
    *       IP_PROPNAM  =
    *       I_PROPERTY  =
    *  IMPORTING
    *       EP_LINK_ID  =
    *       EO_PROPERTY = .
          catch cx_obl_parameter_error .
          catch cx_obl_model_error .
          catch cx_obl_internal_error .
        endtry.
    * Check if everything OK.
        .
    
    *who cares!!
        commit work.
    (0) 

Leave a Reply