SAP Workflow: Using Function Modules in Responsibility Rules

In one of my previous notes on workflow, I touched on the use of responsibility rules for determining the agents of dialog tasks.

See note: Responsibility Rules in Workflow Configuration

In this note, I’d like to continue exploring the capabilities of responsibility rules—this time, with some ABAP involved and using a real-life example.

0. Task Definition

Create a responsibility rule that will determine the agents of a dialog task in a workflow. The rule will receive an object ID of type S (Position). In infotype 1001 (Relationships), this position is linked to multiple P (Person) objects via relationship type ZZ1. The goal is to fetch the relevant P object IDs and then read their 0105 (Communication) infotype, subtype 0001 (System User Name / SY-UNAME).

1. Creating the Function Module

According to the task, I need to create a function module that determines the future agents of the workflow task. The logic was described above.

For testing purposes, I’ll use a Position (S) object that has 2 relevant connections:

Here’s the ABAP code I’ll use to retrieve the required data:

DATA: lw_actor TYPE swhactor.

DATA: lt_1001 TYPE TABLE OF p1001,
lw_1001 TYPE p1001,
lv_pernr TYPE prelp-pernr.

CALL FUNCTION 'RH_READ_INFTY_1001'
EXPORTING
plvar = '01'
otype = 'S'
objid = '50005809' "
istat = '1'
begda = '19000101'
endda = '99991231'
TABLES
i1001 = lt_1001
EXCEPTIONS
nothing_found = 1
wrong_condition = 2
wrong_parameters = 3
OTHERS = 4.
IF sy-subrc is INITIAL.
IF lt_1001 IS NOT INITIAL.
LOOP AT lt_1001 INTO lw_1001 WHERE relat = 'ZZ1'.

  lv_pernr = lw_1001-sobid.

  CLEAR lw_actor.
  lw_actor-otype = 'US'.
  lw_actor-objid = zcl_demo_utility=>get_userid( lv_pernr = lv_pernr ).
  APPEND lw_actor TO actor_tab.
ENDLOOP.
ENDIF.
ENDIF.

Testing:

0:00
/0:52

The user IDs that will act as workflow agents are successfully written into the ACTOR_TAB table.

See: Interface of Function Module for Agent Determination
See: Standard FM SWX_GET_MANAGER

2. Creating the Responsibility Rule

Responsibility rules come in different types. The one discussed in this note is well described in SAP's documentation:

See:  Defining Rules Using Function to Be Executed

Use transaction PFAC to create a rule of type F – Agent Determination Function to be Executed.

0:00
/0:39

3. Preparing the Workflow

Your workflow must contain a container element to hold the position ID from which agents will be determined. I’ll name this element TEST_OBJID. For demonstration, I’ll assign an initial value to TEST_OBJID.

0:00
/0:22

This value will later be passed to the responsibility rule for determining the agents.

Assign the created responsibility rule (see step 2) to the workflow task responsible for user approval.

0:00
/0:22

4. Adjusting the Function Module

In step 1, we hardcoded the position object ID. That was fine for testing, but now we must update the code to dynamically read the object ID from the workflow container (TEST_OBJID).

Here's the modified ABAP code:

DATA: lw_actor TYPE swhactor,
      test_objid TYPE p1001-objid.

DATA: lt_1001 TYPE TABLE OF p1001,
lw_1001 TYPE p1001,
lv_pernr TYPE prelp-pernr.

FIELD-SYMBOLS: <fs_cont> TYPE swcont.

READ TABLE ac_container ASSIGNING <fs_cont> WITH KEY element = 'TEST_OBJID'.
test_objid = <fs_cont>-value.

CALL FUNCTION 'RH_READ_INFTY_1001'
EXPORTING
plvar = '01'
otype = 'S'
objid = test_objid
istat = '1'
begda = '19000101'
endda = '99991231'
TABLES
i1001 = lt_1001

EXCEPTIONS
  nothing_found    = 1
  wrong_condition  = 2
  wrong_parameters = 3
  OTHERS           = 4.
IF sy-subrc <> 0.

ENDIF.
IF lt_1001 IS NOT INITIAL.
LOOP AT lt_1001 INTO lw_1001 WHERE relat = 'ZZ1'.

  lv_pernr = lw_1001-sobid.

  CLEAR lw_actor.
  lw_actor-otype = 'US'.
  lw_actor-objid = zcl_demo_utility=>get_userid( lv_pernr = lv_pernr ).
  APPEND lw_actor TO actor_tab.
ENDLOOP.
ENDIF.

Yes, this code lacks error handling and validation—but the main point here is understanding the principle. Polishing the code is easy once the logic is clear.

See: Function Module for Rule Resolution

5. Testing

For testing purposes, you can use background debugging of a task.

See note: SAP Workflow. Debugging Background Tasks
0:00
/1:25

All in all, everything turned out quite well. Thanks for your attention!