Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Sergio-Gracia
Explorer
4,031

Introduction

In this blog, we describe a method to attach document files to a Journal Entry (BKPF) instance within the same SAP S/4HANA Cloud Public Edition using ABAP.

 
zeio_0-1709726333750.png
 
This requires us to expose and consume the Attachment API as described in the SAP Business Accelerator HUB - Attachments.
 
I belive this blog can be very useful since existing documentation primarily focuses on exposing the services, but we encounter conceptual problems when consuming them locally.
 
This method is also valid for a S/4HANA Private Cloud but has been implemented and tested only in S/4HANA Public Cloud.

Steps

  1. Create the Outbound Service. In Eclipse, and within your development package, follow these steps to create a new Outbound Service:

    zeio_0-1709737643035.png
    • Add name and description.
    • Select the type HTTP.
    • Set the Default Path Prefix for the Attachment method AttachmentContentSet, as follows: "/sap/opu/odata/sap/API_CV_ATTACHMENT_SRV/AttachmentContentSet".

    zeio_1-1709737678982.png
  2. Create the Communication Scenario. Still in Eclipse and under your development package, create a new Communication Scenario as follows:

    zeio_2-1709737713134.png
    • Add name and description.
    • In the Outbound tab, add the previously created service.

      zeio_3-1709737805888.png
    • Use the top-right 'Publish Locally' button to publish the communication scenario.

  3. Create the Communication User. Navigate to the Maintain Communication User app and create a new user. Remember to copy the username and password, as we will need them to configure the communication systems.

    zeio_4-1709738003846.png

    zeio_5-1709738076778.png
  4. Assign the new communication user to our local Communication System. In the Communication System app, select our local system using the top button labeled "Own SAP Cloud System."

    zeio_6-1709738120971.png

    zeio_7-1709738150828.png
    • Copy the hostname, as it will be needed in the next step.
    • In edit mode, assign the previously created user as the User for Inbound Communication and save.
      zeio_8-1709738210163.png
  5. Create a Communication System to consume the service. Go to the Communication Systems App, create a new system, fill the ID and description.

    zeio_9-1709738280494.png

    zeio_10-1709738305034.png
    • Complete hostname field with the value obtained in the previous step (local hostname).

      zeio_12-1709738469078.png
    • Assign the previously created user as Outbound User, and save.

      zeio_13-1709738517919.pngzeio_14-1709738531108.png
  6. Create the Communication Arrangement to expose the service. Navigate to the Communication Arrangement App, create a new one, or check if one exists, based on the Scenario SAP_COM_0002: Finance - Posting Integration. This scenario has sufficient permissions to attach document files to a Journal Entry instance.

    zeio_2-1709739349096.png

     

    zeio_3-1709739393983.png
    • After selecting or creating the communication arrangement, ensure that the communication system is assigned to your local system. Also, add the created communication user as Inbound Communication User.

      SergioGracia_1-1709742372912.png

       



    • We don't need the Outbound Services of the Arrangement for this requirement, so unless necessary for other reasons, deactivate these services by unchecking the Active flag.

      SergioGracia_0-1709741795715.png
  7. Create the Communication Arrangement to consume the service. Create a new Communication Arrangement based on our scenario created in step 2, assign the communication system created and assign the outbound communication user.

    zeio_7-1709739718535.png

     

    SergioGracia_2-1709742477463.png

    After saving, you can verify if the user/system relationship has been correctly configured using the Check Connection button.

    zeio_8-1709739781520.png

    If you get an error, please check the Outbound User has the right password in Communication System.

  8. Consume the Attachment API from an ABAP Class.

  • You can consume the API using ABAP with the following code.

 

    TRY.
        DATA(lo_destination) = cl_http_destination_provider=>create_by_comm_arrangement(
                                 comm_scenario  = 'ZCS_ATTACHMENT' "Communication Arrangement
                                 service_id     = 'ZOS_API_ATTACHMENT_REST' "Outbound Service).

        DATA(lo_http_client) = cl_web_http_client_manager=>create_by_http_destination( i_destination = lo_destination ).

        "Set CSRF Token
        lo_http_client->set_csrf_token(  ).

        DATA(lo_request) = lo_http_client->get_http_request( ).

        lo_request->set_header_field( i_name = 'Slug' i_value = iv_filename )."Filename (with extension)
        lo_request->set_header_field( i_name = 'BusinessObjectTypeName' i_value = iv_bo_type ). "BKPF
        lo_request->set_header_field( i_name = 'LinkedSAPObjectKey' i_value = iv_bo_key ). "Journal Entry concatenate key fields.
        lo_request->set_content_type( content_type = iv_mimetype ). "Mimetype
        lo_request->set_binary( i_data = iv_attachment ). "File content in binary

        DATA(lo_response) = lo_http_client->execute( i_method = if_web_http_client=>post ).


        DATA(ls_status) = lo_response->get_status( ).

        IF ls_status-code NE 201.
          "Catch error (the attachment hasn't be created)
        ENDIF.

      CATCH cx_web_message_error INTO DATA(lx_web).
         "Catch error (the service is misconfigured)
    ENDTRY.

 

Please take a look to "iv_" variables and replace them with valid information. or include the code into a class method with these variables, all of them are strings.

6 Comments
Labels in this area