Within the SAP Application Interface Framework (AIF), e-mail notifications can be sent to interface responsibles via the standard SAP Alert Management. The formatting and content of the email notification is not part of the AIF and must be done in Alert Management. Typically, these email notifications are in rich text format and therefore cannot be extensively formatted in text. 

This short blog post is intended to illustrate how to change these email notifications to HTML format so that the text can be formatted to your preferences.

Prerequisites

An alert category is created for the AIF in Alert Management and the category is assigned to the interface. The alert category is triggered and the system sends an email to the interface responsibles.

Implementation

In the Alert Management environment there is a classic BAdI (ALERT_MODIFY_TEXT) with which the text can be modified. This BAdI returns two parameters in the signature. CP_TEXT_TYPE defines the type. This can be changed to “HTM”. CT_LONG_TEXT is a table and contains the actual text of the e-mail. 

The text can be customized and formatted with HTML tags. In order to get information from the alert container, it must be read so that it can be sent in the email. 

METHOD if_ex_alert_modify_text~modify_long_text.
  CONSTANTS: lc_text_type TYPE so_obj_tp VALUE 'HTM'.
  DATA: lr_swf_cnt_container TYPE REF TO if_swf_cnt_container,
        lv_ns                TYPE /aif/ns,
        lv_ifname            TYPE /aif/ifname,
        lv_ifversion         TYPE /aif/ifversion,
        lv_nsrecipient       TYPE /aif/ns,
        lv_recipient         TYPE /aif/alrt_rec,
        lt_messages          TYPE TABLE OF bapiret2,
        lv_messages_nr       TYPE sytabix,
        lv_msgguid           TYPE sxmsguid,
        lv_date(10)          TYPE c,
        lv_time(10)          TYPE c.

* Set Time/Date Format
  CONCATENATE sy-uzeit+0(2) sy-uzeit+2(2) sy-uzeit+4(2) INTO lv_time SEPARATED BY ':'.
  WRITE sy-datum TO lv_date MM/DD/YYYY.

* Change Mail Format to HTML
  cp_text_type = lc_text_type.

* Assign alert data
  DATA(lr_alert) = NEW cl_alert( ).
  lr_alert ?= io_alert.

* Get Alert Container
  TRY.
      CALL METHOD lr_alert->get_ifcontainer
        RECEIVING
          result = lr_swf_cnt_container.
      CALL METHOD lr_alert->get_creatime
        RECEIVING
          result = DATA(lv_creatime).
      CALL METHOD lr_alert->get_logical_system
        RECEIVING
          result = DATA(lv_logical_system).
    CATCH cx_os_object_not_found.
  ENDTRY.

* Get Elements
  TRY.
      CALL METHOD lr_swf_cnt_container->if_swf_ifs_parameter_container~get
        EXPORTING
          name  = 'NS'
        IMPORTING
          value = lv_ns.
      CALL METHOD lr_swf_cnt_container->if_swf_ifs_parameter_container~get
        EXPORTING
          name  = 'IFNAME'
        IMPORTING
          value = lv_ifname.
      CALL METHOD lr_swf_cnt_container->if_swf_ifs_parameter_container~get
        EXPORTING
          name  = 'IFVERSION'
        IMPORTING
          value = lv_ifversion.
      CALL METHOD lr_swf_cnt_container->if_swf_ifs_parameter_container~get
        EXPORTING
          name  = 'NSRECIPIENT'
        IMPORTING
          value = lv_nsrecipient.
      CALL METHOD lr_swf_cnt_container->if_swf_ifs_parameter_container~get
        EXPORTING
          name  = 'RECIPIENT'
        IMPORTING
          value = lv_recipient.
      CALL METHOD lr_swf_cnt_container->if_swf_ifs_parameter_container~get
        EXPORTING
          name  = 'MESSAGES'
        IMPORTING
          value = lt_messages.
      CALL METHOD lr_swf_cnt_container->if_swf_ifs_parameter_container~get
        EXPORTING
          name  = 'MESSAGES_NR'
        IMPORTING
          value = lv_messages_nr.
      CALL METHOD lr_swf_cnt_container->if_swf_ifs_parameter_container~get
        EXPORTING
          name  = 'MSGGUID'
        IMPORTING
          value = lv_msgguid.

    CATCH cx_swf_cnt_elem_not_found.
    CATCH cx_swf_cnt_elem_type_conflict.
    CATCH cx_swf_cnt_unit_type_conflict.
    CATCH cx_swf_cnt_container.
  ENDTRY.

* Build Mailtext
  CLEAR ct_long_text.
  APPEND VALUE #( line = '<p style="color:red;font-weight:bold;">Attention!<br />An error occured during message processing.</p>' ) TO ct_long_text.
  APPEND VALUE #( line = '<p style="font-weight:bold;">Following, you will find all necessary information.</p>' ) TO ct_long_text.
  APPEND VALUE #( line = '<ul style="list-style-type: disc;">' ) TO ct_long_text.
  APPEND VALUE #( line = '<li>Namespace:&nbsp;' && lv_ns && '</li>' ) TO ct_long_text.
  APPEND VALUE #( line = '<li>Interface:&nbsp;' && lv_ifname && '</li>' ) TO ct_long_text.
  APPEND VALUE #( line = '<li>Interface Version:&nbsp;' && lv_ifversion && '</li>' ) TO ct_long_text.
  APPEND VALUE #( line = '<li>Alert Recipient:&nbsp;' && lv_nsrecipient && '/' && lv_recipient && '</li>' ) TO ct_long_text.
  APPEND VALUE #( line = '<li>User:&nbsp;' && sy-uname && '</li>' ) TO ct_long_text.
  APPEND VALUE #( line = '<li>Date:&nbsp;' && lv_date && '</li>' ) TO ct_long_text.
  APPEND VALUE #( line = '<li>Time:&nbsp;' && lv_time && '</li>' ) TO ct_long_text.
  APPEND VALUE #( line = '<li>Message GUID:&nbsp;' && lv_msgguid && '</li>' ) TO ct_long_text.
  APPEND VALUE #( line = '</ul>' ) TO ct_long_text.
  APPEND VALUE #( line = '<p style="font-weight:bold;">Log Messages:</p>' ) TO ct_long_text.
  APPEND VALUE #( line = '<p>----------------------------------------------------------------------------------------------------</p>' ) TO ct_long_text.
  LOOP AT lt_messages INTO DATA(lv_message).
    APPEND VALUE #( line = '<span>' && lv_message-message && '</span><br />' ) TO ct_long_text.
  ENDLOOP.
  APPEND VALUE #( line = '<p>----------------------------------------------------------------------------------------------------</p>' ) TO ct_long_text.
ENDMETHOD.

Conclusion

This blog describes one way to solve the problem. It serves as a help and provides a solution approach.

Sara Sampaio

Sara Sampaio

Author Since: March 10, 2022

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x