NextTables Knowledge Base

Search BAdI - Do Search Method Explained

Written by Sebastian Uhlig | Oct 7, 2019 9:46:00 AM

In this exit you can implement your own search including formatting the results for display to the end user.

Our Search BAdI blog series in overview

  1. How to Implement a Search BAdI - general overview
  2. Search BAdI - Meta Method Explained
  3. Search BAdI - Do Search Method Explained
 

Filter

The filter contains the search type and the search name. For example, if the search is executed on an InfoObject, the search name is the name of the InfoObject.

The table or DSO is not part of the filter. It means the BAdI is executed across all DSOs or tables which contain the same InfoObject. This way, you don’t have to implement a BAdI multiple times for different DSOs.

If you want to implement different searches for DSOs, which contain the same InfoObject, you can implement a new referenced InfoObject (so that the master data is reused). You can also use the Table Maintenance Meta BAdI to overwrite the settings done in the Search Meta BAdI.

Parameters

This exit is implemented in BADI /NLY/BADI_SEARCH / via interface /NLY/IF_BADI_SEARCH, method DO_SEARCH_EXIT and contains the following parameters.

Property

Type

Description

Possible Values

I_STYPE

CHAR 6

Search Type (originally used)

DDIC    Search based on table and field in Data Dictionary

IOBJ    InfoObject

CUSTOM    Custom

I_SEARCHNAME

CHAR 60

Search Name (originally used)

For example, name of the InfoObject, if Search Type IOBJ is used

I_SEARCH_TERM

STRING

Search Term

 

E_IS_IMPLEMENTED

BOOLEAN

Set as customer exit implemented, skips standard search logic

X true

E_T_SEARCH_RESULT

Table

Search Result

see next table

 

E_T_SEARCH_RESULT Explained

The table E_T_SEARCH_RESULT contains the search results and has the following parameters:

Property

Type

Description

Possible Values

CAT

CHAR 60

Category

Not used, obsolete

CAT_URL

STRING

Category URL

Not used, obsolete

CAT_IMG

STRING

Category Image

Not used, obsolete

TITLE_DESC

CHAR 250

Title Description

 

TITLE_URL

STRING

Title URL

Not used, obsolete

TITLE_KEY

CHAR 60

Title Key

 

SUB_TITLE

CHAR 250

Subtitle

 

CONTENT

STRING

Context

 

ACTION_TITLE

CHAR 40

Button description

Not used, obsolete

ACTION_URL

STRING

Action URL

Not used, obsolete

ACTION_IMG

STRING

Action Image

Not used, obsolete

ACTION_ICON

STRING

Action Icon

Not used, obsolete

ACTION_FUNC

STRING

Action Function

Not used, obsolete

SCORE

DEC 5,2

Search Score

 

 

Code Snippet

In the example below, we utilize a custom implementation for fuzzy search of employees. This tutorial shows the implementation step by step.

METHOD /nly/if_search~do_search_exit.

TYPES:
BEGIN OF ts_changes,
score TYPE decfloat34,
pernr TYPE /b787/oipernr,
nachn TYPE c LENGTH 40,
vorna TYPE c LENGTH 40,
pstlz TYPE c LENGTH 10,
stras TYPE c LENGTH 60,
ort01 TYPE c LENGTH 40,
emplstatus_txtmd TYPE c LENGTH 40,
bukrs TYPE c LENGTH 10,
bukrs_txtmd TYPE c LENGTH 40,
kostl TYPE c LENGTH 10,
kostl_txtmd TYPE c LENGTH 40,
orgeh TYPE c LENGTH 10,
orgid_txtmd TYPE c LENGTH 40,
plans TYPE c LENGTH 10,
plans_txtmd TYPE c LENGTH 40,
END OF ts_changes,
tt_changes TYPE TABLE OF ts_changes.

DATA: ls_search_result TYPE /nly/ts_search_result,
lv_sql TYPE string,
lo_t_table TYPE REF TO data,
l_search_term TYPE string.

* Search term
DATA(lv_search_term) = i_search_term.

FIELD-SYMBOLS:
<fs_s_table> TYPE ts_changes,
<fs_t_table> TYPE tt_changes.

REPLACE ALL OCCURRENCES OF '%20' IN lv_search_term WITH ` `.
REPLACE ALL OCCURRENCES OF '%22' IN lv_search_term WITH `"`.

IF lv_search_term IS INITIAL.
l_search_term = '*'.
ELSE.
l_search_term = |{ lv_search_term }|.
ENDIF.

CREATE DATA lo_t_table TYPE tt_changes.
ASSIGN lo_t_table->* TO <fs_t_table>.

lv_sql = |SELECT TOP 300 DISTINCT SCORE() AS SCORE, pernr, nachn, vorna, pstlz, stras, ort01, emplstatus_txtmd, bukrs, bukrs_txtmd, kostl, kostl_txtmd, orgeh, orgid_txtmd, plans, plans_txtmd |
&& |FROM "ZCDSPERSSEARCH" | " Personal View
&& |WHERE CONTAINS(("NACHN", "VORNA", "PERNR", "STRAS", "ORT01", "BUKRS", "KOSTL", "ORGEH", "PLANS", "PLANS_TXTMD", "ORGID_TXTMD", "BUKRS_TXTMD", "KOSTL_TXTMD" ), '{ l_search_term }', FUZZY(0.7, 'similarCalculationMode=compare')|
&& | , weight (1, 0.8, 0.8, 0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.3, 0.3, 0.3, 0.3 )) |
&& |ORDER BY score() desc|.

TRY.
" Prepare SQL connection and statement
DATA(lo_result) =
cl_sql_connection=>get_connection(
)->create_statement(
)->execute_query( lv_sql ).

lo_result->set_param_table( REF #( <fs_t_table> ) ).

" Get result
lo_result->next_package( ).
lo_result->close( ).
CATCH cx_sql_exception INTO DATA(err).

" Error handling
DATA l_error(200) TYPE c.
l_error = |{ err->get_text( ) }|.

RAISE EXCEPTION TYPE /nly/cx_search_rest
EXPORTING
textid = /nly/cx_table_rest=>custom_message
msgv1 = l_error(50)
msgv2 = l_error+50(50)
msgv3 = l_error+100(50)
msgv4 = l_error+150(50).
ENDTRY.

LOOP AT <fs_t_table> ASSIGNING <fs_s_table>.
CLEAR ls_search_result.
ls_search_result-title_key = |{ <fs_s_table>-pernr }|.
ls_search_result-title_desc = |{ <fs_s_table>-nachn } { <fs_s_table>-vorna } ({ <fs_s_table>-pernr ALPHA = out width = 1 }) |.
ls_search_result-sub_title = | { <fs_s_table>-plans_txtmd } in { <fs_s_table>-orgid_txtmd } |.
ls_search_result-content = |Adresse: { <fs_s_table>-stras } | && | { <fs_s_table>-ort01 } |
&& | <br>BuKrs: { <fs_s_table>-bukrs } { <fs_s_table>-bukrs_txtmd }|
&& | <br>Kost: { <fs_s_table>-kostl } { <fs_s_table>-kostl_txtmd }|
&& | <br>Score: { <fs_s_table>-score }|.
ls_search_result-score = <fs_s_table>-score * 100.
APPEND ls_search_result TO e_t_search_result.
ENDLOOP.

* set as customer exit implemented
e_is_implemented = abap_true.
ENDMETHOD.

Which License is needed for this feature Professional | Enterprise