Skip to end of metadata
Go to start of metadata

Author: Jacek Kopcinski
Submitted: 04.02.2009

Search IDoc by status message (message type, message number). Program can check all statues of the IDoc or just the last one (current).
Copy, activate & use. Enjoy!

*&---------------------------------------------------------------------*
*& Report  Z_SEARCH_IDOCS_BY_STATUS                                    *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*& Search IDoc by Status Message (current status or all statuses)      *
*&                                                                     *
*& BCC, Jacek Kopcinski, 02.2009                                       *
*&                                                                     *
*&---------------------------------------------------------------------*
REPORT z_search_idocs_by_status .

*----------------------------------------------------------------------*
* TABLES                                                               *
*----------------------------------------------------------------------*

TABLES: edids .                           "Status Record (IDoc)
TABLES: edidc .                           "Control record (IDoc)
*----------------------------------------------------------------------*
* TYPES                                                                *
*----------------------------------------------------------------------*

* EDIDS : Status Record (IDoc)
TYPES: BEGIN OF ty_s_idoc ,
    docnum LIKE edids-docnum,             "IDoc Number
    idoctp LIKE edidc-idoctp,
    mestyp LIKE edidc-mestyp,
    credat LIKE edidc-credat,
    countr LIKE edids-countr,
    status LIKE edids-status,
    statxt LIKE edids-statxt,             "Text for status code
    stamid LIKE edids-stamid,             "Status message ID
    stamno LIKE edids-stamno,             "Status message number
END OF ty_s_idoc .

* ABAP List viewer data lines
TYPES: BEGIN OF ty_s_alv1_data .
INCLUDE TYPE ty_s_idoc .
TYPES: END OF ty_s_alv1_data .

TYPE-POOLS: slis.                                   "ABAP List Viewer

TYPE-POOLS: sdydo,                     "Dynamic Documents
            icon.                      "Icon - descriptions

*----------------------------------------------------------------------*
* GLOBAL DATA                                                          *
*----------------------------------------------------------------------*

DATA: gt_idocs TYPE STANDARD TABLE OF ty_s_idoc
                                     WITH HEADER LINE.

* ABAP List viewer data lines
DATA: gt_alv1_data TYPE STANDARD TABLE OF ty_s_alv1_data
                                     WITH HEADER LINE.


*----------------------------------------------------------------------*
* SELECTION-SCREEN                                                     *
*----------------------------------------------------------------------*

SELECTION-SCREEN BEGIN OF BLOCK b0 WITH FRAME TITLE text-b01.
SELECT-OPTIONS: s_credat FOR edidc-credat OBLIGATORY MEMORY ID crd.
SELECT-OPTIONS: s_upddat FOR edidc-upddat.
SELECT-OPTIONS: s_idoctp FOR edidc-idoctp MEMORY ID idc.
SELECT-OPTIONS: s_mestyp FOR edidc-mestyp MEMORY ID mes.
SELECT-OPTIONS: s_direct FOR edidc-direct MEMORY ID dir.
SELECT-OPTIONS: s_status FOR edidc-status OBLIGATORY MEMORY ID sta.
SELECT-OPTIONS: s_stamid FOR edids-stamid MEMORY ID mag.
SELECT-OPTIONS: s_stamno FOR edids-stamno MEMORY ID mno.
PARAMETERS: p_latest TYPE boolean DEFAULT 'X' AS CHECKBOX.
SELECTION-SCREEN END OF BLOCK b0.
*----------------------------------------------------------------------*
START-OF-SELECTION.
*----------------------------------------------------------------------*
  PERFORM ev_start_of_selection.

*----------------------------------------------------------------------*
END-OF-SELECTION.
*----------------------------------------------------------------------*
  PERFORM ev_end_of_selection.

*----------------------------------------------------------------------*
*  F   O   R   M   S                                                   *
*----------------------------------------------------------------------*

