Creation of Workflow Template

we are creating a very simple workflow template with a single User Decision Step. The template is shown (Notice there is no SEND MAIL step. E-Mail is sent via the Work Item exit)


/wp-content/uploads/2013/12/k_348961.png

/wp-content/uploads/2013/12/m_348968.png

The work item exit is mentioned in the user decision step as shown below

/wp-content/uploads/2013/12/n_348969.png

In the exit, we write our E-mail logic in the method AFTER_WI_CREATION. This method is triggered the moment the User Decision Work Item is created and sent to the approver. Our E-mail is also created and sent almost at the same instant. The code in this method is shown. The system E-mail ID will depend on the ID created by your infrastructure team.

(Note: I have shown two different ways to send the E-mail; one via the BCS Class and the other the SOFM Function Module. Please select whichever way you prefer. Remember to comment out one of the ways, else you will get two E-mails on Work Item Creation)

Code for method AFTER_WI_CREATION is below

METHOD after_wi_creation.

  DATA: lcl_v_wi_id        TYPE sww_wiid,  “Work Item ID

        lv_wid_read        TYPE sww_wiid,  “Work Item ID to read container data

        lv_wid_text        TYPE char12,

        l_it_father_wis    TYPE STANDARD TABLE OF swwwihead,

        l_wa_father_wis    TYPE swwwihead,

        send_request       TYPE REF TO cl_bcs,

        text               TYPE bcsy_text,

        body_text          TYPE so_text255,

        document           TYPE REF TO cl_document_bcs,

        sender             TYPE REF TO cl_sapuser_bcs,

        recipient          TYPE REF TO if_recipient_bcs,

        bcs_exception      TYPE REF TO cx_bcs,

        sent_to_all        TYPE os_boolean.

* Declare the Work Item Container data

  DATA: l_it_wi_container  TYPE STANDARD TABLE OF swcont,

        l_wa_wi_container  TYPE swcont,

        l_wa_wi_header     TYPE swwwihead.

* Declaration for the Dynamic Values in text

  DATA: lv_belnr           TYPE belnr_d,

        lv_bukrs           TYPE bukrs,

        lv_gjahr           TYPE char04,

        lv_email           TYPE comm_id_long,

        lv_lines           TYPE i,

        lv_so_obj_len      TYPE so_obj_len.

* Other e-mail technique to do the declarations

  DATA : it_message         TYPE STANDARD TABLE OF solisti1,

         wa_message         TYPE solisti1,

         it_attach          TYPE STANDARD TABLE OF solisti1,

         wa_attach          TYPE solisti1,

         t_receivers        TYPE STANDARD TABLE OF somlreci1,

         wa_receivers       TYPE somlreci1,

         w_doc_data         TYPE sodocchgi1.

* For simplicity of the demo, we are only fetching the work item ID

* You can get the complete work item and workflow details as well

* Please refer class CL_SWF_RUN_WORKITEM_CONTEXT and interface IF_WAPI_WORKITEM_CONTEXT

* Get the Work Item ID

  CALL METHOD wi_context->get_workitem_id

    RECEIVING

      re_workitem = lcl_v_wi_id.

* Pass WID to text field to concatenate to the e-mail body for MAILTO link

  CLEAR: lv_wid_text.

  lv_wid_text = lcl_v_wi_id.

* Fetch the parent work item ID

  REFRESH: l_it_father_wis[].

  CALL FUNCTION ‘SWW_WI_FIND_FATHER_WIS’

    EXPORTING

      wi_id      = lcl_v_wi_id

    TABLES

      father_wis = l_it_father_wis.

* Read the Father WID, if not found then running WID is Father WID

  CLEAR: l_wa_father_wis, lv_wid_read.

  READ TABLE l_it_father_wis INTO l_wa_father_wis INDEX 1.

* If Father Work Item ID found pass to the WID Variable else pass the Original WID

  IF sy-subrc = 0.

    lv_wid_read = l_wa_father_wis-wi_id.

  ELSE.

    lv_wid_read = lcl_v_wi_id.

  ENDIF.

