Wednesday 13 March 2024

How to attach documents to a Journal Entry within SAP S/4HANA Public Cloud from ABAP

Introduction


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

How to attach documents to a Journal Entry within SAP S/4HANA Public Cloud from ABAP.

This requires us to expose and consume the Attachment API as described in the SAP Business Accelerator HUB - Attachments.
 
I believe 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:

How to attach documents to a Journal Entry within SAP S/4HANA Public Cloud from ABAP.

◉ 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".

How to attach documents to a Journal Entry within SAP S/4HANA Public Cloud from ABAP.

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

How to attach documents to a Journal Entry within SAP S/4HANA Public Cloud from ABAP.

◉ Add name and description.
◉ In the Outbound tab, add the previously created service.

How to attach documents to a Journal Entry within SAP S/4HANA Public Cloud from ABAP.

◉ 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.

How to attach documents to a Journal Entry within SAP S/4HANA Public Cloud from ABAP.

How to attach documents to a Journal Entry within SAP S/4HANA Public Cloud from ABAP.

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."


How to attach documents to a Journal Entry within SAP S/4HANA Public Cloud from ABAP.

◉ 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.

How to attach documents to a Journal Entry within SAP S/4HANA Public Cloud from ABAP.

5. Create a Communication System to consume the service. Go to the Communication Systems App, create a new system, fill the ID and description.

How to attach documents to a Journal Entry within SAP S/4HANA Public Cloud from ABAP.

How to attach documents to a Journal Entry within SAP S/4HANA Public Cloud from ABAP.

◉ Complete hostname field with the value obtained in the previous step (local hostname).

How to attach documents to a Journal Entry within SAP S/4HANA Public Cloud from ABAP.

◉ Assign the previously created user as Outbound User, and save.

How to attach documents to a Journal Entry within SAP S/4HANA Public Cloud from ABAP.

How to attach documents to a Journal Entry within SAP S/4HANA Public Cloud from ABAP.

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.

How to attach documents to a Journal Entry within SAP S/4HANA Public Cloud from ABAP.

How to attach documents to a Journal Entry within SAP S/4HANA Public Cloud from ABAP.

◉ 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.

How to attach documents to a Journal Entry within SAP S/4HANA Public Cloud from ABAP.

◉ 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.

How to attach documents to a Journal Entry within SAP S/4HANA Public Cloud from ABAP.

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.

How to attach documents to a Journal Entry within SAP S/4HANA Public Cloud from ABAP.

How to attach documents to a Journal Entry within SAP S/4HANA Public Cloud from ABAP.

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

How to attach documents to a Journal Entry within SAP S/4HANA Public Cloud from ABAP.

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.

No comments:

Post a Comment