NextTables Knowledge Base

How to implement an Import BAdI for NextTables

Written by Sebastian | Jan 31, 2020 3:41:46 PM

In the “How to auto-correct imported values with NextTables” article, I have shown a practical implementation of fuzzy search algorithm for import. Using the Import BAdI you can ensure that only the correct values are imported in the system. In this article, I will guide you step by step through the technical implementation of this BAdI.

In this example, data for company codes is imported. The file to be imported contains the descriptions of company codes instead of the technical key. In the BAdI, we differentiate two cases. If a company code with the respective description exists, the technical key of this company code is used.

If no company code with the imported description can be found, we utilize the fuzzy search algorithm in order to look for a similar description. As you might guess, someone most likely means NextLytics AG if he imports NextLytics AG.

Necessary steps

In order to achieve this result, we utilize /NLY/IF_IMPORT~SET_IMPORT_EXIT method of the /NLY/BADI_IMPORT BAdI. But first, we have to setup the filter accordingly.

Filter

In the How to implement a BAdI for NextTables article you have learned how to create a BAdI implementation. To make sure that the BAdI is executed for a certain table only, you have to setup the filter criteria accordingly. Please enter respective table name and table type. Furthermore, you have to specify the field for which the BAdI should be executed. In our example we use the DSO ZDREXMPL. The field for company code is /BIC/ZDRCCODE.

Implementation

After the filter setup, we have to implement the logic in the implementing class. In order to check and adjust imported values, we use the method /NLY/IF_IMPORT~SET_IMPORT_EXIT. Please double click on the method to create an implementation.

You can use the following code for the implementation.

METHOD /nly/if_import~set_import_exit.

TYPES:
BEGIN OF ts_changes,
score TYPE decfloat34,
ccode TYPE /b787/oibukrs,
txtmd TYPE rstxtmd,
END OF ts_changes,
tt_changes TYPE TABLE OF ts_changes.

DATA: ls_validation TYPE /nly/ts_validation,
lv_sql TYPE string,
lo_t_table TYPE REF TO data,
lv_search_term TYPE string,
lv_ccode TYPE /b787/oibukrs.

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

IF i_step = 1.

* Search term
lv_search_term = i_value.


REPLACE ALL OCCURRENCES OF '%20' IN lv_search_term WITH ` `.
REPLACE ALL OCCURRENCES OF '%22' IN lv_search_term WITH `"`.
* check if exists
SELECT SINGLE /b787/s_bukrs FROM /b787/tbukrs
INTO lv_ccode
WHERE txtmd = lv_search_term.

IF lv_ccode IS NOT INITIAL.
e_value = lv_ccode.
* Notify the user
ls_validation = VALUE #( rowidx = i_tabix
column = is_fields_info-fname
hdr = 'Value was successfully assigned.'
msg = |Company code { e_value } corresponds to the description { i_value }.|
type = /nly/cl_table_rest_v3=>co_msg_type_warning
) .
APPEND ls_validation TO ct_validation.
ELSE. "lv_ccode IS INITIAL
* If nothing found, fuzzy search
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, "/B787/S_BUKRS", TXTMD |
&& |FROM "/B787/TBUKRS" | " Text Table
&& |WHERE CONTAINS(("/B787/S_BUKRS", "TXTMD" ), '{ lv_search_term }', FUZZY(0.7, 'similarCalculationMode=compare')|
&& | , weight (0.8, 1 )) |
&& |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_table_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.


READ TABLE <fs_t_table> ASSIGNING <fs_s_table> INDEX 1.
* Assign value
e_value = <fs_s_table>-ccode.
* Notify the user
ls_validation = VALUE #( rowidx = i_tabix
column = is_fields_info-fname
hdr = 'Value was adjusted'
msg = |Company code { i_value } does not exist. Company code { e_value } with description { <fs_s_table>-txtmd } was determined with a score { ROUND( val = <fs_s_table>-score dec = 2 mode = 2 ) } and therefore adopted as a value.|
type = /nly/cl_table_rest_v3=>co_msg_type_warning
) .
APPEND ls_validation TO ct_validation.
ENDIF. "lv_ccode IS NOT INITIAL

ENDIF. "IF I_STEP = 1.
ENDMETHOD.

 

In this code we check the text table of company code InfoObject for the description. If an entry with the respective description exist, we use the corresponding key as company code value. Afterwards, we notify the user about the changes.

If nothing was found, we prepare and execute a SQL query against the text table using fuzzy search algorithm. The result with the highest score is used as key for company code. After the company code was determined, we notify the user about the changes made by the BAdI.

Activate your changes

As you implement your custom logic, please make sure that all involved pieces are activated, not only the implementing class. You need to activate the following objects:

  • Implementing class together with methods
  • BAdI implementation
  • Enhancement implementation

Which License is needed for this feature Professional | Enterprise