* Read the work flow container data with WID Populated

  REFRESH: l_it_wi_container[].

  CLEAR: l_wa_wi_header.

  lv_wid_read = lcl_v_wi_id – 1.

  CALL FUNCTION ‘SWW_WI_CONTAINER_READ’

    EXPORTING

      wi_id                    = lv_wid_read

    TABLES

      wi_container             = l_it_wi_container

    CHANGING

      wi_header                = l_wa_wi_header

    EXCEPTIONS

      container_does_not_exist = 1

      read_failed              = 2

      OTHERS                   = 3.

* If the call is a success

  IF sy-subrc = 0.

    CLEAR: lv_belnr, lv_bukrs, lv_gjahr, lv_email.

* Read the container values and pass them to generate the dynamic e-mail body

* Read the Company Code from Workflow WS91000120

    CLEAR: l_wa_wi_container.

    READ TABLE l_it_wi_container INTO l_wa_wi_container

    WITH KEY element = ‘BUKRS’.

    IF sy-subrc = 0.

      CONDENSE l_wa_wi_container-value.

      lv_bukrs = l_wa_wi_container-value.

    ENDIF.

* Read the Invoice Number from Workflow WS91000120

    CLEAR: l_wa_wi_container.

    READ TABLE l_it_wi_container INTO l_wa_wi_container

    WITH KEY element = ‘BELNR’.

    IF sy-subrc = 0.

      CONDENSE l_wa_wi_container-value.

      lv_belnr = l_wa_wi_container-value.

    ENDIF.

* Read the Fiscal Year from Workflow WS91000120

    CLEAR: l_wa_wi_container.

    READ TABLE l_it_wi_container INTO l_wa_wi_container

    WITH KEY element = ‘GJAHR’.

    IF sy-subrc = 0.

      CONDENSE l_wa_wi_container-value.

      lv_gjahr = l_wa_wi_container-value.

    ENDIF.

* Read the Company Code from Workflow WS91000120

    CLEAR: l_wa_wi_container.

    READ TABLE l_it_wi_container INTO l_wa_wi_container

    WITH KEY element = ‘EMAIL_ID’.

    IF sy-subrc = 0.

      CONDENSE l_wa_wi_container-value.

      lv_email = l_wa_wi_container-value.

    ENDIF.

  ENDIF.

*—————————————————————————————-*

* Send an e-mail to a dummy e-mail ID stating that the above Work Item has been created

*—————————————————————————————-*

  TRY.

*     ——– create persistent send request ————————

      send_request = cl_bcs=>create_persistent( ).

*     ——– create and set document ——————————-

*     Build the e-mail Body

*      CLEAR: body_text.

*      CONCATENATE ‘Work Item Created. WID:’

*                  lv_wid_text INTO body_text SEPARATED BY space.

*——————————————————————–*

* Start the Mail Body

*——————————————————————–*

      CLEAR: body_text, wa_message.

      CONCATENATE body_text

                  ‘<html><body>Dear User’

                  INTO body_text.

      APPEND body_text TO text.

      wa_message-line = body_text.

      APPEND wa_message TO it_message.

      CLEAR: wa_message.

      CLEAR: body_text.

      CONCATENATE body_text

                  ‘<h3>What you need to know:</h3>’

                  INTO body_text.

      APPEND body_text TO text.

      wa_message-line = body_text.

      APPEND wa_message TO it_message.

      CLEAR: wa_message.

      CLEAR: body_text.

      CONCATENATE body_text

                  ‘Below is the invoice waiting for your approval.’

                  INTO body_text.

      APPEND body_text TO text.

      wa_message-line = body_text.

      APPEND wa_message TO it_message.

      CLEAR: wa_message.

      CLEAR: body_text.

      CONCATENATE body_text

                  ‘<h4>Invoice Information</h4>’

                  INTO body_text.

      APPEND body_text TO text.

      wa_message-line = body_text.

      APPEND wa_message TO it_message.

      CLEAR: wa_message.

*——————————————————————–*

* Start Invoice Information in Table

