cancel
Showing results for 
Search instead for 
Did you mean: 

cl_attachment_service_api usage in S/4HANA Cloud Public - Guidance Needed

dblackwood
Explorer
0 Kudos
376

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!

 

patrick_winkler
Product and Topic Expert
Product and Topic Expert
Have you checked the long text documentation of the class in ADT? It’s pretty extensive.
View Entire Topic
dblackwood
Explorer

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