S4 Email Templates

This new feature was added in S4 where users can define the subject and content text along with variables. There is inbuilt support for plain text, HTML, translation and transport. As the name suggests they are meant to be used in emails however I think in some use cases it can be used as a replacement for SO10 text.

Introduction

Templates are defined in two layers. In the first layer, developers create a template assigned CDS view from which the template can derive values (in both subject and contents). Also in this step, the initial subject and contents are defined in different languages. In the second layer, the user makes a copy of the predefined template following which subject and content can be changed. Users can also use any variable from CDS that was attached to the template by the developer. These variable placeholders are replaced by values at runtime (very much like the mail merger feature in MS Word). Translation in more languages can be added or removed, as desired.

To view existing email templates and create a new based on the pre-delivered templates, use app Maintain Email Template.

In the app, you can see predelivered templates, CDS name, all data fields which can be used in subject and body. You can also see the contents which can be changed after copying.

Usage in SAP S4 Output Management

Out of the box use of Email Template is in S4 OM. You can specify Email Template to used in Email Setting’s Determination Step.

Usually, you would copy email template by selecting pre-delivered template and using Copy button.

Specify template id and name, which will then appear in Custom tab.

Now you can change the subject and content of the copied Email Template.

Using Email Template in the Program

Another way of using an Email template is of course in your program where you have logic to send the email. Using class CL_SMTG_EMAIL_API you can use Email template with CL_BCS instance. Class CL_SMTG_EMAIL_API does everything for you with one simple call to method RENDER_BCS. You pass the instance of CL_BCS, internal table with key and value pair for CDS to retrieve values, language and default language.

REPORT zpwtestemail.

START-OF-SELECTION .

  DATA : email_template_name TYPE smtg_tmpl_id VALUE 'ZZ1_TEMPLATE_DEMO',
         sales_order_id      TYPE vbak-vbeln VALUE '0000000040'.

  DATA(bcs) = cl_bcs=>create_persistent( ).

  DATA(email_api) = 
     cl_smtg_email_api=>get_instance( 
             iv_template_id = email_template_name  ).
  
DATA(sales_order_cds_key) = 
             VALUE if_smtg_email_template=>ty_gt_data_key( 
                  ( name = 'SalesDocument' 
                    value = sales_order_id ) ).

  email_api->render_bcs( 
              io_bcs = bcs 
              iv_language = 'D' 
              iv_language_default = 'E' 
              it_data_key = sales_order_cds_key ).

  DATA(email_receipient)  = 
              cl_cam_address_bcs=>create_internet_address( 
                     'name.last@domain.com' ).
  bcs->add_recipient( 
              EXPORTING i_recipient  = email_receipient    ).

  bcs->send( ) .

  COMMIT WORK .

The method RENDER_BCS will first try to find Email template in the language specified in iv_language if not found then it will take Email template in the language specified in iv_language_default. Very thoughtful of SAP to implement like this.

Using Email Template instead of SO10 Long Text?

I think in some use cases Email templates can be used instead of SO10 text. The reason I think it’s better than SO10 text.

  1. It work in conjuntion with CDS which feeds runtime variables.
  2. Support both HTML and Plain text.
  3. Fiori based user interface which is way better than SO10. Especially in case of webgui, SO10 turns into line based editor. In fiori app you see all available translations in one page.
  4. Class CL_SMTG_EMAIL_API makes usage really easy. I like that you can pass two languages and system does the fallback logic.

The only thing which might be an issue is (may) not be able to change template content in production directly. I do not have access to the production system so I can not really confirm this but it is my gut feel. But, this could be something positive in some scenarios.

If it does make sense for you to use Email Template instead of SO10 you can use of method RENDER, which will return the subject, body in HTML and plain text.

REPORT zpwtestltext.

START-OF-SELECTION .

  DATA : email_template_name TYPE smtg_tmpl_id VALUE 'ZZ1_TEMPLATE_DEMO',
         sales_order_id      TYPE vbak-vbeln VALUE '0000000040'.

  DATA(bcs) = cl_bcs=>create_persistent( ).

  DATA(email_api) = 
      cl_smtg_email_api=>get_instance( 
              iv_template_id = email_template_name  ).
  
  DATA(sales_order_cds_key) = 
      VALUE if_smtg_email_template=>ty_gt_data_key( 
            ( name = 'SalesDocument' 
              value = sales_order_id ) ).

  email_api->render(
    EXPORTING
      iv_language         = 'F'
      iv_language_default = 'E'
      it_data_key         = sales_order_cds_key
    IMPORTING
      ev_subject          = DATA(subject)
      ev_body_html        = DATA(body_html)
      ev_body_text        = DATA(body_text) ).

Creating your own Template

Up until now, we have been using SAP delivered template. Yes, you can copy and change the text but it is linked with a CDS which, rightly so can not be changed. So, what happens if you do like to create a template which either SAP has not delivered or is linked with a custom table.

You create new pre-delivered Email Template.

For example, I have this CDS ZI_Event and I like to create emails to send out invitations.

I can create an Email Template from SE80 (or probably Eclipse – I haven’t checked). Follow the path as shown in the below screenshot.

It’s really straight forward and the below picture explains it all.

After you have saved it in SE80, it will appear in the Pre-delivered tab in the app.

Some more information..

To access the app Maintain Email Template in the S4 Onprem system you need tile catalog SAP_BASIS_TCR_T added to your profile.

Email templates are classed as Key user extensions and they will need to be transported in the usual way using the apps Configure Software Packages and Register Extensions for Transport.

If you do like to send attachments along with email, use the following code to add it to CL_BCS object before you send it.

  "Add pdf attachment
  DATA : email_document TYPE REF TO cl_document_bcs,
         pdf_size       TYPE so_obj_len,
         description    TYPE string,
         subject        TYPE sood-objdes,
         file_content   TYPE solix_tab,
         att_head       TYPE soli_tab.

  TRY .
      email_document ?= bcs->document( ).
      description = email_document->get_subject( ) .
      subject = description.
      CONCATENATE description '.pdf' INTO description.
      APPEND description TO att_head.
    CATCH cx_root.
  ENDTRY.

  file_content = cl_bcs_convert=>xstring_to_solix( pdf_in_xstring ) .

  email_document->add_attachment(
     EXPORTING
     i_attachment_type     = /bobf/if_sp_c=>gc_conv_pdf
     i_attachment_subject  = subject 
     i_att_content_hex     = file_content
     i_attachment_header   = att_head ).

One Reply to “S4 Email Templates

  1. Hello,
    Nice blog

    I’m trying to activate fiori tiles email template by using this role SAP_BASIS_TCR_T from fiori library apps. But It doesn’t exist. How to activate this role ?

Leave a Reply