Quantcast
Channel: Tweaks
Viewing all articles
Browse latest Browse all 66

Useful Trick to Search “Text” in Smartforms/Text modules

$
0
0

You may master the new programming models using Core Data Services. You may build beautiful Fiori and UI5 applications which are sleek, fast and mobile friendly. You may also write SQLScript in your S/4HANA Project. But once an ABAP Programmer, always an ABAP Programmer. Clients will always have some weird requests where you will still need your basic ABAP Programming skills, debugging, analyzing and solutioning acumen.

Background: Recently we received a requirement to replace some hard-coded text written in Text Elements of Smart forms. In the SAP system there were around 350 – 400 custom smart forms to find and replace.

For e.g: I was searching for text “Secret” in which and all smartforms/textmodule its written.

Approaches/Attempts:

Approach 1:

Like many, I thought there should be a table to hold this data and fortunately (from scn community) found table STXFTXT. Field TDLINE holds smart form content and I tried searching.

Results:

voila!! I found what I was looking for. But …..

The field  TDLINE is case sensitive. 🙁 It means, you should know exact word with exact upper case and lower case combination. Otherwise you can’t get correct results in this table (I also read somewhere this table is not at all reliable).

Check, now I am searching for all lower case “secret”. It gives different output.

See here its not returning “ZDEMO” form name in a results

Also Read – JavaScript in SAP Adobe Forms

So what next? Plan B 🙂

Approach 2:

Plan B was to download smart form as XML and search for text. This way you can find the texts.

Solution Implementation:

There is a function module called “FB_CONVERT_FORM_TO_XML” which will convert form to XML by taking Form name and Form type as input. This function module accepts Text Module also as input, which is Bonus.

Now as every ABAPer does, select all forms, loop on each, get XML, convert XML to String and search your Love/Pain stories strings 🙂 and display all found form names in ALV. Isn’t this the everyday story of all ABAP developers? 🙂

Advantage of this approach:

This search is Case Insensitive. It will search for all cases.

Also Check – Trick to Find the list of Smartforms and it’s Include TEXTs (SO10)

Code Snippet for your Reference

Attached program does it. Feel free to Plug and Play . Fancy word for copy paste. 🙂

The important functions used in this sample program are: FB_CONVERT_FORM_TO_XML, SSFH_XSTRINGUTF8_TO_STRING, FB_DISPLAY_FORM, DISPLAY_XML_DOCUMENT . Class used is CL_XML_DOCUMENT and method PARSE_STRING.

The entire code is also at the bottom of this article for your quick reference.

In Selection screen, we can give Form name and String for search.

Displays all Form name/Text Module wherever search string exists, with HotSpot clicking on this will open the Smart form/Text Module in XML format. Navigate to your text element.

Please note: Inactive forms will not be converted to XML, but Function module is smart enough to say its inactive by raising Exceptions.

Now the entire Smart Form properties are in your control. Do, whatever you want.

This is my first article at SAPYard. Your genuine feedback is welcome. Please help me improve and provide you better content.

In the next article I will show how to locate (find) exact node or element where search text lies in the Smartforms. Please stay tuned.

5370+ SAP Technical Practitioners from 6 Continents of the SAP World. Install Telegram app and click this link to join – Telegram SAP Technical Discuss Group. 

Forms (Smartforms and SapScript)

Step by Step Tutorials on S/4HANA

Code Snippet

<code>REPORT zsf_find_text.
*--------------------------------------------------------------------*
* Deferred statement CLASS is used to make the class class known,
* regardless of the location of the actual definition of the class
* in the program
CLASS lcl_event_handeler DEFINITION DEFERRED.
*--------------------------------------------------------------------*
DATA:lv_string TYPE tdsfname.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
SELECT-OPTIONS:s_form FOR lv_string.
PARAMETERS:p_forms  TYPE string NO-DISPLAY,
           find_dat TYPE string LOWER CASE OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b1.
*--------------------------------------------------------------------*
INITIALIZATION.
  s_form-low = 'Z*'.
  APPEND s_form.
*--------------------------------------------------------------------*
  DATA i_formname TYPE string.
  DATA e_xml      TYPE xstring.
*--------------------------------------------------------------------*
  TYPES:BEGIN OF ty_sf,
          formname TYPE tdsfname,
          formtype TYPE tdsftype,
          status   TYPE char8,
          e_xml    TYPE string,
        END OF ty_sf.
  DATA:lt_forms TYPE STANDARD TABLE OF ty_sf.
*--------------------------------------------------------------------*
* Class Definition
CLASS lcl_event_handeler DEFINITION.
  PUBLIC SECTION.
*   Hotspot Handling
    METHODS: handle_hotspot FOR EVENT link_click OF cl_salv_events_table
      IMPORTING
          row
          column  .
ENDCLASS.
*--------------------------------------------------------------------*
* Class Implementation
CLASS lcl_event_handeler IMPLEMENTATION.

* Handling Hotspot Click
  METHOD handle_hotspot.

    READ TABLE lt_forms INTO DATA(ls_forms) INDEX row.
    IF sy-subrc = 0.

      IF column = 'E_XML'.

        DATA: gcl_xml TYPE REF TO cl_xml_document.

        CREATE OBJECT gcl_xml.

