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.
Create the Outbound Service. In Eclipse, and within your development package, follow these steps to create a new Outbound Service:
Create the Communication Scenario. Still in Eclipse and under your development package, create a new Communication Scenario as follows:
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.
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."
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.
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.
After saving, you can verify if the user/system relationship has been correctly configured using the Check Connection button.
If you get an error, please check the Outbound User has the right password in Communication System.
Consume the Attachment API from an ABAP Class.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
10 | |
8 | |
7 | |
6 | |
6 | |
6 | |
5 | |
4 | |
3 | |
3 |