Skip to Content
Author's profile photo Former Member

Add attachment to email step in Workflow through Program Exits

We get this requirement quite often to send attachments to email step in workflow. This can be achieved via programming exit. Same technique can be used for other relevant workflow steps.

Create a simple workflow with with email step.

/wp-content/uploads/2013/11/workflow_330503.png

Now create a class named ZCL_WF_ATTACH and add the interface IF_SWF_IFS_WORKITEM_EXIT to this class.

/wp-content/uploads/2013/11/class1_330504.png

Now lets implement method IF_SWF_IFS_WORKITEM_EXIT~EVENT_RAISED which comes with this interface.

METHOD if_swf_ifs_workitem_exit~event_raised .

   DATA: lv_id TYPE sww_wiid,

         lv_container TYPE REF TO if_swf_ifs_parameter_container,

         lv_attach TYPE TABLE OF obj_record,

         lv_folder_id TYPE soodk,

         lv_str TYPE string,

         lv_size TYPE so_obj_len,

         wa_document_info TYPE sofolenti1,

         lv_data TYPE sodocchgi1,

         lv_objtype TYPE swotobjidobjtype,

         lv_objkey TYPE swotobjidobjkey,

         lv_return TYPE swotreturn,

         lv_sofm TYPE swotrtimeobject,

         lv_objject TYPE obj_record,

         tb_obj TYPE TABLE OF obj_record,

         it_solix_tab1 TYPE solix_tab.

   CHECK im_event_name = ‘CREATED’.

* Fetch the workflow work item Id

   CALL METHOD im_workitem_context->get_workitem_id

     RECEIVING

       re_workitem = lv_id.

* Fetch Container

   CALL METHOD im_workitem_context->get_wi_container

     RECEIVING

       re_container = lv_container.

* Read attachment to confirm that there is no duplication

*CLEAR LV_OBJ_RECORD.

   TRY.

       CALL METHOD lv_container->get

         EXPORTING

           name  = ‘_ATTACH_OBJECTS’

         IMPORTING

           value = lv_attach.

     CATCH: cx_swf_cnt_elem_not_found,

   cx_swf_cnt_elem_type_conflict,

   cx_swf_cnt_unit_type_conflict,

   cx_swf_cnt_container.

   ENDTRY.

   IF lv_attach IS INITIAL.

     CALL FUNCTION ‘SO_FOLDER_ROOT_ID_GET’

       EXPORTING

         owner     = syuname

         region    = ‘B’

       IMPORTING

         folder_id = lv_folder_id.

* Create and set document

     DO 5 TIMES.

       CONCATENATE lv_str ‘Hello’ ‘,’ ‘my’ ‘,’ ‘friend!’ cl_abap_char_utilities=>cr_lf INTO lv_str.

     ENDDO.

*—you can use CL_DOCUMENT_BCS class for this purpose

     CALL METHOD me->string_to_solix

       EXPORTING

         iv_string   = lv_str

         iv_codepage = ‘4110’

       IMPORTING

         et_solix    = it_solix_tab1

         ev_size     = lv_size.

     lv_dataobj_name   = ‘Test Sending Attachments’.

     lv_dataobj_descr  = ‘Attachment 1’.

     lv_dataobj_langu  = sylangu.

     lv_datasensitivty = ‘P’.

     lv_datadoc_size   = lv_size.

     CALL FUNCTION ‘SO_DOCUMENT_INSERT_API1’

       EXPORTING

         folder_id                  = lv_folder_id

         document_data              = lv_data

         document_type              = ‘XLS’

       IMPORTING

         document_info              = wa_document_info

       TABLES

         contents_hex               = it_solix_tab1

       EXCEPTIONS

         folder_not_exist           = 1

         document_type_not_exist    = 2

         operation_no_authorization = 3

         parameter_error            = 4

         x_error                    = 5

         enqueue_error              = 6

         OTHERS                     = 7.

* Populate object type and object key for create an instance

     lv_objtype = ‘SOFM’.

     lv_objkey  = wa_document_infodoc_id.

     CALL FUNCTION ‘SWO_CREATE’

       EXPORTING

         objtype           = lv_objtype

         objkey            = lv_objkey

       IMPORTING

         object            = lv_sofm

         return            = lv_return

       EXCEPTIONS

         no_remote_objects = 1

         OTHERS            = 2.

* Prepare for attaching the object to container

     lv_objjectheader = ‘OBJH’.

     lv_objjecttype    = ‘SWO’.

     lv_objjecthandle  = lv_sofm.

     APPEND lv_objject TO tb_obj.

*—can be used for other workitems

     CALL METHOD lv_container->set

       EXPORTING

         name  = ‘_ATTACH_OBJECTS’

         value = tb_obj[].

*–this will add the attachment to email

     CALL METHOD lv_container->set

       EXPORTING

         name  = ‘ATTACHMENTS’

         value = tb_obj[].

* Commit the changes

     CALL METHOD im_workitem_context->do_commit_work.

   ENDIF.

