Sample Code

Adding attachments to Idoc messages

Sending attachments with XML message is quite common in interfaces between two different systems. But when it comes to SAP, ABAPers usually find themselves at a very tricky situation if idocs are used for sending the messages.

Among the various alternatives, I find the solution using the ABAP ports, the best one. This blog will present the step by step approach and sample code of the solution.

1 Create an ABAP port. Refer this blog for getting the idea and for function module template.

2 Use the ABAP port as receiving port for the outbound idocs.

3 Read the idoc and convert it to XML message. Refer this blog. Please note, the idoc status must reflect the actual processing to change the idoc status if there is any error. Sample code available in the linked blog.

4 Get the pdf as a string. Refer this blog for sample code. Skip the part of GUI download.

5. Encode the RAW string of the PDF data using FM SSFC_BASE64_ENCODE. (Consult your interface partners where they can convert back the encoded data.


      CALL FUNCTION 'SSFC_BASE64_ENCODE'
        EXPORTING
          bindata                  = lv_pdfxst
*         BINLENG                  =
        IMPORTING
          b64data                  = lv_b64data
        EXCEPTIONS
          ssf_krn_error            = 1
          ssf_krn_noop             = 2
          ssf_krn_nomemory         = 3
          ssf_krn_opinv            = 4
          ssf_krn_input_data_error = 5
          ssf_krn_invalid_par      = 6
          ssf_krn_invalid_parlen   = 7
          OTHERS                   = 8.

6. Concatenate the data with XML from the Idoc. Remember the data must be added before the main tag of the XML of the idoc ends. So you need to do string manipulation from the Idoc data you got. Here our Idoc type is ZINVOICE001 so we are cutting13 characters (</ZINVOICE001>) from the end.


      DATA(lv_length) = strlen( lv_xml_data ).
      lv_length = lv_length - 13.
      DATA(lv_lasttag) = lv_xml_data+lv_length(13).
      lv_xml_data = lv_xml_data+(lv_length).
      CONCATENATE lv_xml_data
                  '&lt;INVOICEATTACHMENT>&lt;Filename>'
                  lv_pdfname
                  '&lt;/Filename>&lt;FileType>'
                  'PDF'
                  '&lt;/FileType>&lt;EncodedData>'
                  lv_b64data
                  '&lt;/EncodedData>&lt;/INVOICEATTACHMENT>'
                  lv_lasttag INTO lv_xml_data.

7 Now you need to send the idoc to partner system. You can choose an HTTP Post from here. Refer this blog for the code.

Phew… this is a long post and you need to work with the sample codes provided here to make up your own code. I believe, if you understand the individual concepts, you can develop the program quite easily. Unfortunately there is no short cut for such a complex solution and I cannot provide one code fits all scenario here. But still if you need some answers, do let us know.

As usual, if you like the content – be sure to share the blog with your contacts and follow us on your social media channel to get new updates.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)

Follow by Email
Twitter
LinkedIn
LinkedIn
Share