*---------------------------------------------------------------------*
*       FORM ALV_LIST_DISPLAY                                         *
*---------------------------------------------------------------------*
*       Invoke main ALV function to format and show the report        *
*---------------------------------------------------------------------*
FORM alv_list_display.

  DATA: ls_layout      TYPE slis_layout_alv,
        ls_print       TYPE slis_print_alv,
        ls_keyinfo     TYPE slis_keyinfo_alv,
        lt_fieldcat    TYPE slis_t_fieldcat_alv,
        lt_exctab      TYPE slis_t_extab,
        lt_sorttab     TYPE slis_t_sortinfo_alv.

  PERFORM alv_init_list_columns  TABLES   lt_sorttab
                                 CHANGING lt_fieldcat
                                          ls_keyinfo.

  PERFORM alv_init_list_layout   TABLES   lt_exctab
                                 CHANGING ls_layout
                                          ls_print.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = sy-cprog
      i_structure_name   = 'GT_ALV1_DATA'
      is_layout          = ls_layout
      it_fieldcat        = lt_fieldcat
      it_excluding       = lt_exctab
      it_sort            = lt_sorttab
      i_save             = 'A'
      is_print           = ls_print
    TABLES
      t_outtab           = gt_alv1_data
    EXCEPTIONS
      program_error      = 1
      OTHERS             = 2.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

ENDFORM.                           " ALV_LIST_DISPLAY

*----------------------------------------------------------------------*
*       Form  alv_init_list_layout
*----------------------------------------------------------------------*
*       Set the list level and print attributes
*----------------------------------------------------------------------*
*      -->OS_LAYOUT  List layout
*      -->OS_PRINT   Print settings
*----------------------------------------------------------------------*
FORM alv_init_list_layout   TABLES ot_exctab TYPE slis_t_extab
                          CHANGING os_layout TYPE slis_layout_alv
                                   os_print  TYPE slis_print_alv.

  REFRESH ot_exctab. "add menu items to be excluded

* List layout
  os_layout-zebra             = 'X'.     " stripped pattern
* Print settings
  os_print-no_print_selinfos  = 'X'.     " Skip selection statistics
  os_print-no_print_listinfos = 'X'.     " Skip list statistics

ENDFORM.                    "alv_init_layout

*---------------------------------------------------------------------*
*       FORM ALV_BUILD_SORTTAB                                        *
*---------------------------------------------------------------------*
*       Set up sorting information for ALV_INIT_REPORT_LAYOUT         *
*---------------------------------------------------------------------*
FORM alv_build_sorttab TABLES ot_sorttab TYPE slis_t_sortinfo_alv
                       USING  iv_tabname   TYPE slis_fieldname
                              iv_fieldname TYPE slis_fieldname
                              iv_up        TYPE c
                              iv_down      TYPE c
                              iv_subtot    TYPE c
                              iv_comp      TYPE c
                              iv_spos      TYPE n.

  ot_sorttab-spos      = iv_spos.
  ot_sorttab-fieldname = iv_fieldname.
  ot_sorttab-tabname   = iv_tabname.
  ot_sorttab-up        = iv_up.
  ot_sorttab-down      = iv_down.
  ot_sorttab-subtot    = iv_subtot.
  ot_sorttab-comp      = iv_comp.
  APPEND ot_sorttab.

ENDFORM.                            " ALV_BUILD_SORTTAB

*---------------------------------------------------------------------*
*       FORM ALV_INIT_LIST_COLUMNS                                    *
*---------------------------------------------------------------------*
*       Set up report column layout of the ALV_LIST_DISPLAY           *
*---------------------------------------------------------------------*
*  <-- OT_FIELDCAT list of fields                                     *
*  <-- OT_SORTTAB  sorting/grouping definition                        *
*  <-- OS_KEYINFO  itab key fields (required only for hier.lists)     *
*---------------------------------------------------------------------*
FORM alv_init_list_columns
                     TABLES ot_sorttab  TYPE slis_t_sortinfo_alv
                   CHANGING  ot_fieldcat TYPE slis_t_fieldcat_alv
                   os_keyinfo  TYPE slis_keyinfo_alv.

  REFRESH ot_fieldcat.
  REFRESH ot_sorttab.

  PERFORM alv_modify_fieldcat CHANGING ot_fieldcat[].

  PERFORM alv_build_sorttab TABLES ot_sorttab USING:
 'GT_ALV1_DATA'         'DOCNUM'            'X' ' ' ' ' ' '           1.

ENDFORM.                               " INIT_REPORT_LAYOUT

