Financial Management Blogs by SAP
Get financial management insights from blog posts by SAP experts. Find and share tips on how to increase efficiency, reduce risk, and optimize working capital.
cancel
Showing results for 
Search instead for 
Did you mean: 
KarstenSchreyer
Associate
Associate
292

You can use the Business Add-In (BAdI) EDOC_PART_CONN_CHANGE_EMAIL_V2 to customize the e-mails you send to your customers. This BAdI allows you to tailor e-mail details such as addresses, subject lines, and body text. Additionally, you can modify existing attachments and add new ones.

Method Overview: CHANGE_EMAIL_TO_CUSTOMER

This BAdI uses the method CHANGE_EMAIL_TO_CUSTOMER with the following parameters:

Importing Parameters:

  • COMPANYEMAILADDRESSES: Contains e-mail addresses associated with the company code of the electronic document.
  • CUSTOMEREMAILADDRESSES: Contains customer-associated e-mail addresses.
  • EDOCUMENT: Provides data about the electronic document, including company code, source key, creation date, and more.

Changing Parameters:

  • EMAIL*: Parameters with the prefix EMAIL* relate to properties of the e-mail, such as subject, sender, recipients, and text.
  • ATTACHMENTS: Allows modification or addition of attachments. The fields to maintain include:
    • ATTACHM_TYPE: Type of attachment
    • ATTACHM_SUBJECT: Attachment name
    • ATTACHM_SIZE: Attachment size
    • ATTACHM_LANGUAGE: Language of the attachment
    • CONTENT_TEXT: Text format content for attachments (ensure that each line doesn’t exceed 255 characters)
    • CONTENT_HEX: Binary content for attachments, for example: PDF, XML (ensure that each line doesn’t exceed 255 characters)
    • ATTACHM_HEADER: Header data (ensure that each line doesn’t exceed 255 characters)
    • VSI_PROFILE: Virus scan profile for security

Which electronic document processes are supported?

The new BAdI supports customization for the following processes:

  • Germany Customer Invoice: E-Mail
  • Poland KSeF eInvoice
  • Saudi Arabia eInvoice (Generation)
  • Saudi Arabia Simplified Invoice
  • Saudi Arabia Tax Invoice
  • Thailand eTax
  • Thailand eReceipt

How do you implement the BAdI in ABAP Development Tools (ADT)?

  1. In your ABAP project, go to the enhancement spot ES_EDOCUMENT_CLOUD (path: package GLO-EDO > Enhancements > Enhancement Spots).
  2. Right-click ES_EDOCUMENT_CLOUD and select New BAdI Enhancement Implementation.
  3. Enter a package, name, and description.
  4. Choose Add BAdI Implementation and select EDOC_PART_CONN_CHANGE_EMAIL_V2.
  5. Enter a BAdI implementation name.
  6. Choose Implementing Class to create a new implementation class. The new implementation class automatically implements the interface IF_EDOC_CHANGE_EMAIL_V2.
  7. After generating the class, open the interface method and press F3 to view its parameters. You are now ready to implement your custom logic. To display the documentation, press F2.

Example Implementation

The following example shows how to modify the sender, recipient, subject, and e-mail text, and how to add an attachment. 

In addition, this example includes files of the eDocument file types: REQUEST, SEND_REQ, and SEND_RESP. These eDocument file types are defined in table EDOFILETYPE, where you can select the ones relevant for your country scenario. Note that each eDocument file type has its own MIME type. For example, requests and responses generally use the MIME type text/xml (with the attachment type XML), while PDFs use application/pdf (with the attachment type PDF).

In the code between lines 46 and 69, the raw file content is divided into chunks of 255 characters. Since you’re attaching a binary file, you need to populate the CONTENT_HEX field of the ls_attachment structure.

 

 

 

 

 

DATA: lv_sender       LIKE LINE OF companyemailaddresses,
      lv_recipient_to LIKE LINE OF customeremailaddresses,
      ls_content_line LIKE LINE OF emailcontenttext.

* Add the sender
READ TABLE companyemailaddresses INTO lv_sender INDEX 1.
emailsender = lv_sender-smtp_addr.

* Add recipients (To)
LOOP AT customeremailaddresses INTO lv_recipient_to.
  APPEND lv_recipient_to-smtp_addr TO emailrecipientsto.
ENDLOOP.

* Add the subject
CONCATENATE 'Invoice' edocument-source_key edocument-create_date
  INTO emailsubject SEPARATED BY space.

* Add email content lines (255 characters max. per line)
ls_content_line = 'Dear Customer,'.
APPEND ls_content_line TO emailcontenttext.
ls_content_line = 'Please find attached our invoice.'.
APPEND ls_content_line TO emailcontenttext.
ls_content_line = 'This is an automated email.'.
APPEND ls_content_line TO emailcontenttext.


*Add additional files (request and responses) to the attachment
DATA: ls_attachment  TYPE edoc_email_attachment_cloud,
      lv_country_iso TYPE intca.

TRY.
    DATA(lo_edocument) = cl_edocument_ext_es=>retrieve_by_edoc_guid(
                                                  EXPORTING
                                                    iv_edoc_guid = edocument-edoc_guid
                                                    iv_skip_interface_det = abap_true ).

    DATA(lt_edocumentfile) = lo_edocument->get_edoc_files(  ).

    LOOP AT lt_edocumentfile INTO DATA(ls_edocumentfile) 
        WHERE file_type = 'REQUEST' OR
              file_type = 'SEND_REQ' OR
              file_type = 'SEND_RESP'.
      ls_attachment-attachm_subject = ls_edocumentfile-file_name.
      ls_attachment-attachm_type = 'XML'.

      DATA  lp_offset TYPE i.
      DATA  ls_solix_line TYPE edoc_attch_content_hex.
      DATA  lp_pdf_string_len TYPE i.
      DATA  lp_solix_rows TYPE i.
      DATA  lp_last_row_length TYPE i.
      DATA  lp_row_length TYPE i.

*     Transform xstring to SOLIX
      lp_row_length = 255.
      lp_offset = 0.

      lp_pdf_string_len = xstrlen( ls_edocumentfile-file_raw ).

      lp_solix_rows = lp_pdf_string_len DIV lp_row_length.
      lp_last_row_length = lp_pdf_string_len MOD lp_row_length.
      DO lp_solix_rows TIMES.
        ls_solix_line-line = ls_edocumentfile-file_raw+lp_offset(lp_row_length).
        APPEND ls_solix_line TO ls_attachment-content_hex.
        ADD lp_row_length TO lp_offset.
      ENDDO.
      IF lp_last_row_length > 0.
        CLEAR ls_solix_line-line.
        ls_solix_line-line = ls_edocumentfile-file_raw+lp_offset(lp_last_row_length).
        APPEND ls_solix_line TO ls_attachment-content_hex.
      ENDIF.

      ls_attachment-attachm_size = xstrlen( ls_edocumentfile-file_raw ).
      APPEND ls_attachment TO attachments.
    ENDLOOP.
  CATCH cx_edocument_ext INTO DATA(lx_exception).
*    Throw error message
ENDTRY.