Skip to Content
Technical Articles
Author's profile photo Akshay Yerawar

Programmatically create and retrieve documents attached in GOS (Generic Object Services) via Stored business document.

Few days back I came across a requirement, to programmatically create and retrieve documents attached in GOS (Generic Object Services) via Stored business object. It took significant amount of time for me to do this. Thought sharing the solution might help someone who may come across this requirement.

I have tried to cover the scenario which I have solved with this. Any suggestions/improvements/modifications in this document are appreciated.

 

Prerequisite:

Business object, Object ID, Document type are the prerequisite for this process. To get these details, refer T-code OAC3.

 

 

A. Existing attachments to the material.

 

 

B. To create attachment:

Following are the function modules required to be executed for attaching the document.

  1. Use FM ARCHIVOBJECT_CREATE_FILE to generate document id.

A different ARCHIV_DOC_ID is generated every time FM is executed.

 

  1. Use FM ARCHIV_CONNECTION_INSERT to attach the document.

 

 

 

C. To fetch attachment:

Following are the function modules required to be executed to get the binary value of the attachment.

  1. Use FM ARCHIVOBJECT_GET_URI to get Archive document id i.e. ARC_DOC_ID.

This FM gives document id of the attachment which is required to be passed in next function module. Pass your Business object in OBJECTTYPE (e.g. BUS1001006 for Material Master, BUS2012 for Purchase Order) & object in OBJECT (e.g. Material code, Purchase order number).

 

 

 

 

  1. Use FM ARCHIVOBJECT_GET_TABLE to get binary value.

This function module takes ARC_DOC_ID as an input & gives the binary value of the attached document.

 

Pass Content repository to the ARCHIVE_ID, Document type to DOCUMENT_TYPE & get ARCHIV_DOC_ID from FM ARCHIVOBJECT_GET_URI.

This will give the binary value for the attachment.

 

If you want to download the file in ECC, you can call method cl_gui_frontend_services=>gui_download. With this method file can be downloaded in given path.

If you want to use this in Ui5 application, you can use FM “SCMS_BINARY_TO_XSTRING“, which will convert binary value to XSTRING value. Ui5 developer can consume this Xstring value through Odata service.

 

Happy Learning!!! 🙂

