---
title: "转 Program Exits in Work Item for Workflow "
author: "evangxli"
date: "2013-08-15T09:44:57Z"
tags: []
categories: ["行业应用"]
slug: "program-exits-in-work-item-for-workflow"
draft: false
lastmod: "2013-08-15T09:44:57Z"
id: 1917178
url: "https://sapabap.iteye.com/blog/1917178"
---

## Program Exits in Work Item for Workflow

By Anirban Bhattacharjee, KPIT Cummins and Infosystems from [Link](http://www.saptechnical.com/)

**Purpose:** The purpose of this document is to demonstrate how to use Program Exits in a Work Item for Workflow.

**Business Scenario:** Many times we have scenarios where we want to do some additional custom processing when a work item is created or executed. 

SAP has given us a way by which we can design Program Exits in a work item. The events on which we can program this are mentioned below

- Before Work Item Creation
- After Work Item Creation
- Before Work Item Execution
- After Work Item Execution
- After the execution of a Synchronous Object Method in the work item
- Before Physical Deletion of the Work Item
- On the Status Change of a Work Item
- On a Rule execution in the Work Item
- After an action execution in the Work Item
- Before and action execution in the Work Item

We will see that the class we will model later on in this tutorial will contain the methods that can be implemented for handling the above events.

**Here we will send e-mail via this Program Exit once when a work item is created and once when it is executed.**

There will be no e-mail steps modeled in the workflow. They will happen solely from the program exit.

**Process to design the Program Exit in a Work Item**

Let us look at the tab in a work item where this exit is designed

![](http://www.saptechnical.com/Tutorials/Workflow/Exit/Index.9.jpg)

Note that the SAP Documentation states that the class that will be modeled here must support the **IF\_SWF\_IFS\_WORKITEM\_EXIT** interface. Please ensure that inside this class we will not be using any ABAP Instruction that can release the current LUW like COMMIT WORK, ROLLBACK WORK or using RFC Function Calls.

Now we will design a class via the transaction SE24. We will call this class **ZZCL\_WORK\_ITEM\_EXIT**.

The ***Properties*** tab of the class will look as shown. We will add the type group **SWRCO**

![](http://www.saptechnical.com/Tutorials/Workflow/Exit/Index.10.jpg)

We will now declare the interface **IF\_SWF\_IFS\_WORKITEM\_EXIT** in the interface tab

![](http://www.saptechnical.com/Tutorials/Workflow/Exit/Index.11.jpg)

Once we declare the interface, the attributes related to this interface will appear in the ***Attributes*** Tab.

![](http://www.saptechnical.com/Tutorials/Workflow/Exit/Index.12.jpg)

Notice the events that can happen with a work item in the ***Description*** column. **This is what was mentioned at the start of the tutorial.**

We will now add a couple of custom attributes as mentioned below:

**WI\_CONTEXT** as a type reference to **IF\_WAPI\_WORKITEM\_CONTEXT**; this interface returns the various attributes of a work item.

Displaying this interface via **SE24** and showing you the methods

![](http://www.saptechnical.com/Tutorials/Workflow/Exit/Index.13.jpg)

To see how to implement these methods in a class, please refer **CL\_SWF\_RUN\_WORKITEM\_CONTEXT.**

This class is very helpful to understand how to do various operations on a work item.

The other custom attribute will be named **PROCESS\_STATUS**. This will contain a default value **‘sap.bc.bmt.wfm.process.status’.**

The attributes tab now looks like as shown below

![](http://www.saptechnical.com/Tutorials/Workflow/Exit/Index.14.jpg)

Both the attributes are ***Instance Attribute*** and ***Private*** visibility.

Now let us navigate to the ***Methods*** Tab of this class.

![](http://www.saptechnical.com/Tutorials/Workflow/Exit/Index.15.jpg)

The only method created by default is **EVENT\_RAISED**. This will be called whenever the Work Item Events (mentioned at the start of the tutorial) are triggered.

The parameters that appear in this method are

![](http://www.saptechnical.com/Tutorials/Workflow/Exit/Index.16.jpg)

(Note that there is a parameter **IM\_WORKITEM\_CONTEXT** which refers interface **IF\_WAPI\_WORKITEM\_CONTEXT**)

Now we will create two more methods called **AFTER\_WI\_CREATION** and **AFTER\_WI\_EXECUTION** as shown.

![](http://www.saptechnical.com/Tutorials/Workflow/Exit/Index.17.jpg)

Both the methods are ***Instance Method*** with ***Private*** Visibility.

Write the following code in the **AFTER\_WI\_CREATION** method

(This code will send an e-mail to a dummy e-mail ID with the Work Item ID stating that it was **Created.** Will trigger after work item is created)

METHOD after\_wi\_creation.

  DATA: lcl\_v\_wi\_id        TYPE sww\_wiid,  "Work Item ID  
        lv\_wid\_text        TYPE char12,  
        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.

* 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-&gt;get\_workitem\_id  
    RECEIVING  
      re\_workitem = lcl\_v\_wi\_id.

* Pass WID to text field  
  CLEAR: lv\_wid\_text.  
  lv\_wid\_text = lcl\_v\_wi\_id.

\*----------------------------------------------------------------------------------------*  
* 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=&gt;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.

      APPEND body\_text TO text.  
      document = cl\_document\_bcs=&gt;create\_document(  
                      i\_type    = 'RAW'  
                      i\_text    = text  
                      i\_length  = '12'  
                      i\_subject = 'E-Mail sent AFTER Work Item Creation' ).

*     Add document to send request  
      CALL METHOD send\_request-&gt;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=&gt;create( sy-uname ).  
      CALL METHOD send\_request-&gt;set\_sender  
        EXPORTING  
          i\_sender = sender.

*     --------- Add recipient (e-mail address) -----------------------  
*     Create recipient - passing a dummy e-mail ID here  
      recipient = cl\_cam\_address\_bcs=&gt;create\_internet\_address('anirban@testtest.com' ).

*     Add recipient with its respective attributes to send request  
      CALL METHOD send\_request-&gt;add\_recipient  
        EXPORTING  
          i\_recipient = recipient  
          i\_express   = 'X'.

*     ---------- Send document ---------------------------------------  
      CALL METHOD send\_request-&gt;send(  
        EXPORTING  
          i\_with\_error\_screen = 'X'  
        RECEIVING  
          result              = sent\_to\_all ).

      COMMIT WORK.

* -----------------------------------------------------------  
* *                     exception handling  
* -----------------------------------------------------------  
    CATCH cx\_bcs INTO bcs\_exception.  
*  Write own code to catch exception  
  ENDTRY.

ENDMETHOD.

Write the following code in the **AFTER\_WI\_EXECUTION** method

(This code will send an e-mail to a dummy e-mail ID with the Work Item ID stating that it was **Executed.** Will trigger after work item is executed)

METHOD after\_wi\_creation.

  DATA: lcl\_v\_wi\_id        TYPE sww\_wiid,  "Work Item ID  
        lv\_wid\_text        TYPE char12,  
        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.

* 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-&gt;get\_workitem\_id  
    RECEIVING  
      re\_workitem = lcl\_v\_wi\_id.

* Pass WID to text field  
  CLEAR: lv\_wid\_text.  
  lv\_wid\_text = lcl\_v\_wi\_id.

\*----------------------------------------------------------------------------------------*  
* 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=&gt;create\_persistent( ).

*     -------- create and set document -------------------------------  
*     Build the e-mail Body  
      CLEAR: body\_text.  
      CONCATENATE 'Work Item EXECUTED. WID:'  
                  lv\_wid\_text INTO body\_text SEPARATED BY space.

      APPEND body\_text TO text.  
      document = cl\_document\_bcs=&gt;create\_document(  
                      i\_type    = 'RAW'  
                      i\_text    = text  
                      i\_length  = '12'  
                      i\_subject = 'E-Mail sent AFTER Work Item EXECUTION' ).

*     Add document to send request  
      CALL METHOD send\_request-&gt;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=&gt;create( sy-uname ).  
      CALL METHOD send\_request-&gt;set\_sender  
        EXPORTING  
          i\_sender = sender.

*     --------- Add recipient (e-mail address) -----------------------  
*     Create recipient - passing a dummy e-mail ID here  
      recipient = cl\_cam\_address\_bcs=&gt;create\_internet\_address('anirban@testtest.com' ).

*     Add recipient with its respective attributes to send request  
      CALL METHOD send\_request-&gt;add\_recipient  
        EXPORTING  
          i\_recipient = recipient  
          i\_express   = 'X'.

*     ---------- Send document ---------------------------------------  
      CALL METHOD send\_request-&gt;send(  
        EXPORTING  
          i\_with\_error\_screen = 'X'  
        RECEIVING  
          result              = sent\_to\_all ).

      COMMIT WORK.

* -----------------------------------------------------------  
* *                     exception handling  
* -----------------------------------------------------------  
    CATCH cx\_bcs INTO bcs\_exception.  
*  Write own code to catch exception  
  ENDTRY.

ENDMETHOD.

Write the following code in the method **EVENT\_RAISED**. In this method based on the Work Item event triggered, our corresponding custom method will be called.

METHOD if\_swf\_ifs\_workitem\_exit~event\_raised.

* Get the Work Item Context  
  me-&gt;wi\_context = im\_workitem\_context.

* Check if the Event after WI Creation is triggered  
  IF im\_event\_name = swrco\_event\_after\_creation.

* Call our method AFTER\_WI\_CREATION  
    me-&gt;after\_wi\_creation( ).

* Check if the Event after WI Execution is triggered  
  ELSEIF im\_event\_name = swrco\_event\_after\_execution.

* Call our method AFTER\_WI\_EXECUTION  
    me-&gt;after\_wi\_execution( ).

  ENDIF.

ENDMETHOD.

![](http://www.saptechnical.com/Tutorials/Workflow/Exit/Page2.18.jpg)

Now save and activate the complete class **ZZCL\_WORK\_ITEM\_EXIT** along with all its components.

**![](http://www.saptechnical.com/Tutorials/Workflow/Exit/Page3.19.jpg)**

Now we will call this class in our user decision step in the workflow at the tab **Program Exits** as shown below  

![](http://www.saptechnical.com/Tutorials/Workflow/Exit/Page3.20.jpg)

Now we save and activate the complete workflow definition as well.

Notice that there are no e-mail steps after the user decision anywhere in the workflow definition.

![](http://www.saptechnical.com/Tutorials/Workflow/Exit/Page3.21.jpg)

Now we will test the workflow via SWUS transaction. When the user decision step is created, the first e-mail should go.

![](http://www.saptechnical.com/Tutorials/Workflow/Exit/Page3.22.jpg)

The work item ID is 412083 as seen below

![](http://www.saptechnical.com/Tutorials/Workflow/Exit/Page3.23.jpg)

Checking in SOST transaction if the Work Item Creation E-Mail has triggered 

![](http://www.saptechnical.com/Tutorials/Workflow/Exit/Page3.24.jpg)

![](http://www.saptechnical.com/Tutorials/Workflow/Exit/Page3.25.jpg)

So we have successfully sent the **first e-mail to the dummy e-mail ID**, which went **After** **Work Item Creation** via the program exit.

Notice the work item ID is **412083**. This is correct as per workflow logs.

Now we will execute the work item. After execution, we will see the workflow log

**BEFORE EXECUTION**

![](http://www.saptechnical.com/Tutorials/Workflow/Exit/Page3.26.jpg)

**AFTER EXECUTION**

![](http://www.saptechnical.com/Tutorials/Workflow/Exit/Page3.27.jpg)

Checking SOST if the e-mail after WI execution has triggered

![](http://www.saptechnical.com/Tutorials/Workflow/Exit/Page3.28.jpg)

![](http://www.saptechnical.com/Tutorials/Workflow/Exit/Page3.29.jpg)

So we have successfully sent the **second e-mail to the dummy e-mail ID**, which went **After** **Work Item Execution** via the program exit.

**Conclusion:** Hence we have successfully modeled the Program Exit that can be used in a work item. With the help of this exit, we can do numerous custom operations at various events, which occur during the complete life span of a work item.