Monday 8 November 2021

Understanding how email template works in Flexible Workflow

Introduction:

Today in this blog post, we will have a deep dive to understand how system uses email templates and how email is triggered by flexible workflow. There are many beautiful blog post available today on email template on flexible workflow, but the information on how system uses these template to trigger email is missing.

Los Geht’s:

A couple of points which are worth re-iterating:

◉ The email template for Flexible Workflow is maintained using Fiori App “Maintain Email Template”.

◉ The name of the email template should follow the below naming convension

    ◉ YY1_<Scenario_ID>_CRT_<StepID> / YY1_<Scenario_ID>_CRT_ALL: Email template to be used when workitem is created

    ◉ YY1_<Scenario_ID>_COMPLETE_POSITIVE: Email template to be used when workflow is approved (Point to note: It will be triggered when the workflow is approved, and not the workitem)

    ◉ YY1_<Scenario_ID>_COMPLETE_NEGATIVE: Email template to be used when workflow is rejected(Point to note: It will be triggered when the workflow is rejected)

So, whenever the email templates are maintained, it is stored in below tables

SMTG_TMPL_HDR Email Template Header 
SMTG_TMPL_HDR_T Email Template Name and Description 

SAP Business Workflow, SAP ABAP Development, SAP ABAP Extensibility, SAP ABAP Career, SAP ABAP Tutorial and Materials, SAP ABAP Guides, SAP ABAP Jobs
(Email template defined in Maintain Email Template Fiori App)

SAP Business Workflow, SAP ABAP Development, SAP ABAP Extensibility, SAP ABAP Career, SAP ABAP Tutorial and Materials, SAP ABAP Guides, SAP ABAP Jobs
(Email template stored in Table)

Point to note: We can see the table is a cross client table, so no need to transport the email template across client.

Now that email template has been maintained using the Fiori apps, various event like work item creation, workflow approval and rejection, system check for the respective template associated with the workflow and creates an entry in the table “SWW_WIREGISTER” on the respective event(WI Creation, Approval and rejection) if email template is maintain for those events.

Let’s have a look into table entry to have an idea how it’s looks.

SAP Business Workflow, SAP ABAP Development, SAP ABAP Extensibility, SAP ABAP Career, SAP ABAP Tutorial and Materials, SAP ABAP Guides, SAP ABAP Jobs
(Registration of a Work Item)

We can notice below points in the table entries

◉ Application type is OPM(Output Management)
◉ Subcategory
    ◉ 001 – Email on workitem creation
    ◉ 003 – Email on workflow approved/rejected
◉ Value: We can in value field the respective “Email template” and “use case” has been stored
◉ Use cases: Use case is the events on which email needs to be sent.
    ◉ useCase-mailToWorkitemRecipients
    ◉ useCase-postiveWorkflowCompletion
    ◉ useCase-negativeWorkflowCompletion
◉ The class “CL_SWF_RUN_OM_SERVICE“ (Workflow: Mail service via output management) is responsible for workitem registration.

SAP Business Workflow, SAP ABAP Development, SAP ABAP Extensibility, SAP ABAP Career, SAP ABAP Tutorial and Materials, SAP ABAP Guides, SAP ABAP Jobs
(Class CL_SWF_RUN_OM_SERVICE)

We can see various important method available in the class, for example “REGISTER”  method, which saves the entry to “SWW_WIREGISTER” table and “GET_TEMPLATE_ID” gets the workflow email template. All the method name are self explanatory, so I am skipping the explanation of the functionality of these methods.

SAP Business Workflow, SAP ABAP Development, SAP ABAP Extensibility, SAP ABAP Career, SAP ABAP Tutorial and Materials, SAP ABAP Guides, SAP ABAP Jobs
(Constants in class CL_SWF_RUN_OM_SERVICE)

Let’s have a look, where and how the class “CL_SWF_RUN_OM_SERVICE” is used to registered the workitem to trigger the email.

Let’s have a look into the the class “CL_SWF_RUN_WIM_DIALOG“, which will be called when a dialog workitem is created. The class has a method “CREATE_VIA_WFM” which calls the register method of class “CL_SWF_RUN_OM_SERVICE” to save the entry in “SWW_WIREGISTER“.

SAP Business Workflow, SAP ABAP Development, SAP ABAP Extensibility, SAP ABAP Career, SAP ABAP Tutorial and Materials, SAP ABAP Guides, SAP ABAP Jobs
(Method CL_SWF_RUN_WIM_DIALOG =>CREATE_VIA_WFM registering the workitem for email)

So, this how the entry will be created in the table “SWW_WIREGISTER” for workitem creation use cases. Similarly system has to register the workitem on Workflow completion. That will be called from “IF_SWF_FLEX_IFS_RUN_APPL_STEP~AFTER_COMPLETION_CALLBACK” of callback class of the respective Flexible workflow.

Below is the screenshot of call back classes of flexible workflow for PO.(T-code: SWDD_SCENARIO_DISP, WF Template: WS00800238)

SAP Business Workflow, SAP ABAP Development, SAP ABAP Extensibility, SAP ABAP Career, SAP ABAP Tutorial and Materials, SAP ABAP Guides, SAP ABAP Jobs

SAP Business Workflow, SAP ABAP Development, SAP ABAP Extensibility, SAP ABAP Career, SAP ABAP Tutorial and Materials, SAP ABAP Guides, SAP ABAP Jobs

So, we understood how system is storing the entry in “SWW_WIREGISTER”. Now, that the entry are available in the table, but email has not yet triggered. So, how the email is triggered?

There is a report program “RSWF_OUTPUT_MANAGEMENT” which runs as a batch job every 1 min, which reads the table “SWW_WIREGISTER” and triggers the email.

Below are the screenshot of the program RSWF_OUTPUT_MANAGEMENT. We can see on the line number 28, the method “EXECUTE_ACTION” is called on object “LO_INSTANCE” and LO_INSTANCE is a object of class “CL_SWF_RUN_JOB_OUTPUT_MGMNT“.

SAP Business Workflow, SAP ABAP Development, SAP ABAP Extensibility, SAP ABAP Career, SAP ABAP Tutorial and Materials, SAP ABAP Guides, SAP ABAP Jobs
(Report RSWF_OUTPUT_MANAGEMENT)

We can see the “EXECUTE_ACTION” method calls “SEND_MAIL_TO_RECIPIENTS” method.

SAP Business Workflow, SAP ABAP Development, SAP ABAP Extensibility, SAP ABAP Career, SAP ABAP Tutorial and Materials, SAP ABAP Guides, SAP ABAP Jobs
(Source of CL_SWF_RUN_JOB_OUTPUT_MGMNT->EXECUTE_ACTIONS)

Vallah!!!” We can see the “EXECUTE_ACTIONS” uses the method “SEND_COMPLETION_MAILS” of class “CL_SWF_RUN_OM_SERVICE“. The method SEND_MAIL_TO_RECIPIENTS reads the table “SWW_WIREGISTER” and sends the email to the recipient using email template.

SAP Business Workflow, SAP ABAP Development, SAP ABAP Extensibility, SAP ABAP Career, SAP ABAP Tutorial and Materials, SAP ABAP Guides, SAP ABAP Jobs

SAP Business Workflow, SAP ABAP Development, SAP ABAP Extensibility, SAP ABAP Career, SAP ABAP Tutorial and Materials, SAP ABAP Guides, SAP ABAP Jobs
(Overview of process)

Source: sap.com

No comments:

Post a Comment