on 2024 Sep 05 2:39 AM
Hi Experts!
I'm new to SAP S/4HANA Cloud Public Edition and I'm currently learning how to handle attachments using the ABAP cl_attachment_service_api local API. I'm having trouble with basic operations like creating and deleting attachments, and I'm not sure how to make the attachments appear in the "Manage Documents" app.
Any guidance or code examples would be greatly appreciated!
Thanks in advance!
Dear community,
Following the insightful suggestion from @patrick_winkler to add a COMMIT WORK after the create_attachment() call, all methods are now functioning as expected.
Here's the updated code:
DATA:
media_resource TYPE if_attachment_service_api=>ty_s_media_resource,
attachment_service TYPE REF TO if_attachment_service_api,
error TYPE REF TO cx_attachment_service,
text TYPE string.
DATA(str) = `{"test": "test01"}`.
DATA(xstr) = cl_abap_conv_codepage=>create_out( codepage = `UTF-8`
)->convert( source = str ).
media_resource = VALUE #( mime_type = `application/json; charset=utf-8`
value = xstr ).
attachment_service = cl_attachment_service_api=>get_instance( ).
TRY.
attachment_service->create_attachment(
EXPORTING iv_attachmentobjecttype = 'BUSINESSPARTNER'
iv_attachmentobjectkey = '14100014'
iv_filename = 'Testing.json'
iv_documenttype = 'SAT'
is_media_resource = media_resource
IMPORTING es_attachment = DATA(attach) ).
COMMIT WORK. " Commit to ensure the attachment is persisted
CATCH cx_attachment_service INTO error.
text = error->get_longtext( ).
ENDTRY.
TRY.
attachment_service->get_list_of_attachments(
EXPORTING iv_attachmentobjecttype = 'BUSINESSPARTNER'
iv_attachmentobjectkey = attach-attachmentobjectkey
iv_technicalobjecttype = attach-technicalobjecttype
IMPORTING et_attachments = DATA(attachments) ).
CATCH cx_attachment_service INTO error.
text = error->get_longtext( ).
ENDTRY.
TRY.
attachment_service->delete_attachment(
EXPORTING iv_logicaldocument = attach-logicaldocument
iv_archivedocumentid = attach-archivedocumentid
iv_technicalobjecttype = attach-technicalobjecttype
iv_attachmentobjecttype = 'BUSINESSPARTNER'
iv_attachmentobjectkey = attach-attachmentobjectkey
IMPORTING ev_success = DATA(success)
et_messages = DATA(messages) ).
CATCH cx_attachment_service INTO error.
text = error->get_longtext( ).
ENDTRY.
Thanks again for your support!
Best regards,
Dan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
67 | |
10 | |
10 | |
10 | |
10 | |
8 | |
8 | |
6 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.