Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
bpawanchand
Active Contributor

        After a long gap, I could find time, a good mood to write an article and submit. Recently when I was going through the posts of SAP Workflow forums, I found lot of similar threads which are discussing about sending mails when a specific task instance is created and attachments to work item.

        The main focus of this article is not about sending mails when a work item is created, currently the main focus of this article would be to creating an attachment dynamically. The attachment can be a .DOC, .XLS, .PDF, .XML.

        The term dynamic here refers to the data and content that are populated during the workflow execution that can be either populated in the workflow container or work item container. This article specifically deals with creating a excel sheet and it can be extended to such an extent where you can even populate list values inside the generated excel sheet.

OK…… let me say you how it can be done in simple 3 steps.

  1. Create a Business Class by including IF_WORKFLOW interface in the class.
  2. Create a Simple Transformation,to populate the values in the excel sheet call the transformation from a method created in the business class.
  3. Create a Standard task and include the new business class and method name and define the proper binding and finally include this task in a workflow.

Create a new simple transformation from object navigator (SE80)

<?sap.transform simple?>

<tt:transform xmlns:tt="http://www.sap.com/transformation-templates">
   <?mso-application progid="Excel.Sheet"?>

   <tt:root name="APPL_STAT"/>

   <tt:template>

     <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
     xmlns:o="urn:schemas-microsoft-com:office:office"
     xmlns:x="urn:schemas-microsoft-com:office:excel"
     xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">

       <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
         <LastSaved>2011-06-09T09:12:23Z</LastSaved>
         <Created>2006-09-16T00:00:00Z</Created>
         <Version>14.00</Version>
       </DocumentProperties>
       <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
         <AllowPNG/>
         <RemovePersonalInformation/>
       </OfficeDocumentSettings>
       <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
         <WindowHeight>10545</WindowHeight>
         <WindowWidth>22035</WindowWidth>
         <WindowTopX>240</WindowTopX>
         <WindowTopY>45</WindowTopY>
         <ProtectStructure>False</ProtectStructure>
         <ProtectWindows>False</ProtectWindows>
       </ExcelWorkbook>
       <Style ss:ID="Default" ss:Name="Normal">
         <Alignment ss:Vertical="Bottom"/>
         <Borders/>
         <Font ss:Color="#000000" ss:FontName="Calibri" ss:Size="11" x:Family="Swiss"/>
         <Interior/>
         <NumberFormat/>
         <Protection/>
       </Style>
       <Styles>
         <Style ss:ID="Default" ss:Name="Normal">
           <Alignment ss:Vertical="Bottom"/>
           <Borders/>
           <Font ss:Color="#000000" ss:FontName="Calibri" ss:Size="11" x:Family="Swiss"/>
           <Interior/>
           <NumberFormat/>
           <Protection/>
         </Style>
         <style ss:ID="s77">
           <Alignment ss:Vertical="Center"/>
           <Font ss:Bold="1" ss:Color="#000000" ss:FontName="Calibri" ss:Size="11" x:Family="Swiss"/>
           <Interior ss:Color="#E46D0A" ss:Pattern="Solid"/>
         </style>
         <style ss:ID="s77">
           <Alignment ss:Vertical="Center"/>
           <Font ss:Bold="1" ss:Color="#000000" ss:FontName="Calibri" ss:Size="11" x:Family="Swiss"/>
           <Interior ss:Color="#E46D0A" ss:Pattern="Solid"/>
         </style>
         <style ss:ID="s78">
           <Alignment ss:Vertical="Center"/>
           <Interior ss:Color="#E46D0A" ss:Pattern="Solid"/>
         </style>
         <style ss:ID="s78">
           <Alignment ss:Vertical="Center"/>
           <Interior ss:Color="#E46D0A" ss:Pattern="Solid"/>
         </style>
       </Styles>
       <Worksheet ss:Name="Leave Requests">
         <Table ss:ExpandedColumnCount="100" ss:ExpandedRowCount="20000" x:FullColumns="1" x:FullRows="1">
           <Column ss:Width="185.25"/>
           <Column ss:Width="66.75"/>
           <Column ss:Width="74.25"/>
           <Column ss:Width="56.25"/>
           <Column ss:Span="4" ss:Width="185.25"/>
           <Column ss:Index="10" ss:Width="183.75"/>
           <Column ss:Width="185.25"/>
           <Column ss:Width="183.75"/>
           <Column ss:Width="185.25"/>
           <Column ss:Width="57"/>
           <Column ss:Width="103.5"/>
           <Column ss:Width="82.5"/>
           <Column ss:Width="72.75"/>
           <Row>
             <Cell ss:StyleID="s77">
               <Data ss:Type="String">REQUEST_ID</Data>
             </Cell>
             <Cell ss:StyleID="s77">
               <Data ss:Type="String">VERSION_NO</Data>
             </Cell>
             <Cell ss:StyleID="s77">
               <Data ss:Type="String">REQUEST_TYPE</Data>
             </Cell>
             <Cell ss:StyleID="s77">
               <Data ss:Type="String">STATUS</Data>
             </Cell>
             <Cell ss:StyleID="s77">
               <Data ss:Type="String">INITIATOR_CLS</Data>
             </Cell>
             <Cell ss:StyleID="s77">
               <Data ss:Type="String">INITIATOR_INS</Data>
             </Cell>
             <Cell ss:StyleID="s77">
               <Data ss:Type="String">OWNER_CLS</Data>
             </Cell>
             <Cell ss:StyleID="s77">
               <Data ss:Type="String">OWNER_INS</Data>
             </Cell>
             <Cell ss:StyleID="s77">
               <Data ss:Type="String">NEXT_PROCESSOR_C</Data>
             </Cell>
             <Cell ss:StyleID="s77">
               <Data ss:Type="String">NEXT_PROCESSOR_I</Data>
             </Cell>
             <Cell ss:StyleID="s77">
               <Data ss:Type="String">RESPONSIBLE_CLS</Data>
             </Cell>
             <Cell ss:StyleID="s77">
               <Data ss:Type="String">RESPONSIBLE_INS</Data>
             </Cell>
             <Cell ss:StyleID="s77">
               <Data ss:Type="String">ITEM_LIST_ID</Data>
             </Cell>
             <Cell ss:StyleID="s77">
               <Data ss:Type="String">MOD_USER</Data>
             </Cell>
             <Cell ss:StyleID="s77">
               <Data ss:Type="String">MOD_TIMESTAMP</Data>
             </Cell>
             <Cell ss:StyleID="s77">
               <Data ss:Type="String">MOD_TIMEZONE</Data>
             </Cell>
             <Cell ss:StyleID="s77">
               <Data ss:Type="String">WORKITEM_ID</Data>
             </Cell>
           </Row>
           <tt:loop ref=".APPL_STAT">
             <Row>
               <Cell>
                 <Data ss:Type="String">
                   <tt:value ref="REQUEST_ID"/>
                 </Data>
               </Cell>
               <Cell>
                 <Data ss:Type="String">
                   <tt:value ref="VERSION_NO"/>
                 </Data>
               </Cell>
               <Cell>
                 <Data ss:Type="String">
                   <tt:value ref="REQUEST_TYPE"/>
                 </Data>
               </Cell>
               <Cell>
                 <Data ss:Type="String">
                   <tt:value ref="STATUS"/>
                 </Data>
               </Cell>
               <Cell>
                 <Data ss:Type="String">
                   <tt:value ref="INITIATOR_CLS"/>
                 </Data>
               </Cell>
               <Cell>
                 <Data ss:Type="String">
                   <tt:value ref="INITIATOR_INS"/>
                 </Data>
               </Cell>
               <Cell>
                 <Data ss:Type="String">
                   <tt:value ref="OWNER_CLS"/>
                 </Data>
               </Cell>
               <Cell>
                 <Data ss:Type="String">
                   <tt:value ref="OWNER_INS"/>
                 </Data>
               </Cell>
               <Cell>
                 <Data ss:Type="String">
                   <tt:value ref="NEXT_PROCESSOR_C"/>
                 </Data>
               </Cell>
               <Cell>
                 <Data ss:Type="String">
                   <tt:value ref="NEXT_PROCESSOR_I"/>
                 </Data>
               </Cell>
               <Cell>
                 <Data ss:Type="String">
                   <tt:value ref="RESPONSIBLE_CLS"/>
                 </Data>
               </Cell>
               <Cell>
                 <Data ss:Type="String">
                   <tt:value ref="RESPONSIBLE_INS"/>
                 </Data>
               </Cell>
               <Cell>
                 <Data ss:Type="String">
                   <tt:value ref="ITEM_LIST_ID"/>
                 </Data>
               </Cell>
               <Cell>
                 <Data ss:Type="String">
                   <tt:value ref="MOD_USER"/>
                 </Data>
               </Cell>
               <Cell>
                 <Data ss:Type="String">
                   <tt:value ref="MOD_TIMESTAMP"/>
                 </Data>
               </Cell>
               <Cell>
                 <Data ss:Type="String">
                   <tt:value ref="MOD_TIMEZONE"/>
                 </Data>
               </Cell>
               <Cell>
                 <Data ss:Type="String">
                   <tt:value ref="WORKITEM_ID"/>
                 </Data>
               </Cell>
             </Row>
           </tt:loop>
         </Table>
         <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
           <Selected/>
           <Panes>
             <Pane>
               <Number>3</Number>
               <ActiveRow>20000</ActiveRow>
               <ActiveCol>20000</ActiveCol>
             </Pane>
           </Panes>
           <ProtectObjects>False</ProtectObjects>
           <ProtectScenarios>False</ProtectScenarios>
         </WorksheetOptions>
       </Worksheet>
     </Workbook>
   </tt:template>