*——————————————————————–*

      CLEAR: body_text.

      CONCATENATE body_text

                  ‘<table border = “1” bgcolor = “skyblue”>’

                  INTO body_text.

      APPEND body_text TO text.

      wa_message-line = body_text.

      APPEND wa_message TO it_message.

      CLEAR: wa_message.

      CLEAR: body_text.

      CONCATENATE body_text

                  ‘<tr><td>Invoice Number</td><td>’

                  lv_belnr

                  ‘</td></tr>’

                  INTO body_text.

      CONDENSE body_text.

      APPEND body_text TO text.

      wa_message-line = body_text.

      APPEND wa_message TO it_message.

      CLEAR: wa_message.

      CLEAR: body_text.

      CONCATENATE body_text

                  ‘<tr><td>Company Code</td><td>’

                  lv_bukrs

                  ‘</td></tr>’

                  INTO body_text.

      CONDENSE body_text.

      APPEND body_text TO text.

      wa_message-line = body_text.

      APPEND wa_message TO it_message.

      CLEAR: wa_message.

      CLEAR: body_text.

      CONCATENATE body_text

                  ‘<tr><td>Fiscal Year</td><td>’

                  lv_gjahr

                  ‘</td></tr>’

                  INTO body_text.

      CONDENSE body_text.

      APPEND body_text TO text.

      wa_message-line = body_text.

      APPEND wa_message TO it_message.

      CLEAR: wa_message.

      CLEAR: body_text.

      CONCATENATE body_text

                  ‘<tr><td>Gross Invoice Value (USD)</td><td>200,000.00</td></tr></table>’

                  INTO body_text.

      APPEND body_text TO text.

      wa_message-line = body_text.

      APPEND wa_message TO it_message.

      CLEAR: wa_message.

*——————————————————————–*

* End Invoice Table

*——————————————————————–*

*——————————————————————–*

* Start Multiline Table

*——————————————————————–*

      CLEAR: body_text.

      CONCATENATE body_text

                  ‘<h4>Line Item Data displayed in tabular format</h4>’

                  INTO body_text.

      APPEND body_text TO text.

      wa_message-line = body_text.

      APPEND wa_message TO it_message.

      CLEAR: wa_message.

      CLEAR: body_text.

      CONCATENATE body_text

                  ‘<table border=”1″ bgcolor = “lightgreen”>’

                  INTO body_text.

      APPEND body_text TO text.

      wa_message-line = body_text.

      APPEND wa_message TO it_message.

      CLEAR: wa_message.

      CLEAR: body_text.

      CONCATENATE body_text

                  ‘<tr>’

                  ‘<th>Header 1</th>’

                  ‘<th>Header 2</th>’

                  ‘<th>Header 3</th>’

                  ‘<th>Header 4</th>’

                  ‘<tr>’

                  ‘<td>row 1, cell 1</td>’

                  ‘<td>row 1, cell 2</td>’

                  ‘<td>row 1, cell 3</td>’

                  ‘<td>row 1, cell 4</td>’

                  ‘</tr>’

                  INTO body_text.

      APPEND body_text TO text.

      wa_message-line = body_text.

      APPEND wa_message TO it_message.

      CLEAR: wa_message.

      CLEAR: body_text.

      CONCATENATE body_text

                  ‘<tr>’

                  ‘<td>row 2, cell 1</td>’

                  ‘<td>row 2, cell 2</td>’

                  ‘<td>row 2, cell 3</td>’

                  ‘<td>row 2, cell 4</td>’

                  ‘</tr>’

                  INTO body_text.

      APPEND body_text TO text.

      wa_message-line = body_text.

      APPEND wa_message TO it_message.

      CLEAR: wa_message.

      CLEAR: body_text.

      CONCATENATE body_text

                  ‘<tr>’

                  ‘<td>row 3, cell 1</td>’

                  ‘<td>row 3, cell 2</td>’

                  ‘<td>row 3, cell 3</td>’

                  ‘<td>row 3, cell 4</td>’

                  ‘</tr>’

                  INTO body_text.

      APPEND body_text TO text.

      wa_message-line = body_text.

      APPEND wa_message TO it_message.

      CLEAR: wa_message.

      CLEAR: body_text.

      CONCATENATE body_text

                  ‘<tr>’

                  ‘<td>row 4, cell 1</td>’

                  ‘<td>row 4, cell 2</td>’

                  ‘<td>row 4, cell 3</td>’

                  ‘<td>row 4, cell 4</td>’

                  ‘</tr>’

                  ‘</table>’

                  INTO body_text.

      APPEND body_text TO text.

      wa_message-line = body_text.

      APPEND wa_message TO it_message.

      CLEAR: wa_message.

