SAP Knowledge Base Article - Public

3482444 - How to add multiple email recipients for billing document output - SAP S/4HANA Cloud Public Edition

Symptom

By default, billing document output in output channel EMAIL can only be sent to one recipient per output item, for example, to the standard address of the bill-to party maintained in customer master data. However, you want billing document output to automatically be sent to multiple email addresses, and you want the system to determine those email addresses based on attributes of the billing document that is being output.

You can achieve this by implementing custom logic and making settings in the Email Recipients step of the Output Parameter Determination app.

Environment

SAP S/4HANA Cloud

Resolution

As of SAP S/4HANA Cloud 2402.1, you can include multiple recipient email addresses in billing output items using the method described below.
In addition to the direct recipients (email type code TO), you can also include addresses for CC and BCC recipients.

You enable the sending of output to multiple email recipients by implementing custom logic using the Business Add-In (BAdI) Addition of Email Recipients (APOC_ADD_EMAIL_RECIPIENTS), which the system then calls during output parameter determination. The custom logic can evaluate attributes of the billing document (such as header fields, item fields, and partners) that is being output to determine the recipient email addresses.

Follow the steps below to implement the BAdI and make the settings in the Output Parameter Determination app that enable the system to call the BAdI during output parameter determination.

1. Create the BAdI implementation

a. Open the Custom Logic app and click Create:

 

b. Select Output Request Item as the business context and, under Extension Point, select the BAdI Addition of Email Recipients. Then click the Step 2 button:

 

c. Enter BILLING_DOCUMENT as the filter value and save.

 

d. Click on Step 3. Provide an Implementation Description and Implementation ID. Click Review and then Create:

 

e. Click Publish.

f. Click Open Code Editor and then Edit to write your custom logic. When done, click Save and Publish.

 

You must fill your BAdI implementation with the custom logic that determines the target email recipients.

The following example code shows how you can use the BAdI to read and process the header, item, and partner data of the billing document. By setting conditions to evaluate this data, your custom logic can set email recipients dynamically (for example, by retrieving business partner email addresses from a CDS view) or hardcode the email addresses directly. The BAdI can also read any custom fields that you have added at header or item level.

CUSTOM LOGIC EXAMPLE:

DATAadditional_email_recipient LIKE LINE OF additionalemailrecipients.

* Read runtime data of your billing document which is not necessarily stored on DB yet:
* Read billing document header
cl_sdbil_output_runtime_data=>get_header_data(
  EXPORTING
    billingdocument CONV #outputcontrolapplicationobject )
  IMPORTING
    data            DATA(billing_document_header) ).

* Read billing document items
cl_sdbil_output_runtime_data=>get_item_data(
  EXPORTING
    billingdocument CONV #outputcontrolapplicationobject )
  IMPORTING
    items           DATA(billing_document_items) ).

* Read billing document header partners
cl_sdbil_output_runtime_data=>get_header_partners(
  EXPORTING
    billingdocument CONV #outputcontrolapplicationobject )
  IMPORTING
    partners        DATA(billing_doc_header_partners) ).

* Read billing document item partners
cl_sdbil_output_runtime_data=>get_item_partners(
  EXPORTING
    billingdocument CONV #outputcontrolapplicationobject )
  IMPORTING
    partners        DATA(billing_doc_item_partners) ).

* Custom fields are also available
IF billing_document_header-yy1_custom_field IS INITIAL.
  READ TABLE billing_doc_header_partners WITH KEY partnerfunction 'RE' INTO DATA(bill_to_partner).

* Read an email address from a CDS view:
  SELECT *
    FROM i_addressemailaddress_2
    WHERE addressid @bill_to_partner-addressid
    INTO TABLE @DATA(email_addressesPRIVILEGED ACCESS.

* Set email recipient dynamically:
* In this case the email address maintained in the BP is assigned as "TO" email, the contact persons of the BP get the mail in CC

  LOOP AT email_addresses ASSIGNING FIELD-SYMBOL(<email>).
    additional_email_recipient-outputrequestitememailaddress = <email>-emailaddress.
    IF <email>-addresspersonid IS INITIAL.
      additional_email_recipient-outputrequestitememailrole = 'TO'.
    ELSE.
      additional_email_recipient-outputrequestitememailrole = 'CC'.
    ENDIF.

    APPEND additional_email_recipient TO additionalemailrecipients.
  ENDLOOP.

ELSE.
* Set email recipient hard-coded:
  additional_email_recipient-outputrequestitememailrole = 'TO'.
  additional_email_recipient-outputrequestitememailaddress = 'recipient_to@example.com'.
  APPEND additional_email_recipient TO additionalemailrecipients.

  additional_email_recipient-outputrequestitememailrole = 'TO'.
  additional_email_recipient-outputrequestitememailaddress = 'additional_recipient_to@example.com'.
  APPEND additional_email_recipient TO additionalemailrecipients.

  additional_email_recipient-outputrequestitememailrole = 'CC'.
  additional_email_recipient-outputrequestitememailaddress = 'recipient_cc@example.com'.
  APPEND additional_email_recipient TO additionalemailrecipients.

  additional_email_recipient-outputrequestitememailrole = 'BCC'.
  additional_email_recipient-outputrequestitememailaddress = 'recipient_bcc@example.com'.
  APPEND additional_email_recipient TO additionalemailrecipients.
ENDIF.

 

 

2. Configure output parameter determination to call the BAdI

a. Open the Output Parameter Determination app. Use the first drop-down list to show the rules for Billing Document and select the determination step Email Recipient:

b. Click Edit and use the + button to add a new row.

 

c. Under Email Type Code choose Direct Value Input and use the value help to select an email type code (for example, TO for the direct recipient). Under Email Address, choose Select Expression:

Enter the following values as search criteria:
Application Name = OPD_APOC_SYSTEM
Expression Type = Constant

d. Click Search and select Add email recipients from custom logic:

Click OK.

e. Activate your changes by clicking the Activate button. During output parameter determination, the system will now call the Addition of Email Recipients BAdI for each entered row to determine additional email recipients:

 

Note that all email type codes that you want to fill via the BAdI need an entry here. For example, if the BAdI returns the CC address and there is no row for CC in the table, this returned address will be ignored.

See Also

How to Add Additional Email Recipients for Billing Documents by Using Custom Logic | SAP Help Portal

KBA 3331087 - SAP S/4HANA output control - BAdI: Addition of Email Recipients

KBA 3455336 - How to fetch customer email address from field "Clrk's internet add." when the billing document is sent to the customer

KBA 2764152 - Error "Number of email recipients must not exceed 1 for sender country &&"

Keywords

Multiple, Email addresses, TO, CC, BCC, Email settings, Output parameter determination, OPD, Billing document, Invoice, BAdI, Addition of Email Recipients, APOC_ADD_EMAIL_RECIPIENTS, Extensibility, Custom Logic, Output Request Item, Extension, Extension Point, BILLING_DOCUMENT, Implementation, Code Editor, additionalemailrecipients, partners, partner function, address id, addressid, dynamic email recipient, contact persons, role, Email Type Code, Add email recipients from custom logic,  , KBA , SD-BIL-GF-OC-2CL , Output Control Billing (Public Cloud) , CA-GTF-OC , SAP S/4HANA output management , How To

Product

SAP S/4HANA Cloud Public Edition all versions ; SAP S/4HANA Cloud all versions