Attach Files to SAP Business Documents Using GOS – ABAP

In this article, we will see how to attach files to SAP business documents (objects) using Generic Object Services (GOS). First, we will understand the concept of GOS, then, how to use the GOS toolbox in GUI to attach files. Afterward, we’ll learn to implement an ABAP logic to attach a file interfaced through SAP Process Orchestration (SAP PI/PO) or SAP Cloud Platform Integration (CPI) from an external system. Finally, we will implement a custom ABAP program that can attach any type of file uploaded from your local PC to any SAP object.

If your requirement is to use ArchiveLink to store files, You can refer to the following articles,

Generic Object Services – GOS

Generic Object Services are a collection of common functionalities provided by SAP that business objects can reuse without having to implement ABAP codes. For example, GOS includes functionalities such as attaching a PDF to a document, entering a note, sending a document, starting a workflow, etc. The collection of these common functionalities provided by GOS is bundled together in the GOS toolbox for GUI mode use.

These common functionalities allow developers and SAP consultants to add them to any SAP business object with the minimum development effort.

GOS tool box with functionalities shown in SAP business document GUI
GOS Tool-Box in SAP GUI

In the screenshot above you can see the GOS toolbox and its function buttons displayed for SAP business object “credit memos”. What we will focus on in this article is the “create attachment” function of GOS.


How to Attach Files in GUI Using GOS

When the GOS is activated for an SAP business document type, you will be able to see the GOS toolbox from transactions related to that business object. For example, if GOS has been activated for sales orders, sales order change transaction VA02 and sales order display transaction VA03 will have the GOS toolbox.

Similarly, you can use the GOS toolbox for other business documents, such as Purchase Orders, Purchase Requisitions, Invoices, Accounting Documents, and even master data, such as Business Partner, Material Master, etc.

To attach a document, go to the desired transaction and click on the GOS toolbox, then click on “create” and then on “create attachment“. When a pop up appears, select the file location and click “ok”.

Attach file to a SAP business document in GUI mode using the toolbox
GOS create attachment in GUI mode

To view a list of files or notes attached to the object, use the option “Attachment List” on the toolbox.

GOS toolbox showing the attachment list
Attachment list of an SAP business document

Using GOS in Integration Scenarios

Let us assume a business scenario where Process Orchestration (SAP PI/PO) or Cloud Platform Integration (CPI) extracts files from an external sFTP location and routes them to SAP S4 HANA. In S4 HANA side, an ABAP program then attaches these files to corresponding business documents.

Following are a few examples of this type of integration scenario:

  • Attaching invoice PDF to SAP sales order
  • Attaching a picking list to SAP delivery note
  • Linking digital signature to SAP invoice

SAP middleware fetches the files and encodes them in base64 format. On PI/PO side, you can use a User-Defined Function or a Java Mapping to encode file content to base64. Then the middleware routes the encoded message in base64 to SAP Application Integration Framework (AIF). The base64 format of the content should be decoded and attached to the SAP business document.

scenario to illustrate the external file attachment to SAP business document. Files routed from FTP server to S4 HANA via PI/PO.
External file attachment to SAP document business scenario

File Attachment ABAP Program Flow in AIF and S4 HANA

Now we will look at the data processing logic in SAP S4 HANA and AIF. That is, how to design and implement the ABAP code necessary to process the base64-encoded file and attach it to the SAP object.

ABAP program logic to attach a file to a SAP business document. processing logic in ABAP.
ABAP program logic to attach an interfaced file to an SAP document

Use the following FMs in the given sequence to convert the base64-encoded content and attach the file to the SAP object

  1. The first step of the processing is to decode the base64 format to XString format. XString is a predefined ABAP internal data type that represents the file content in a byte format. We can use the FM SCMS_BASE64_DECODE_STR for this purpose.
  2. The second step is to convert the XString format of the file to BIN format. FM SCMS_XSTRING_TO_BINARY allows us to convert XString format to BIN format.
  3. The third step is to convert the BIN format to SO uni-code format using FM SO_CONVERT_CONTENTS_BIN.
  4. The forth step of the logic is to create the address where your file is saved. For this we will use FM SO_FOLDER_ROOT_ID_GET.
  5. Next step is to insert the file content to the address (folder ID) created in the previous step. Use the FM SO_OBJECT_INSERT for this purpose.
  6. Finally, call the FM BINARY_RELATION_CREATE_COMMIT to link the file attached with the SAP business object/document.
ABAP FMs to use to attach a file to SAP document in SAP S4 HANA.
FM flow of the ABAP logic in S4 HANA

Custom ABAP Program to Attach Files to SAP Documents

I was able to write a custom ABAP that showcases how to use some of the FMs I have described in the previous chapter. The purpose of the ABAP program is to attach any type of file uploaded from the local PC to an SAP business document, such as Sales Order, Delivery, Invoice, Account Document, etc.

You can specify the SAP document you would like to attach the file to using two selection parameters:

  • Business Object Type – This corresponds to the SAP business object type. For example, BKPF for accounting documents, VBRK for invoices, VBAK for sales orders, LIKP for deliveries.
  • Document number – The SAP business object document number.

SAP business object type and corresponding document number should match in order for the program to work as expected.

selection screen of the ABAP program designed to attach a file uploaded from PC to SAP business document.
Selection screen of the custom ABAP program – attach a file

ABAP Code to Upload and Attach Files

Main Program

Include


To summarize, we talked about the concept of GOS and GOS functionality in GUI mode in the first chapters. Next, we discussed how to design an ABAP logic that can process and attach files transferred via an interface. Then we looked at the FMs needed to decode base64 format and attach files to a business document. Finally, we implemented an ABAP program that can upload files from local PC and attach them to SAP business documents.

If you have any questions or comments, please post them below.

TECH GADGETS I USE EVERY DAY

These are some of the tech gadgets I use every day. If you make a purchase through these links I will earn a small comission at absolutely no extra cost to you.

5 thoughts on “Attach Files to SAP Business Documents Using GOS – ABAP

  1. vipin saraika says:

    ABAP program logic to attach an interfaced file to an SAP document
    I have followed all the 6 steps to receive the XML payload from SuccessFactors and store it in SAP HR, requirement is to receive a JPG photo in decoded base64 format via CPI as XML payload and then store this employee photo in business object ‘PREL’.
    I have followed all the 6 steps mentioned by you for interfacing the document and tried running I don’t get any error in any of the function modules but the photo is also not saving in the business objet PREL can please someone help?

    • PallaviG says:

      I had similar issue when I tried above code, problem seems to be when the objkey is complete.
      Here the example was for SD invoice, VBRK unique key is document number.
      Though in my case, the value of object key was Document number+Document year. When passed, the program worked as expected.
      FM BINARY_RELATION_CREATE_COMMIT, Exporting param obj_rolea-objkey should be complete key. Try this,

  2. yoshit says:

    Hi Isuru,

    The attachment doesn’t work when trying the BKPF version of the code,
    However, the error message doesn’t give any feedback at all to point out what’s wrong.

    I think I’m using the wrong object type or key, but i have no way of identifying the error without an error message to start with.

    can ask if there is something you can add to put me into the right direction?

  3. Yoshi says:

    Hi Isuru,

    Thanks for this, it save me tons of hours for research, trial and error.
    but I just need to confirm, this only works for S4 right?

    My test program in R3 isnt attaching anything while the one in the S4 system doing ok.

Leave a Reply

Your email address will not be published. Required fields are marked *