Launching a Workflow from an ABAP Program

Launching a Workflow from an ABAP Program

Problem Statement

In one of my previous posts about workflows, I shared my perspective on topics related to business objects and events. These entities are typically used in scenarios where a consultant needs to answer the question: "What should trigger the start of a workflow?"

See the note: SAP Workflow. Business Objects and Events

It is not uncommon to encounter situations where a workflow needs to be triggered directly from an ABAP program. This post focuses specifically on such scenarios.

Solution

SAP provides a wide range of standard functional modules that allow you to work with workflows directly from ABAP programs. You can search for them using the pattern SAP_WAPI* in transaction SE37.

The main function module to pay attention to is called SAP_WAPI_START_WORKFLOW. Using it, you can start a workflow and also pass values to the workflow container via the input_container parameter.

As an example, let’s consider a program that starts a workflow with the ID WS77300061 upon execution:

REPORT zprogram10.
 
DATA: lv_rc          TYPE sy-subrc,
      lt_workitem_id TYPE STANDARD TABLE OF swr_cont WITH HEADER LINE,
      lv_task        TYPE swr_struct-task,
      lt_table       TYPE TABLE OF swwwihead.
 
PARAMETERS: p1 AS CHECKBOX.
 
IF p1 EQ abap_true.
 
  lv_task = 'WS77300061'.
 
  CALL FUNCTION 'SAP_WAPI_START_WORKFLOW'
    EXPORTING
      task        = lv_task
      do_commit   = 'X'
      user        = sy-uname
    IMPORTING
      return_code = lv_rc.
 
  IF sy-subrc <> 0.
 
  ENDIF.
 
  CALL FUNCTION 'SWW_WI_TASK_INSTANCES_READ'
    EXPORTING
      task              = lv_task
    TABLES
      workitems_of_task = lt_table.
  .
 
  SORT lt_table BY wi_ct DESCENDING.
 
  READ TABLE lt_table ASSIGNING FIELD-SYMBOL(<fs_table>) INDEX 1.
  WRITE:/ 'Workflow with id # ', <fs_table>-wi_id , ' was created'.
 
ELSE.
  WRITE:/ ':('.
ENDIF.

A video demonstrating the result of this program is available below.

0:00
/0:47

In addition to directly starting a workflow, it’s important to remember that events can also serve as triggers for launching workflows. Triggering an event from an ABAP program is also straightforward. The function module SAP_WAPI_CREATE_EVENT can help with this.

Before calling this function module, ensure that your workflow has a defined start event.

See: Using Events

As an illustration, let’s look at a workflow whose start event is linked to the event HIRED of the business object EMPLOYEET. We’ll create a copy of the workflow WS77300061 for this example.
0:00
/0:57

Check transaction SWETYPV

See: Evaluation and Maintenance of Type Linkages

Back in ABAP, for example:

REPORT zprogram11.
 
PARAMETERS: p_1 AS CHECKBOX.
 
IF p_1 EQ abap_true.
 
  CALL FUNCTION 'SAP_WAPI_CREATE_EVENT'
    EXPORTING
      object_type = 'EMPLOYEET'
      object_key  = ' '
      event       = 'HIRED'
    .
  IF sy-subrc <> 0.
  ENDIF.
 
  WRITE:/ 'Event Raised!'.
ELSE.
  WRITE:/ 'No event raised'.
ENDIF.

Result

0:00
/0:40

Spring is just around the corner.
Thanks for your time. Hugs.