Thursday 14 September 2017

Simplify your life: Searching SO10 or other texts for string

Feel free to use the code for function modules/classes or other programs.

Selection-screen


SAP ABAP Tutorials and Materials, SAP ABAP Certifications, SAP ABAP String, SAP ABAP Learning


The defaults are set voor SO10 text selection. But you could change that of course for a more generic search on texts. VBBK for instance.

The searchstring is default 80 characters long, and you should be able to use lower case.

Program


*&---------------------------------------------------------------------*
*& Report  ZAFX_TEXT_SCANNER
*&
*&---------------------------------------------------------------------*
*& sample program of search within text
*&
*&---------------------------------------------------------------------*
REPORT zafx_text_scanner.

TYPES: BEGIN OF ty_stxl,
         tdname   TYPE stxl-tdname,
         tdobject TYPE stxl-tdobject,
         tdid     TYPE stxl-tdid,
         clustr   TYPE stxl-clustr,
         clustd   TYPE stxl-clustd,
       END OF ty_stxl.

TYPES: BEGIN OF ty_text,
         tdname TYPE stxl-tdname,
         text   TYPE string,
       END OF ty_text.

DATA:  t_stxl TYPE STANDARD TABLE OF ty_stxl.
FIELD-SYMBOLS: <stxl> TYPE ty_stxl.

* compressed text data without text name
TYPES: BEGIN OF ty_stxl_raw,
         clustr TYPE stxl-clustr,
         clustd TYPE stxl-clustd,
       END OF ty_stxl_raw.
DATA:  t_stxl_raw TYPE STANDARD TABLE OF ty_stxl_raw.
DATA:  w_stxl_raw TYPE ty_stxl_raw.

* decompressed text
DATA:  t_tline TYPE STANDARD TABLE OF tline.
FIELD-SYMBOLS: <tline> TYPE tline.
DATA: t_stxh TYPE STANDARD TABLE OF stxh,
      w_stxh TYPE stxh.
DATA ls_read_text TYPE ty_text.

* selection parameters
PARAMETERS p_obj TYPE stxh-tdobject OBLIGATORY DEFAULT 'TEXT'.
PARAMETERS p_id TYPE stxh-tdid DEFAULT 'ST'.
SELECTION-SCREEN SKIP.
PARAMETERS p_search(80) OBLIGATORY.

START-OF-SELECTION.
*--------------------------------------------------------------------*

  IF p_id IS NOT INITIAL.
    SELECT tdname tdobject tdid
       FROM stxh
         INTO CORRESPONDING FIELDS OF TABLE t_stxh
      WHERE tdobject = p_obj AND tdid = p_id ORDER BY tdfdate DESCENDING.
  ELSE.
    SELECT tdname tdobject tdid
     FROM stxh
       INTO CORRESPONDING FIELDS OF TABLE t_stxh
    WHERE tdobject = p_obj ORDER BY tdfdate DESCENDING.
  ENDIF.

END-OF-SELECTION.
*--------------------------------------------------------------------*

  IF t_stxh IS NOT INITIAL.
    SELECT tdname tdobject tdid clustr clustd
            INTO TABLE t_stxl
            FROM stxl
            PACKAGE SIZE 1000
            FOR ALL ENTRIES IN t_stxh "WITH APPLICATION DATA AND TDNAME
            WHERE relid    = 'TX'          "standard text
              AND tdobject = t_stxh-tdobject
              AND tdname   = t_stxh-tdname
              AND tdid     = t_stxh-tdid
              AND tdspras  = sy-langu.

      LOOP AT t_stxl ASSIGNING <stxl>.
*   decompress text
        CLEAR: t_stxl_raw[], t_tline[].
        w_stxl_raw-clustr = <stxl>-clustr.
        w_stxl_raw-clustd = <stxl>-clustd.
        APPEND w_stxl_raw TO t_stxl_raw.
        IF t_stxl_raw IS NOT INITIAL.
          TRY.
              IMPORT tline = t_tline FROM INTERNAL TABLE t_stxl_raw
              IGNORING CONVERSION ERRORS
              ACCEPTING TRUNCATION.
            CATCH cx_sy_conversion_codepage .
              CONTINUE.
*            catch cx_sy_import_mismatch_error .
*              continue.
            CATCH cx_sy_import_format_error.
              CONTINUE.
            CATCH cx_sy_expimp_db_sql_error .
              CONTINUE.
          ENDTRY.
        ELSE.
          CONTINUE.
        ENDIF.
*  access text lines for further processing

        CLEAR ls_read_text.
        ls_read_text-tdname = <stxl>-tdname.

        LOOP AT t_tline ASSIGNING <tline>.
          CONCATENATE ls_read_text-text <tline>-tdline INTO ls_read_text-text SEPARATED BY cl_abap_char_utilities=>cr_lf.
        ENDLOOP.
        IF sy-subrc = 0.
          SHIFT ls_read_text-text LEFT DELETING LEADING space.
          SHIFT ls_read_text-text LEFT DELETING LEADING cl_abap_char_utilities=>cr_lf.

          IF ls_read_text-text CS p_search.
            WRITE:/ <stxl>-tdname.
          ENDIF.
        ENDIF.
      ENDLOOP.
      FREE t_stxl.
    ENDSELECT.
  ENDIF.

The choice is made to first make a total text string in ls_read_text and search this. You could also compare in the loop of course and then exit the loop and continue. You might also want just the first hit (TDFDATE DESCENDING) and EXIT when you have found the string.

The search is done in the logon language sy-langu.

Result

SAP ABAP Tutorials and Materials, SAP ABAP Certifications, SAP ABAP String, SAP ABAP Learning

A report with only the names of the TEXTS is output here (<stxl>-tdname). You might want to do something else when the ls_read_text-text contains string p_search. You can of course also display the text.

No comments:

Post a Comment