*&---------------------------------------------------------------------*
*&      Form  alv_modify_fieldcat
*&---------------------------------------------------------------------*
*       Corrections in the ALV fieldcatalog
*----------------------------------------------------------------------*
*      -->OT_FIELDCAT  ALV fieldcatalog
*----------------------------------------------------------------------*
FORM alv_modify_fieldcat  CHANGING ot_fieldcat TYPE slis_t_fieldcat_alv.

  DATA: ls_fieldcat TYPE slis_fieldcat_alv.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'DOCNUM'.
  ls_fieldcat-ref_tabname   = 'EDIDC'.
  APPEND ls_fieldcat TO ot_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'IDOCTP'.
  ls_fieldcat-ref_tabname   = 'EDIDC'.
  APPEND ls_fieldcat TO ot_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'MESTYP'.
  ls_fieldcat-ref_tabname   = 'EDIDC'.
  APPEND ls_fieldcat TO ot_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'CREDAT'.
  ls_fieldcat-ref_tabname   = 'EDIDC'.
  APPEND ls_fieldcat TO ot_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'STATUS'.
  ls_fieldcat-ref_tabname   = 'EDIDS'.
  APPEND ls_fieldcat TO ot_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'STATXT'.
  ls_fieldcat-ref_tabname   = 'EDIDS'.
  APPEND ls_fieldcat TO ot_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'STAMID'.
  ls_fieldcat-ref_tabname   = 'EDIDS'.
  APPEND ls_fieldcat TO ot_fieldcat.

  CLEAR ls_fieldcat.
  ls_fieldcat-fieldname = 'STAMNO'.
  ls_fieldcat-ref_tabname   = 'EDIDS'.
  APPEND ls_fieldcat TO ot_fieldcat.

ENDFORM.                    " alv_modify_fieldcat

*----------------------------------------------------------------------*
*       Form  ev_start_of_selection
*----------------------------------------------------------------------*
*       Steps after the start of selection event
*----------------------------------------------------------------------*
FORM ev_start_of_selection.

  PERFORM get_transaction_data.
  PERFORM process_transaction_data.

ENDFORM.                    "ev_start_of_selection

*----------------------------------------------------------------------*
*      Form  ev_end_of_selection
*----------------------------------------------------------------------*
*      Steps after the end of selection event
*----------------------------------------------------------------------*
FORM ev_end_of_selection.

  PERFORM output_data.

ENDFORM.                    "ev_end_of_selection

*---------------------------------------------------------------------*
*       FORM GET_TRANSACTION_DATA                                     *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
FORM get_transaction_data.
  IF p_latest IS NOT INITIAL.
    SELECT
      edidc~docnum edidc~idoctp edidc~mestyp edidc~credat edids~status
      edids~countr edids~statxt edids~stamid edids~stamno
      FROM edidc INNER JOIN edids ON edidc~docnum = edids~docnum
      INTO CORRESPONDING FIELDS OF TABLE gt_idocs
     WHERE idoctp IN s_idoctp
       AND mestyp IN s_mestyp
       AND direct IN s_direct
       AND edidc~credat IN s_credat
       AND upddat IN s_upddat.
  ELSE.
    SELECT
      edidc~docnum edidc~idoctp edidc~mestyp edidc~credat edids~status
      edids~countr edids~statxt edids~stamid edids~stamno
      FROM edidc INNER JOIN edids ON edidc~docnum = edids~docnum
      INTO CORRESPONDING FIELDS OF TABLE gt_idocs
     WHERE idoctp IN s_idoctp
       AND mestyp IN s_mestyp
       AND direct IN s_direct
       AND edidc~credat IN s_credat
       AND upddat IN s_upddat
       AND edids~status IN s_status
       AND stamid IN s_stamid
       AND stamno IN s_stamno.
  ENDIF.

  SORT gt_idocs BY docnum ASCENDING countr DESCENDING.
  DELETE ADJACENT DUPLICATES FROM gt_idocs COMPARING docnum.

  IF p_latest IS NOT INITIAL.
    DELETE gt_idocs WHERE status NOT IN s_status OR stamid NOT IN
    s_stamid OR stamno NOT IN s_stamno.
  ENDIF.
ENDFORM.                               " GET_TRANSACTION_DATA

*---------------------------------------------------------------------*
*       FORM PROCESS_TRANSACTION_DATA                                 *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
FORM process_transaction_data.

  LOOP AT gt_idocs .
    MOVE-CORRESPONDING gt_idocs TO gt_alv1_data .
    APPEND gt_alv1_data .
  ENDLOOP.

  FREE gt_idocs .

ENDFORM.                               " PROCESS_TRANSACTION_DATA

*---------------------------------------------------------------------*
*       FORM OUTPUT_DATA                                              *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
FORM output_data.

  PERFORM alv_list_display.

ENDFORM.                               " OUTPUT_DATA//