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.
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.

Leave a Reply