*——————————————————————–*

* End Multiline Table

*——————————————————————–*

      CLEAR: body_text.

      CONCATENATE body_text

                  ‘<h3>What you need to do:</h3>’

                  INTO body_text.

      APPEND body_text TO text.

      wa_message-line = body_text.

      APPEND wa_message TO it_message.

      CLEAR: wa_message.

      CLEAR: body_text.

      CONCATENATE body_text

                  ‘To approve the invoice via portal, please log onto the YOUR-COMPANY Business Portal by clicking here:’

                  INTO body_text.

      APPEND body_text TO text.

      wa_message-line = body_text.

      APPEND wa_message TO it_message.

      CLEAR: wa_message.

* Generate the YOUR Company Portal Production Link

      CLEAR: body_text.

      CONCATENATE body_text

                  ‘<a href=”http://companyprderp.yourcompany.net:50000/irj/portal“>YOUR_COMPANY Portal</a></p>’

                  INTO body_text.

      APPEND body_text TO text.

      wa_message-line = body_text.

      APPEND wa_message TO it_message.

      CLEAR: wa_message.

* Genrate the Dynamic MAILTO Links

      CLEAR: body_text.

      CONCATENATE body_text

                  ‘To process directly from e-mail, make your selection here  ‘

                  INTO body_text.

      APPEND body_text TO text.

      wa_message-line = body_text.

      APPEND wa_message TO it_message.

      CLEAR: wa_message.

      CLEAR: body_text.

      CONCATENATE body_text

                  ‘<a href=”mailto:eccoffline@ecc-dev.yourcompany.com?subject=A’

                  lv_wid_text

                  ‘ – Approve Document”>APPROVE</a>  ‘

                  INTO body_text.

      APPEND body_text TO text.

      wa_message-line = body_text.

      APPEND wa_message TO it_message.

      CLEAR: wa_message.

      CLEAR: body_text.

      CONCATENATE body_text

                  ‘<a href=”mailto:eccoffline@ecc-dev.yourcompany.com?subject=R’

                  lv_wid_text

                  ‘ – Reject Document”>REJECT</a>’

                  INTO body_text.

      APPEND body_text TO text.

      wa_message-line = body_text.

      APPEND wa_message TO it_message.

      CLEAR: wa_message.

* Start DISCLAIMERS and WARNING Notifications

      CLEAR: body_text.

      CONCATENATE body_text

                  ‘<br><h4>Please Note: The links will send an e-mail via MICROSOFT OUTLOOK.’

                  ‘Please do not change the subject, body or any information in that e-mail.</h4>’

                  INTO body_text.

      APPEND body_text TO text.

      wa_message-line = body_text.

      APPEND wa_message TO it_message.

      CLEAR: wa_message.

      CLEAR: body_text.

      CONCATENATE body_text

                  ‘<h4>MICROSOFT Outlook must be installed in your system.</h4>’

                  INTO body_text.

      APPEND body_text TO text.

      wa_message-line = body_text.

      APPEND wa_message TO it_message.

      CLEAR: wa_message.

      CLEAR: body_text.

      CONCATENATE body_text

                  ‘<span style=”color:#FF0000″>’ “This is color for RED. Browser Safe colors mandatory to prevent broswer crash.

                  INTO body_text.

      APPEND body_text TO text.

      wa_message-line = body_text.

      APPEND wa_message TO it_message.

      CLEAR: wa_message.

      CLEAR: body_text.

      CONCATENATE body_text

                  ‘<h4> <style=”color: red”> CAUTION! Please do not reply to this e-mail.’

                  ‘For approval via e-mail, please click the links provided only. </h4></br>’

                  INTO body_text.

      APPEND body_text TO text.

      wa_message-line = body_text.

      APPEND wa_message TO it_message.

      CLEAR: wa_message.

      CLEAR: body_text.

      CONCATENATE body_text

                  ‘</span>’

                  INTO body_text.

      APPEND body_text TO text.

      wa_message-line = body_text.

      APPEND wa_message TO it_message.

      CLEAR: wa_message.

      CLEAR: body_text.

      CONCATENATE body_text

                  ‘</body></html>’

                  INTO body_text.

      APPEND body_text TO text.

      wa_message-line = body_text.

      APPEND wa_message TO it_message.

      CLEAR: wa_message.

