During one of our requirement I found ABAP Consumer proxy class doesn’t create the attachment method like Java consumer proxy. We need to insert the attachment at runtime using WS protocol. Here I am going to explain the steps and code.

Assumption:

  1. Web service you are going to consume has the attachment enabled

        <sapattahnd:Enabled xmlns:sapattahnd=”http://www.sap.com/710/features/attachment/“>true</sapattahnd:Enabled>


  2.  Web service is consumed and proxy class is activated

  3.  Create a Z class, method and use the code below


METHOD set_proposal.
* Author: Amaresh Pani
  DATA: lcl_obj TYPE REF TO zca_co_customerproposal,
        ls_request  TYPE  zca_send_customer_proposal_re4,
        ls_response TYPE  zca_send_customer_proposal_re3.
  TRY.
      CREATE OBJECT lcl_obj.
      CATCH cx_ai_system_fault INTO lx_fault.
      gv_msg = lx_fault->get_text( ).
  ENDTRY.
*----------------------------------------------------------------------------------------**
DATA: lo_attachments                TYPE REF TO if_wsprotocol_attachments.
DATA: g_attachment    TYPE REF TO  if_ai_attachment,
      g_attachments    TYPE          prx_attach,      "Internal table for attachment
      g_attach_xstring TYPE          xstring.          "variable of xstring type.
* binary xstring file stored in Z table
SELECT SINGLE value FROM zzupld INTO g_attach_xstring WHERE filename = 'CUST'.
TRY.
    lo_attachments ?= lcl_obj->get_protocol( if_wsprotocol=>attachments ).
    g_attachment = lo_attachments->get_attachment_from_binary(
          data = g_attach_xstring                " File content
          type = if_ai_attachment=>c_mimetype_pdf " MIME type
          name = 'CustomerProposal' ).            " Name of PDF to be uploaded
    APPEND g_attachment TO g_attachments.
    lo_attachments->set_attachments( g_attachments ).
    CATCH cx_ai_system_fault INTO lx_fault.
    gv_msg = lx_fault->get_text( ).
ENDTRY.
*----------------------------------------------------------------------------------------**
ls_request-send_customer_proposal_request-document-opprotunity_id-content = '7117941'. " Defaulted content of proxy
  TRY.
      lcl_obj->send_customer_proposal(
        EXPORTING
          send_customer_proposal_request  = ls_request
        IMPORTING
          send_customer_proposal_respons = ls_response ).
    CATCH cx_ai_system_fault INTO lx_fault.
      gv_msg = lx_fault->get_text( ).
    CATCH cx_ai_application_fault INTO lx_fault1.
      gv_msg = lx_fault->get_text( ).
  ENDTRY.
ENDMETHOD.



Content in Z table zzupld

Capture.JPG

Remember to set attachment “YES” while creating the endpoint.

Capture1.JPG

Execute the method to trigger the web service with attachment. Enable payload trace in SRT_UTIL to see the file attachment.

Capture5.JPG

SOAP creates tag MULTIPART. PART_1 contains the general SOAP xml content. PART_2 contains the attachment as above.

To report this post you need to login first.

1 Comment

You must be Logged on to comment or reply to a post.

  1. Ravi Narravula

    Hi Amaresh,

    Thanks for the information.

    I have a requirement to save a file attachment in SAP using Web Service, File will come from client(PHP).

    Right now, I have a web service which deals with only data.

    Client(PHP) requests data to SAP by providing input data, SAP will return the particular data to client.

    In same Web Service, Client(PHP) will upload the file, That need to be save in SAP.

    Please provide the sample programs or scenarios to handle attachments in server(SAP) side .

    Best Rgards,

    Ravi Narravula

     

    (0) 

Leave a Reply