Assigned Tags

      23 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Pratik Doshi
      Pratik Doshi

      Excellent document akshay. Keep it up.

      Author's profile photo Akshay Yerawar
      Akshay Yerawar
      Blog Post Author

      Thank you Pratik. Appreciate that.

      Author's profile photo Michelle Crapo
      Michelle Crapo

      Interesting.  We don't have any sort of document management . We save documents to a drive on the database server.   OAC3 is not set up. This might be an interesting future project for me.  As to load onto the database server a custom program is used anyway.  Then another customer program to pull back the data for display.  This seems like a better way.

      Michelle

      Author's profile photo Akshay Yerawar
      Akshay Yerawar
      Blog Post Author

      Thanks Michelle. I'm happy that you found it useful.

      Author's profile photo Sayantani Sensharma
      Sayantani Sensharma

      I was searching for a way to achieve this for two days and couldn’t find anything. Thankfully I stumbled upon your blog! This has been a life saver! Thank you ?

      I have an additional requirement to enable deletion of the attached files as well. Can you tell me the FM name for it?

      Author's profile photo Akshay Yerawar
      Akshay Yerawar
      Blog Post Author

      Hi Sayantani,

      Glad to hear this blog helped you.

      For deletion functionality you may use FM "ARCHIVOBJECT_DELETE".

      Author's profile photo Shruti Shah
      Shruti Shah

      Where will you enter download document logic if I want to download document when the user clicks on the attachments option.

      Author's profile photo Akshay Yerawar
      Akshay Yerawar
      Blog Post Author

      Hi Shruti,

      It depends in which application you are implementing the logic.

      If you are implementing this logic in Module pool, you have write logic in PAI.

      If you are implementing this logic in Webdynpro application, you have to write logic on that Action.

      If you are implementing this logic in UI5 application, you have to write logic in respective entity set of the oData service.

      Author's profile photo Naveen Kumar
      Naveen Kumar

      Hi, Than you for posting this blog and is very helpful

      I have a similar requirement, using the Business Object BUS2038, we were posting attachments to non sap server

      While posting attachments in the FM : ARCHIVOBJECT_CREATE_FILE, if we give the path name from the local system, this FM capture the path and saves the attachment to other server

      We were using Sap Odata and Fiori as a front end, in Java Script sending the file path from the local system is obsolete and it's not a Good Idea.

      Could you please let me know, if there is any other Function Module which will imports File Content and generates the Document Id for the Business Object

       

      Thanks & Regards,

      Naveen.

      Author's profile photo Akshay Yerawar
      Akshay Yerawar
      Blog Post Author

      Hello Naveen,

      Glad to hear you found the document useful.

      I'm not getting your point. Can you please explain me why you need other function module?

      Regards,

      Akshay

      Author's profile photo michael spadaro
      michael spadaro

      Hi Akshay,

      I think his requirement is similar to mine. I need to run these FM's in the background so a local directory will not work for me. I need to give ARCHIVOBJECT_CREATE_FILE a server path like '/usr/sap/trans/testfile.txt'.

      Is there a FM that will create file from a SAP server location?

       

      Thanks,
      Mike

      Author's profile photo Akshay Yerawar
      Akshay Yerawar
      Blog Post Author

      Hello Mike,

      Right now I have very limited access to the system so won't be able to explore the things. I suggest get help of your technical resource. He might help you to find alternative FM for this.

      Regards,

      Akshay Yerawar

      Author's profile photo Tushar Subhra Dey
      Tushar Subhra Dey

      ARCHIVOBJECT_CREATE_FILE accepts both application and presentation server file path. You can use either.

      Author's profile photo Marcela Martinez
      Marcela Martinez

      Hi Akshay,

      Thanks a lot for sharing your experience, it is very useful!

      I have a requirement for adding a button to upload/display attachments in a custom development (a Z program). Can you guide me how to apply your solution? I mean... at the beginning you mention that  Business object, Object ID and Document type are the prerequisite for this process and you show in a screenshot 2 BUS objects. In my particular requirement as it is a custom one, how I have to manage this objects?

      Thanks in advance and kind regards!
      Marce.

      Author's profile photo Akshay Yerawar
      Akshay Yerawar
      Blog Post Author

      Hello Marce,

       

      Thanks you Marce. Glad you found it useful.

      Business objects are the one which are SAP system defined. I'm not sure can we create the custom business objects or not.

      For your requirement, I can suggest alternative option which I have used a lot. You can use FM "BAPI_DOCUMENT_CREATE2" to create DMS document. With this, system will create DMS document number, aginst which you can do the single or multiple attachments as per your requirement.

      For this approach, you need to take help of your DMS functional consultant as some configuration are prerequisite for it.

      Please let me know if you need any further help in this.

      Regards,

      Akshay Yerawar

      Author's profile photo Marcela Martinez
      Marcela Martinez

      Hi Akshay,

      Thanks again for your response.

      I created a custom BOR object at SWO1 t-code with my 2 needed fields as a key and also add standard functionality for attaching PDG, JPG, etc documents.

      Comments and post very useful!!

      Kind regards,

      Marce.

      Author's profile photo Akshay Yerawar
      Akshay Yerawar
      Blog Post Author

      Hi Marcela,

      Glad to hear this document helped you to achieve what you ware looking for.

      Regards,

      Akshay

      Author's profile photo Abhishek Banerjee
      Abhishek Banerjee

      Hi Marcela -

      I am having similar requirement here. I have a custom report and I want that report output(PDF) to be attached and stored to OpenText. Now I am curious to know what all artifacts do I need to create when creating custom object-SWO1. How do I call these custom business object for attaching the report PDF and ultimately storing to OpenText. Any help is appreciated.

       

      Thank you.

      Author's profile photo Marce MP
      Marce MP

      Hi Abhishek,

      I did step-by-step what the blog suggests for creating BOR object: Created a custom BOR object at SWO1 t-code with my 2 needed fields as a key. Then in my program I have a Screen where I activated Generic Object Services Toolbar in SAP Objects (GOS toolbar) using Class CL_GOS_MANAGER with:

      Object Type = BOR created object
      Object Key: concatenate fields that you will specified as a Key in BOR object

      Code example for GOS toolbar activation:

        DATAlr_gos_manager TYPE REF TO cl_gos_manager,
              ls_borident    TYPE borident.
      
        ls_borident-objtype <name of BOR object>.
        CONCATENATE field1 field1 fieldn INTO ls_borident-objkey.
      
        CREATE OBJECT lr_gos_manager
          EXPORTING
            is_object      ls_borident
            ip_no_commit   ' '
          EXCEPTIONS
            object_invalid 1.

      And regarding OpenText I'm so sorry but I'm not able to help you.

      Hope this help you.

      Kind regards.

      Author's profile photo Arvind Prasad Barnwal
      Arvind Prasad Barnwal

      This is amazing Akshay, however I have a requirement where I need to refresh the Go_BROWSER Attribute in CL_GOS_ATTACHMENTS class. I dont know how to do that. Actually my Attachment list is not getting refreshed automatically.

      Author's profile photo Akshay Yerawar
      Akshay Yerawar
      Blog Post Author

      Hello Arvind,

      I'm glad you liked it.

      Right now I don't have system access. Will check and let you know once I get the access.

      Regards,

      Akshay Yerawar

      Author's profile photo Raghul Rathi
      Raghul Rathi

      Hi Akshay

      Your content is good, but I have a requirement where I need to update the external document (URL) in the attachment option available in FB02 in BAPI. Can you guide me want need to be updated in the BAPI to attach the external link which is 250 character length.

      Regards

      Raghul

      Author's profile photo Ashim Das
      Ashim Das

      Hi Akshay,

      I have a custom program to call 'BAPI_ACC_DOCUMENT_POST' to post a document. As seen as the document is created by the BAPI, I need to attach an excel using the same program to this document. For this, do I need to add an entry in OAC3? What values need to be passed to the FM 'ARCHIVOBJECT_CREATE_FILE'  in this case?