ENDMETHOD.

Here is the screenshot of other methods used. You can directly use CL_DOCUMENT_BCS for this conversion or other methods available to do the conversion.

/wp-content/uploads/2013/11/class2_330505.png

/wp-content/uploads/2013/11/class3_330506.png

/wp-content/uploads/2013/11/class4a_330507.png

Complete code for method STRING_TO_XSTRING

method STRING_TO_XSTRING .

   DATA lo_conv TYPE REF TO cl_abap_conv_out_ce.

     DATA lv_bom TYPE xstring.

     DATA lv_xbuf TYPE xstring.

     DATA lv_cp TYPE abap_encod.

     TRY.

         IF iv_convert_cp IS INITIAL.

           EXPORT p = iv_string TO DATA BUFFER lv_xbuf.

           IMPORT p = ev_xstring FROM DATA BUFFER lv_xbuf

                  IN CHARTOHEX MODE.

         ELSE.

           IF iv_codepage IS INITIAL.

             lv_cp = cl_sx_mime_singlepart=>get_sx_node_codepage( ).

           ELSE.

             lv_cp = iv_codepage.

           ENDIF.

*       convert string to xstring using class cl_abap_conv_out_ce

*       in this form available also in 620

           lo_conv = cl_abap_conv_out_ce=>create(

             encoding = lv_cp

             ignore_cerr = ‘X’ ).

           lo_conv->write( data = iv_string ).

           ev_xstring = lo_conv->get_buffer( ).

*       add the byte order mark

           IF iv_add_bom = ‘X’.

             CASE lv_cp.

               WHEN ‘4110’.                                  “UTF-8

                 lv_bom = cl_abap_char_utilities=>byte_order_mark_utf8.

               WHEN ‘4102’.                                  “UTF-16BE

                 lv_bom = cl_abap_char_utilities=>byte_order_mark_big.

               WHEN ‘4103’.                                  “UTF-16LE

                 lv_bom = cl_abap_char_utilities=>byte_order_mark_little.

             ENDCASE.

             IF lv_bom IS NOT INITIAL.

               CONCATENATE lv_bom ev_xstring INTO ev_xstring IN BYTE MODE.

             ENDIF.

           ENDIF.

         ENDIF.

       CATCH cx_root.                                        “#EC *

         RAISE EXCEPTION TYPE cx_bcs

           EXPORTING

             error_type = cx_bcs=>creation_failed.

     ENDTRY.

endmethod.

if you want to add IDOC or any standard document as attachment then use below code.

* Populate object type and object key for create an instance

     DATAlv_objtype TYPE swo_objtyp,

           lv_objkey   TYPE swo_typeid,

           lv_sofm     TYPE swotrtimeobject,

           lv_return   TYPE swotreturn.

     lv_objtype = ‘IDOCORDERS’.

     lv_objkey  = ‘0000000022238440’.

*Creating SOFM object

     CALL FUNCTION ‘SWO_CREATE’

       EXPORTING

         objtype           = lv_objtype

         objkey            = lv_objkey

       IMPORTING

         object            = lv_sofm

         return            = lv_return

       EXCEPTIONS

         no_remote_objects = 1

         OTHERS            = 2.

* Prepare for attaching the object to container

     DATA: lv_object TYPE obj_record,

           tb_obj    TYPE TABLE OF obj_record.

     lv_objectheader = ‘OBJH’.

     lv_objecttype    = ‘BO’.

     lv_objecthandle  = lv_sofm.

     APPEND lv_object TO tb_obj.

     CALL METHOD lv_container->set

       EXPORTING

         name  = ‘_ATTACH_OBJECTS’

         value = lv_object.


Now once we are done with class creation add this class in the program exits tab of email step.

/wp-content/uploads/2013/11/attach_330508.png

Now we are done activate the workflow and execute and check the email in SAP inbox.

/wp-content/uploads/2013/11/email_330512.jpg

You can use similar method to add PDF or other attachments.

Assigned Tags

      5 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Sai Krishna
      Sai Krishna

      Nice work buddy Thumps Up 🙂

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      Thanks for your kind word.

      Author's profile photo Andrea Frati
      Andrea Frati

      hi, when i try to implement a class i receive this error type "Il tipo "SWOTOBJID–OBJTYPE" is unknown

      how can solve this problem ?

      thanks

      Author's profile photo Jiaao Yu
      Jiaao Yu

      many years later I face the same problem, and that because the format of symbol '-'

      there are many other same issues like this

      mark, if it will help others

      Author's profile photo Martin Kejmar
      Martin Kejmar

      Hi Experts,

      i have implemented this solution to my scenario (sending workflow email with attachement) and in the step of calling function SWO_CREATE I became an exception. The parameter exporting into this function module seems good - at least these parameters are filled with some values. I am new in this workflow things and I would like to ask you for help. Is it possible to conclude some solution from information that i give you ?

      Many thanks,

      Martin