Homelab, Linux, JS & ABAP (~˘▾˘)~
 

[ABAP] Z_CLEANUP_LRQ_WORKFLOWS

A cleanup report I’ve written some years ago, to end incomplete leave request workflows. I couldn’t figure out why some workflows hadn’t been completed over the years. Since the absences were from a long time ago, the goal was simply to clean up the system. Maybe someone else will need to do something similar someday.

*&---------------------------------------------------------------------*
*& Report Z_CLEANUP_LRQ_WORKFLOWS
*&---------------------------------------------------------------------*
*& This report was written to end old absence request workflows,
*& where the absence request got already approved,
*& but somehow the WF got not set to status 'COMPELTED'.
*& https://me.sap.com/notes/0002650820
*&---------------------------------------------------------------------*
REPORT z_cleanup_lrq_workflows.

TABLES: ptreq_header, swwwihead, pa0002.

TYPES:
  BEGIN OF ty_log,
    icon          TYPE icon-id,
    objid         TYPE objektid,
    vorna         TYPE vorna,
    nachn         TYPE nachn,
    request_id    TYPE ptreq_header-request_id,
    version_no    TYPE tim_req_version,
    request_type  TYPE tim_req_type,
    status        TYPE tim_req_status,
    mod_timestamp TYPE timestamp,
    timestamp     TYPE string,
    workitem_id	  TYPE sww_wiid,
    wi_stat       TYPE sww_wistat,
    wi_stat_new   TYPE sww_wistat,
    comment       TYPE string,
  END OF ty_log.

DATA gt_log     TYPE TABLE OF ty_log WITH DEFAULT KEY.
DATA return     TYPE TABLE OF swl_return.
DATA new_status TYPE sww_wistat.
DATA status     TYPE ptarq_uia_request-status.

INITIALIZATION.

  SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-b01.

    SELECT-OPTIONS: s_pernr FOR pa0002-pernr              MATCHCODE OBJECT prem,
                    s_guid  FOR ptreq_header-request_id,
                    s_rtype FOR ptreq_header-request_type DEFAULT 'ABSREQ',
                    s_statu FOR status                    DEFAULT 'POSTED',
                    wi_stat FOR swwwihead-wi_stat         DEFAULT 'STARTED'.

    PARAMETERS: fdate  TYPE datum DEFAULT '20180101',
                ftime  TYPE tims,
                tdate  TYPE datum DEFAULT '20231231',
                ttime  TYPE tims,
                p_test AS CHECKBOX DEFAULT abap_true.

  SELECTION-SCREEN END OF BLOCK b1.

START-OF-SELECTION.

  CONVERT DATE fdate TIME ftime INTO TIME STAMP DATA(ts_from) TIME ZONE 'UTC'.
  CONVERT DATE tdate TIME ttime INTO TIME STAMP DATA(ts_upto) TIME ZONE 'UTC'.

  SELECT a~request_id, a~version_no, a~status, a~request_type, a~mod_timestamp, a~workitem_id, b~objid, c~wi_stat, d~vorna, d~nachn
    FROM ptreq_header    AS a
        JOIN ptreq_actor AS b ON a~initiator_ins = b~actor_id
        JOIN swwwihead   AS c ON a~workitem_id   = c~wi_id
        JOIN pa0002      AS d ON b~objid         = d~pernr
    INTO CORRESPONDING FIELDS OF TABLE @gt_log
    WHERE a~request_id    IN @s_guid
      AND a~status        IN @s_statu
      AND a~request_type  IN @s_rtype
      AND a~mod_timestamp BETWEEN @ts_from AND @ts_upto
      AND b~objid         IN @s_pernr
      AND c~wi_stat       IN @wi_stat.

END-OF-SELECTION.

  LOOP AT gt_log ASSIGNING FIELD-SYMBOL(<log>).

    <log>-timestamp = |{ <log>-mod_timestamp TIMESTAMP = ENVIRONMENT }|.

    IF p_test = abap_true.
      <log>-comment = |Testrun! No workflow changed!|.
      CONTINUE.
    ENDIF.

    IF <log>-wi_stat <> |STARTED|. CONTINUE. ENDIF.

    CALL FUNCTION 'SWL_ADM_WI_COMPLETE'
      EXPORTING
        wi_id                         = <log>-workitem_id
      TABLES
        return                        = return
      EXCEPTIONS
        workitem_not_found            = 1
        no_authority                  = 2
        wrong_workitem_type           = 3
        workflow_definition_not_found = 4
        internal_error                = 5
        action_canceled               = 6
        OTHERS                        = 7.
    IF sy-subrc <> 0.
      <log>-icon    = icon_red_light.
      <log>-comment = COND #( WHEN lines( return ) > 0 THEN return[ 1 ]-text
                                                       ELSE SWITCH #( sy-subrc WHEN 1 THEN |workitem_not_found|
                                                                               WHEN 2 THEN |no_authority|
                                                                               WHEN 3 THEN |wrong_workitem_type|
                                                                               WHEN 4 THEN |workflow_definition_not_found|
                                                                               WHEN 5 THEN |internal_error|
                                                                               WHEN 6 THEN |action_canceled|
                                                                               WHEN 7 THEN |OTHERS| ) ).
    ELSE.
      " Check if WF status really got changed
      SELECT SINGLE wi_stat FROM swwwihead INTO <log>-wi_stat_new WHERE wi_id = <log>-workitem_id.
      <log>-icon    = COND #( WHEN <log>-wi_stat_new <> |COMPLETED| THEN icon_red_light ELSE icon_green_light ).
      <log>-comment = COND #( WHEN <log>-wi_stat_new <> |COMPLETED| THEN |Something went wrong!| ).
    ENDIF.

  ENDLOOP.


  IF gt_log IS INITIAL.
    MESSAGE i002(wusl) DISPLAY LIKE 'E'.
  ELSE.
    TRY.
        cl_salv_table=>factory( IMPORTING r_salv_table = DATA(alv_table)
                                CHANGING  t_table      = gt_log ).

        alv_table->get_columns( )->get_column( 'ICON'          )->set_long_text( 'Icon' ).
        alv_table->get_columns( )->get_column( 'WI_STAT_NEW'   )->set_long_text( 'Neuer WF Status' ).
        alv_table->get_columns( )->get_column( 'WI_STAT_NEW'   )->set_medium_text( 'Neuer Status' ).
        alv_table->get_columns( )->get_column( 'COMMENT'       )->set_long_text( 'Kommentar' ).
        alv_table->get_columns( )->get_column( 'TIMESTAMP'     )->set_long_text( 'Letzte Änderung' ).
        alv_table->get_columns( )->get_column( 'COMMENT'       )->set_output_length( 100 ).
        alv_table->get_columns( )->get_column( 'TIMESTAMP'     )->set_output_length( 20 ).
        alv_table->get_columns( )->get_column( 'MOD_TIMESTAMP' )->set_visible( abap_false ).

        alv_table->display( ).

      CATCH cx_root.
    ENDTRY.
  ENDIF.

Leave a Reply

Your email address will not be published. Required fields are marked *