domingo, 13 de outubro de 2019

E-Mail Templates in S/4 HANA

E-Mail communication is very common business requirements in day-to-day life. SAP understands that and comes up with very interesting feature in S/4 HANA (cloud and on premise both) – E-Mail Templates. In this article, I will provide a little overview and a demo of E-Mail templates.

What is E-Mail templates ?
With S/4 HANA Output Management, SAP provides E-mail templates to be configured which will be mapped to output types in BRF+. We can maintain HTML and Plain text in different languages in these  E-Mail templates and also, can map a CDS view for handling dynamic variables. This feature would save a lot of hardcoding or other custom ways to maintain E-mail content as done in past. Although in S/4 HANA, SAP uses E-Mail templates specifically in output management, still we can use this feature independent of output configurations and we will see in Demo section below for its usage.

Prerequisites:
  • Basic Knowledge of CDS views.
  • Good knowledge of ABAP.
  • Basic understanding of HTML.

How to create E-Mail Template?
There is no specific transaction to create email templates but we can create it as a repository object in SE80 transaction as follow:
  1. Select the package ( or Local objects ) and right click.
  2. Select “Create” -> “More” -> “Email Template”

However, we can always view/Edit the existing E-Mail templates from program SMTG_WB_START.
Different Component in E-Mail templates:
Header –
  • We need to maintain a name / description for the email template.
  • Also, we can maintain a CDS view which should be pre-delivered and can be used to provide dynamic variables in email content (body or subject).
Texts-
  1. Languages             Maintain Email in different languages
  2. CDS Fields             Set of CDS view fields used in Email content
  3. Email Subject         Email subject description
  4. Body HTML            Email body content in HTML
  5. Body Plain Text      Email body content in plain text
Dynamic Variables in E-mail Content:
For maintaining dynamic variables, we would need to create a CDS view which would contain the required data. For each different email variables, we can pass CDS key with Name/Value pair to Email Template API classes and replace the variables with desired content very easily.
  • Create a CDS view ZRSCDS_INVOICE_DATA (for demo, I took reference of billing document header and Item tables).
@AbapCatalog.sqlViewName: 'ZRSCDS_INVDATA'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Invoice Data Line Item wise' 
//@VDM.viewType:#BASIC
define view ZRSCDS_INVOICE_DATA 
as select from vbrk as zzrs_vbrk
join vbrp as zzrs_vbrp
    on zzrs_vbrk.vbeln = zzrs_vbrp.vbeln 
{
 key zzrs_vbrk.vbeln,
 key zzrs_vbrp.posnr,      
     zzrs_vbrk.fkart, 
     zzrs_vbrk.vbtyp,
     @Semantics.currencyCode: true
     zzrs_vbrk.waerk, 
     zzrs_vbrk.vkorg, 
     zzrs_vbrk.fkdat,    
     @Semantics.amount.currencyCode: 'waerk' 
     @DefaultAggregation: #SUM
     zzrs_vbrk.netwr, 
     zzrs_vbrk.kunag as kunag,      
     zzrs_vbrp.fkimg, 
     zzrs_vbrp.vrkme, 
     zzrs_vbrp.meins, 
     zzrs_vbrp.matnr         
}
  • Add this CDS view in Email template Header.
  • Assign CDS view fields in E-Mail Body and Subject where ever required.

Email Preview-
We can always preview our email template how it will look once sent to customers by clicking on “preview” button as highlighted:

How to call E-Mail Templates ?
Till now, I have created E-mail templates in system. Now, I would like to integrate this in one of the calling programs which send email to customers and email content will be taken from templates. For the demo purpose, I created a simple program where I can pass receiver email address, E-Mail Template, Language and CDS Key ( Billing Document in our case ).
SAP has provided E-Mail template API class which can be instantiated and used to get email content. Steps are as follow:
  • Create instance of class CL_SMTG_EMAIL_API.
DATA(lo_email_api) = cl_smtg_email_api=>get_instance( iv_template_id = p_em_id  ).
  • Create instance of class CL_BCS.
DATA(lo_bcs) = cl_bcs=>create_persistent( ).
  • Prepare CDS view Key table with Key Field name and value.
DATA(lt_cds_key) = VALUE ty_gt_data_key( ( name = 'vbeln' value = p_vbeln ) ).
  • Integrate E-Mail subject and body with email instance
lo_email_api->render_bcs( io_bcs = lo_bcs iv_language = p_spras it_data_key = lt_cds_key ). 
  • Set Sender, receiver and send the email.
  " Set Email Sender
  DATA(lo_sender) = cl_sapuser_bcs=>create( sy-uname ).

  lo_bcs->set_sender( i_sender = lo_sender ).

  " Set Email Receiver(s)
  DATA(lo_recipient) = cl_cam_address_bcs=>create_internet_address( p_rec ).
  lo_bcs->add_recipient( EXPORTING i_recipient = lo_recipient ).

  " Send Email
  lo_bcs->send( ).

Selection-Screen of Demo Program :

Result Email with replacement of dynamic variables :

Nenhum comentário:

Postar um comentário