Workflow. Sending Emails (2)

Workflow: Sending Emails to Dialog Task Processors

In this note, I’d like to share another method of sending an email to a dialog task processor within a workflow, which can be useful in project work.

See also: Workflow. Sending Emails

This time, the focus is on the Program Exits tab available when working with a task directly in the Workflow Builder (transaction SWDD).

See: Tab Page Programming Exits
You can enter programming exits on this tab page. A programming exit is an ABAP class that the system executes at a specific time during the processing of a workflow. Programming exits enable you to implement your own enhancements and adaptations.

To start using this workflow functionality, you must create an implementation class that supports the IF_SWF_IFS_WORKITEM_EXIT interface.

See: Tab Page Programming Exits
Features

ABAP classes that you specify here as programming exits must implement the interface IF_SWF_IFS_WORKITEM_EXIT. The system executes the classes in the specified order.

The method EVENT_RAISED is executed from every ABAP class as soon as the relevant work item performs a status change defined in the interface.

The method EVENT_RAISED is executed in each ABAP class as soon as the related work item undergoes a status change defined in the interface. Note that the implementation class can catch different states of the dialog task (see input parameter IM_EVENT_NAME).

Example

Preparing the Workflow

In the video snippet below, I show the sequence of steps where I create a workflow and several new elements in the workflow container. These elements will later be accessed in the class I’m creating before sending the email.

0:00
/3:29

Implementation Class

For demonstration purposes, the source code of the class in this note is as follows:

METHOD if_swf_ifs_workitem_exit~event_raised.
    DATA:
      value_get    TYPE char10,
      lv_user      TYPE sww_aagent,
      lv_email     TYPE string,
      lo_document  TYPE REF TO cl_document_bcs,
      sender       TYPE REF TO cl_sapuser_bcs,
      lv_body      TYPE string,
      text         TYPE bcsy_text,
      send_request TYPE REF TO cl_bcs,
      recipient    TYPE REF TO if_recipient_bcs,
      sent_to_all  TYPE os_boolean.
 
    DATA(lv_wi_id) = im_workitem_context->get_workitem_id(  ).
    DATA(lv_heaad) = im_workitem_context->get_header(  ).
 
    IF im_event_name EQ swrco_event_after_creation.
 
      CLEAR value_get.
      CALL METHOD get_elem(
        EXPORTING
          im_workitem_context = im_workitem_context
          element_name        = 'prm1'
        IMPORTING
          element_value       = value_get
                                ).
 
      CLEAR value_get.
      CALL METHOD get_elem(
        EXPORTING
          im_workitem_context = im_workitem_context
          element_name        = 'prm2'
        IMPORTING
          element_value       = value_get
                                ).
 
      CLEAR value_get.
      CALL METHOD get_elem(
        EXPORTING
          im_workitem_context = im_workitem_context
          element_name        = 'users'
        IMPORTING
          element_value       = lv_user
                                ).
      IF lv_user IS NOT INITIAL.
 
        CALL FUNCTION 'EFG_GEN_GET_USER_EMAIL'
          EXPORTING
            i_uname           = lv_user+2
          IMPORTING
            e_email_address   = lv_email
          EXCEPTIONS
            not_qualified     = 1
            user_not_found    = 2
            address_not_found = 3
            OTHERS            = 4.
        IF sy-subrc <> 0.
          lv_email = '[email protected]'.
        ENDIF.
 
        lv_body = 'mail from wi id #wi_id#'.
        SHIFT lv_wi_id LEFT DELETING LEADING '0'.
        REPLACE ALL OCCURRENCES OF '#wi_id#' IN lv_body WITH lv_wi_id.
 
        APPEND lv_body   TO text.
 
        send_request = cl_bcs=>create_persistent( ).
 
        lo_document = cl_document_bcs=>create_document(
                        i_type    = 'HTM'
                        i_text    = text
                        i_subject = 'mail from program exit' ).
 
        CALL METHOD send_request->set_document( lo_document ).
 
        sender = cl_sapuser_bcs=>create( 'WF-BATCH' ).
        CALL METHOD send_request->set_sender
          EXPORTING
            i_sender = sender.
 
        DATA lv_mail TYPE adr6-smtp_addr.
        lv_mail = lv_email.
        recipient = cl_cam_address_bcs=>create_internet_address(
                                          lv_mail ).
 
 
        CALL METHOD send_request->add_recipient
          EXPORTING
            i_recipient = recipient
            i_express   = 'X'.
 
 
        CALL METHOD send_request->send(
          EXPORTING
            i_with_error_screen = 'X'
          RECEIVING
            result              = sent_to_all ).
        IF sent_to_all = 'X'.
 
        ENDIF.
 
      ENDIF.
 
    ENDIF.
 
  ENDMETHOD.

Finally, I add the created class to the Program Exits tab of the dialog task in the new workflow.

0:00
/0:22

Testing

0:00
/1:38