My Inbox: Auto Approve Workflow Task

You can use below code to approve a workflow task sitting in someones My Inbox app.

There are some prerequisite for below code to work.

  • There should already be a BADI implementation of BADI /IWWRK/ES_WF_WI_BEFORE_UPD_IB to handle approve outcome of the step.
  • You would have to replace WS9.. number in below code to match your workflow number.
  • You would have to replace step id in code to match step id of the task in the workflow you want to approve.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
function zmm_pr_autoapprove.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(WORK_ITEM) TYPE  SWR_STRUCT-WORKITEMID
*"  TABLES
*"      RETURN STRUCTURE  BAPIRETURN
*"----------------------------------------------------------------------
  constants : lc_decision_key type  swr_decikey value '0001'.
 
  data : lo_badi_wi_before_upd type ref to /iwwrk/badi_wf_before_upd_ib,
         lv_work_item          type swr_struct-workitemid,
         ls_wi_wapi_detail     type swr_widtl,
         lt_appl_container_tab type /iwwrk/tt_appl_container,
         lt_wf_container_tab   type /iwwrk/tt_wf_container,
         lt_simple_cont        type standard table of swr_cont,
 
         lv_return_code        type syst-subrc,
         lt_msg_lines          type standard table of swr_messag,
         lt_return             type bapirettab.
 
  field-symbols : <lfs_msg_line> type swr_messag,
                  <lfs_return>   type bapireturn.
 
  lv_work_item = work_item .
 
  "get the customer badi specify the badi filter that are the
  "workflow id and the step number
  try.
      get badi lo_badi_wi_before_upd
        filters
          workflow_id = 'WS93400002'
          step_id     = '0000000004'.
    catch cx_badi_not_implemented.
      return.
  endtry.
 
  "get work item details ---------------------------------------------------------
  call function 'SAP_WAPI_GET_WORKITEM_DETAIL'
    exporting
      workitem_id     = lv_work_item
      user            = sy-uname
      language        = sy-langu
    importing
      workitem_detail = ls_wi_wapi_detail
      return_code     = lv_return_code
    tables
      message_lines   = lt_msg_lines.
 
  "error handling
  if lv_return_code is not initial .
    loop at lt_msg_lines assigning <lfs_msg_line> .
      append initial line to return assigning <lfs_return> .
      <lfs_return>-type    = <lfs_msg_line>-msg_type .
      <lfs_return>-message = <lfs_msg_line>-line .
    endloop .
    return .
  endif .
 
  "container tab ------------------------------------------------------------------
  call function 'SAP_WAPI_READ_CONTAINER'
    exporting
      workitem_id      = lv_work_item
      language         = sy-langu
      user             = sy-uname
    importing
      return_code      = lv_return_code
    tables
      simple_container = lt_simple_cont
      message_lines    = lt_msg_lines.
 
  if lv_return_code is not initial .
    loop at lt_msg_lines assigning <lfs_msg_line> .
      append initial line to return assigning <lfs_return> .
      <lfs_return>-type = <lfs_msg_line>-msg_type .
      <lfs_return>-message = <lfs_msg_line>-line .
    endloop .
    return .
  endif .
 
  "Delete all the attachments objects from the container
  delete lt_simple_cont where element eq /iwwrk/if_wf_constants_gw=>wf_cont_objects-attachment.
  append lines of lt_simple_cont to lt_wf_container_tab.
 
  "this will be executed only if the customer implement handler for the
  "workflow and step id's
  call badi lo_badi_wi_before_upd->before_update
    exporting
      is_wi_details         = ls_wi_wapi_detail
      iv_decision_key       = lc_decision_key
      it_wf_container_tab   = lt_wf_container_tab
      it_appl_container_tab = lt_appl_container_tab
    changing
      ct_return             = lt_return.
 
  read table lt_return assigning field-symbol(<lfs_ret>)
  with key type  = /iwwrk/if_wf_constants_gw=>message_type-error .
  if sy-subrc = 0 .
    append initial line to return assigning <lfs_return> .
    <lfs_return>-type    = <lfs_ret>-type .
    <lfs_return>-message = <lfs_ret>-message .
  else.
    commit work and wait .
  endif.
 
endfunction.

One Reply to “My Inbox: Auto Approve Workflow Task

  1. Fantastic blog! The step-by-step explanation on auto-approving workflow tasks in My Inbox is clear and practical. A great resource for anyone working with SAP ABAP workflows. Thanks for sharing.

Leave a Reply