</tt:transform>

Call transformation from the class method or BOR method.Include type group SWC0 in the class or Include the INCLUDE CNTN01_SWC in the class. This is needed because inside the method we have to create the instance of the SOFM by using macro SWC0_CREATE_OBJECT

DATA lt_lv_req          TYPE STANDARD TABLE OF   ptreq_header.
DATA lv_xml             TYPE                     xstring.
DATA lv_text            TYPE                     string.
DATA lo_exception       TYPE REF TO              cx_root.
DATA ls_atthdr          TYPE                     swr_att_header.
DATA ls_attid           TYPE                     swr_att_id.
*--------------------------------------------------------------------*
* Get All the leave request from the DB table                        *
*--------------------------------------------------------------------*
SELECT * FROM ptreq_header
   INTO TABLE lt_lv_req
   UP TO 10 ROWS.
*--------------------------------------------------------------------*
* Call Transformation to generate XSTRING of the generated data      *
*--------------------------------------------------------------------*
TRY .
     CALL TRANSFORMATION zswf_excel_sheet
                  SOURCE appl_stat = lt_lv_req
                  RESULT XML lv_xml.
   CATCH cx_st_error INTO lo_exception.
     lv_text = lo_exception->get_text( ).
ENDTRY.
*--------------------------------------------------------------------*
*  Add Attachment to work item                                       *
*--------------------------------------------------------------------*
ls_atthdr-file_type      = 'B'.
ls_atthdr-file_name      = 'Leave Requests'.
*  ls_atthdr-file_extension = ''.
ls_atthdr-language       = 'EN'.
IF lv_xml IS NOT INITIAL.
   CALL FUNCTION 'SAP_WAPI_ATTACHMENT_ADD'
     EXPORTING
       workitem_id = iv_wiid
       att_header  = ls_atthdr
       att_bin     = lv_xml
       do_commit   = 'X'
     IMPORTING
       att_id      = ls_attid.
ENDIF.
swc0_create_object eo_sofm 'SOFM' ls_attid-doc_id.

Once above class and simple transformations are created then create a standard task and include this task in a workflow.

Binding From Task to Method Container

Make sure that you export the workitem Id and the data that has to be displayed in the attachment, from task container to method container and import the SOFM object instance from Method container to task container

Binding between Task to Workflow Container

Finally export the SOFM object to workflow container and bind the SOFM object to the ATTACH_OBJECTS multi-line container element to the workitem or task, which will be sent to the agent.

When the agent clicks the attachment from the SAP Inbox save the attachment as .XLS. Please find the above screen shot:

UseCase:

  • One of the best use case for the above example is , consider a scenario where you have a Purchase order with different Items created and you need to send a excel sheet with a specific format including all the items that are added in the PO with colors.(a Header - Items Scenario)
  • The other use case is gathering the employee opinion on a specific topic, by populating the user name to whom the workitem is sent with pre poluated list box and finally to the administrator in lock mode or to read the descision of the each user by using parsing classes and updating a database table.
6 Comments
Labels in this area