*——————————————————————–*

* End of HTML Code Body

*——————————————————————–*

* Calculate the Document Size

      CLEAR: lv_lines, lv_so_obj_len.

      DESCRIBE TABLE it_message LINES lv_lines.

      lv_so_obj_len = 255 * lv_lines.

* Create the Document

      document = cl_document_bcs=>create_document(

                      i_type    = ‘HTM’ “Send HTML E-Mail and not ‘RAW’

                      i_text    = text

                      i_length  = lv_so_obj_len

                      i_subject = ‘E-Mail sent AFTER Work Item Creation’ ).

*     Add document to send request

      CALL METHOD send_request->set_document( document ).

*     ——— set sender ——————————————-

*     note: this is necessary only if you want to set the sender

*           different from actual user (SY-UNAME). Otherwise sender is

*           set automatically with actual user.

      sender = cl_sapuser_bcs=>create( sy-uname ).

      CALL METHOD send_request->set_sender

        EXPORTING

          i_sender = sender.

*     ——— Add recipient (e-mail address) ———————–

*     Create recipient – passing the e-mail ID here

      recipient = cl_cam_address_bcs=>create_internet_address( lv_email ).

*     Add recipient with its respective attributes to send request

      CALL METHOD send_request->add_recipient

        EXPORTING

          i_recipient = recipient

          i_express   = ‘X’.

*     ———- Send document —————————————

      CALL METHOD send_request->send(

        EXPORTING

          i_with_error_screen = ‘X’

        RECEIVING

          result              = sent_to_all ).

*      IF sent_to_all = ‘X’.

*        WRITE text-003.

*      ENDIF.

      COMMIT WORK.

* ———————————————————–

* *                     exception handling

* ———————————————————–

    CATCH cx_bcs INTO bcs_exception.

*      WRITE: text-001.

*      WRITE: text-002, bcs_exception->error_type.

*      EXIT.

  ENDTRY.

*——————————————————————–*

* Another way to send e-mail in HTML via function module

*——————————————————————–*

  CLEAR: w_doc_data.

  w_doc_data-sensitivty = ‘F’. “Fill the document data and get size of attachment

  w_doc_data-doc_size   = lv_lines * 255. “Populate the subject/generic message attributes

  w_doc_data-obj_langu  = sy-langu.

  w_doc_data-obj_name   = ‘SAPRPT’.

  w_doc_data-obj_descr  = ‘E-Mail sent AFTER Work Item Creation’.

  CLEAR: wa_receivers.

  REFRESH: t_receivers[].

  wa_receivers-receiver   = lv_email. “Get requestor mail id from table PA0105 based on his PERNR.

  wa_receivers-rec_type   = ‘U’.

  wa_receivers-com_type   = ‘INT’.

  wa_receivers-notif_del  = ‘X’.

  wa_receivers-notif_ndel = ‘X’.

  APPEND wa_receivers TO t_receivers.

* Send the HTML E-Mail

  CALL FUNCTION ‘SO_NEW_DOCUMENT_SEND_API1’

    EXPORTING

      document_data  = w_doc_data

      document_type  = ‘HTM’   ” To get the notification in HTML format

      commit_work    = ‘X’

    TABLES

      object_content = it_message

      receivers      = t_receivers.

ENDMETHOD.

This completes our coding and configuration. We will now test our Workflow and see how it works in Part-3


Cheers

Pradyp

To report this post you need to login first.

3 Comments

You must be Logged on to comment or reply to a post.

  1. Ronen Weisz

    Although I recommend using sap_wapi* function modules and not sww*, I like the idea of sending workflow approval by mails, but don’t you think that modifying the extended notification bsp application, replacing the decision web application links with the ‘mailto’ links would be a much easier and more generic way of implementing this?

    (0) 
  2. Marcos S

    Excellent post!!

    I’m stucked when creating method AFTER_WI_CREATION.

    It’s suposed to be created within a new ABAP Class ZZCL_WORK_ITEM_EXIT, right?

    I’ve implemented interface IF_WAPI_WORKITEM_CONTEXT but could not get how to refer class CL_SWF_RUN_WORKITEM_CONTEXT.

    capturese24.JPG

    (0) 

Leave a Reply