*Parses XML String to DOM
        CALL METHOD gcl_xml->parse_string
          EXPORTING
            stream = ls_forms-e_xml.
*Display XML
        CALL METHOD gcl_xml->display.

      ELSE.

        CALL FUNCTION 'FB_DISPLAY_FORM'
          EXPORTING
            i_formname           = ls_forms-formname
            i_formtype           = ls_forms-formtype
            i_with_dialog        = abap_false
          EXCEPTIONS
            no_name              = 1
            no_form              = 2
            no_access_permission = 3
            illegal_language     = 4
            illegal_formtype     = 5
            OTHERS               = 6.
        IF sy-subrc &lt;> 0.
* Implement suitable error handling here
        ENDIF.

      ENDIF.
    ENDIF.
  ENDMETHOD.
ENDCLASS.
*--------------------------------------------------------------------*
START-OF-SELECTION.

  SELECT formname formtype FROM stxfadm INTO TABLE lt_forms
  WHERE formname IN s_form .

*--------------------------------------------------------------------*
  LOOP AT lt_forms ASSIGNING FIELD-SYMBOL(&lt;ls_forms>).

    DATA(lv_tabix) = sy-tabix.

    p_forms = &lt;ls_forms>-formname.
*   Convert to XML
    CALL FUNCTION 'FB_CONVERT_FORM_TO_XML'
      EXPORTING
        i_formname       = p_forms
      IMPORTING
        e_xml            = e_xml
      EXCEPTIONS
        no_active_source = 3.
    IF sy-subrc &lt;> 0.
      &lt;ls_forms>-status = 'Inactive'.
      CONTINUE.
    ENDIF.

    DATA ostr_output_data TYPE xstring.
    DATA codepage         TYPE cpcodepage.
    DATA cstr_output_data TYPE string.

*   Convert XML to String
    CALL FUNCTION 'SSFH_XSTRINGUTF8_TO_STRING'
      EXPORTING
        ostr_output_data = e_xml
      IMPORTING
        cstr_output_data = &lt;ls_forms>-e_xml
      EXCEPTIONS
        conversion_error = 1
        internal_error   = 2
        OTHERS           = 3.
    IF sy-subrc &lt;> 0.
      CONTINUE.
    ENDIF.

*   SEARCH Command
    SEARCH &lt;ls_forms>-e_xml FOR find_dat AND MARK.
    IF sy-subrc NE 0.
      DELETE lt_forms INDEX lv_tabix.
    ENDIF.
  ENDLOOP.
*--------------------------------------------------------------------*
  DATA: o_alv TYPE REF TO cl_salv_table.
  DATA: lx_msg TYPE REF TO cx_salv_msg.
  DATA: lr_columns TYPE REF TO cl_salv_columns,
        lr_column  TYPE REF TO cl_salv_column_table,
        lr_colums  TYPE REF TO cl_salv_columns_table,
        lr_display TYPE REF TO cl_salv_display_settings,
        title      TYPE lvc_title.
*--------------------------------------------------------------------*
  TRY.
      cl_salv_table=>factory(
        IMPORTING
          r_salv_table = o_alv
        CHANGING
          t_table      = lt_forms ).
    CATCH cx_salv_msg INTO lx_msg.
  ENDTRY.
*--------------------------------------------------------------------*
  lr_display = o_alv->get_display_settings( ).
  title = 'Search Results for Text:'(001) &amp;&amp; | " | &amp;&amp; find_dat &amp;&amp; | " |.
  lr_display->set_list_header( title  ).
  lr_display->set_striped_pattern( if_salv_c_bool_sap=>true ).
  lr_columns = o_alv->get_columns( ).
  lr_columns->set_optimize( abap_true ).
  DATA(gr_functions) = o_alv->get_functions( ).
  gr_functions->set_all( abap_true ).
  lr_colums  = o_alv->get_columns( ).
  lr_column ?= lr_colums->get_column( 'FORMNAME' ).
  lr_column->set_cell_type( if_salv_c_cell_type=>hotspot ).
  lr_column ?= lr_colums->get_column( 'STATUS' ).
  lr_column->set_long_text( 'Status' ).
  lr_column->set_short_text( 'Status' ).
  lr_column->set_medium_text( 'Status' ).
  lr_column ?= lr_colums->get_column( 'FORMTYPE' ).
  lr_column->set_visible( if_salv_c_bool_sap=>false ).
  lr_column ?= lr_colums->get_column( 'E_XML' ).
  lr_column->set_cell_type( if_salv_c_cell_type=>hotspot ).

*--------------------------------------------------------------------*
  DATA:co_report TYPE REF TO lcl_event_handeler.
  DATA: lo_events TYPE REF TO cl_salv_events_table.

  CREATE OBJECT co_report.
* All events
  lo_events = o_alv->get_event( ).
*
* Event handler
  SET HANDLER co_report->handle_hotspot FOR lo_events.
  o_alv->display( ).
*--------------------------------------------------------------------*</code>

Viewing all articles
Browse latest Browse all 66

Trending Articles