Loading...

How to dynamically generate email body using XSL Transformation

SAP Standard workflow doesn’t really provide an option for the developer to compose a good looking email. A nice workaround can be to use XSL (eXtensible Stylesheet Language) Transformation

XSL Transformations can be created or edited via transaction STRANS. The transformation in the example below generates a simple HTML page with title and a table with 3 rows and 2 columns. The dynamic data to be populated in the page is being passed to the transformation via parameter PARAM declared in the <tt:root> tag.

Transaction STRANS

The transformation can be called with the snipped below and the parameters can be maintained in any structure that will later on be processed by the XSLT.

constants:
  lc_xsl type string value '&amp;nbsp;',
  lc_html type string value '&nbsp;'.

data:
  ls_parameter type zmbs_xslt_parameter,
  lv_html_payload type string.

ls_parameter-subject = 'Demo Subject'.
ls_parameter-id      = 42.
ls_parameter-name    = 'My Name'.
ls_parameter-desc    = 'My Description'.

* Build the XHTML payload using the ST template:
call transformation zmb_xslt_demo
  source param = ls_parameter
  result xml lv_html_payload
  options xml_header = 'NO'.

* The XSL can't return '&nbsp;' and this is a problem
* for empty table cells <td></td> as it breaks the layout,
* so here we replace the &amp;nbsp; coming from the 
* transformation with &nbsp; which can be interpreted by the browser.
replace all occurrences of lc_xsl in lv_html_payload with lc_html.