Skip to content
  • There are no suggestions because the search field is empty.

The Import BAdI in NextTables for SAP BW

The Import BAdI end to end: what it is narrowly for, why validation belongs elsewhere, all parameters and CT_VALIDATION, and a full implementation turning descriptions into keys.

📝 Availability: NextTables for SAP BW, Enterprise edition. This article documents a BAdI; BAdI support is an Enterprise feature.

You will learn

What the Import BAdI is for, why it is the exception rather than the normal way to check imported data, every parameter of SET_IMPORT_EXIT, and a complete worked implementation that turns a company code description into its technical key, with a fuzzy match when the description is not exact.

This article is for ABAP developers. It merges the Import BAdI parameter reference and its implementation tutorial into one place, because neither is much use without the other.

⚠️ Read this before you start. Implement the Import BAdI only when a value has to be changed before it can pass the data type check, for example a Yes for a field of type NUMC1 or a description where the field expects its key. That is the one job no other hook can do. Checks, messages and derived values belong in the Update BAdI: it sees the whole row, receives the rows of a request together and covers every write. The Import BAdI is called twice for every value of the field in its filter, for imports from a file or the clipboard and for every row a user adds or changes in the grid, so on a large import each line in it costs noticeably more time than the same line in the Update BAdI. How import validation works in NextTables for SAP BW shows which hook a requirement needs, and The Update BAdI (SET_UPDATE_EXIT) in NextTables for SAP BW explains the hook most requirements need.

Prerequisites

What it is good for

The classic case is a value that is right but written in a form the field cannot store. A spreadsheet column holds Yes, YES and yes; the target field is NUMC1 and only accepts 1. Standard processing rejects all three, because by the time it looks at them they are already expected to be type compatible. The Import BAdI sees them one step earlier and can map them.

The second case, worked through below, is a file that carries descriptions where the table expects keys. A file listing company code descriptions can be turned into technical keys during the import, including a fuzzy match when the description in the file is not written exactly as it is in the system.

The two steps

The exit is called twice for every value of the field in its filter, distinguished by I_STEP. That happens for an import from a file or the clipboard and for every row the grid validates, and the parameters do not say which of the two it is:

  • I_STEP = 1, before the type check. I_VALUE holds the raw value from the file, the clipboard or the grid. Return the mapped value in E_VALUE. This is what the Import BAdI is for.
  • I_STEP = 2, after auto-correction and the type check have run and produced their messages. Here you can read and adjust those messages. Messages about your own business rules come from the Update BAdI.

After step 2 the Update BAdI is called, where validation and derivation proper happen.

The filter

Like every NextTables BAdI, the Import BAdI runs only when its filter matches. Set the table name, the table type and the field name in the Filter Values section.

For an alias table, set the table type to ALIASTABLE and the table name to the alias name. The exit then receives the alias in I_TTYPE and I_TABNAME, while I_TECHNAME names the database table of the physical object. A filter that names the physical object applies only to data entered into that object directly, so an implementation that should run for both needs a filter combination for each.

The Filter Values section of an Import BAdI implementation, with table name, table type and field name criteria

⚠️ Always fill FIELDNAME. You can leave it empty and branch on the field inside the implementation, but do not: the exit then runs for every field of every imported row. On a real import that is a measurable performance cost for logic that applies to one column.

Parameters

ParameterTypeDescriptionPossible values
I_TECHNAMECHAR 30Technical name of the tableFor example /BIC/AZOMACOST2
I_TABNAMECHAR 30Table nameFor example ZOMACOST
I_TTYPECHAR 10Table typeDDIC Data Dictionary table
DSO DSO, advanced or classic
CUSTOM custom, usable for views
IOBJ_ATT InfoObject attributes
IOBJ_TXT InfoObject texts
ALIASTABLE alias table
IS_FIELDS_INFOStructureField information for the column being importedThe field properties described in the Meta method reference
I_STEPCHAR 1Step1 before auto-correction and the data type check
2 after auto-correction and the data type check
I_VALUESTRINGField value before conversion and validation
I_TABIXINT4Row number in the imported data
E_SKIPBOOLEANSkip further processingX skip further processing
E_VALUEANYField value after your conversion. This is what gets stored.
CT_VALIDATIONTableValidation messages shown in the import reportSee below
CT_MESSAGESTableMessages shown to the user, as in the table maintenance BAdI. They appear for an import from a file or the clipboard and when the grid validates a row. Use it for messages about the mapping; messages about business rules belong in the Update BAdI.See Error handling in the table maintenance BAdI for SAP BW

CT_VALIDATION

Each line is one message against one cell, which is why it carries a row and a column rather than just text:

FieldTypeDescriptionPossible values
ROWIDXINT4Row numberNormally I_TABIX
COLUMNCHAR 30Field nameNormally IS_FIELDS_INFO-FNAME
TYPECHAR 30Message typeWARNING, ERROR, INFO, SUCCESS
HDRSTRINGMessage header
MSGSTRINGMessage

Report every change you make. A value the BAdI silently rewrote is a value nobody can account for later.

The NextTables import validation report listing errors and warnings per row and column

Step-by-Step Instructions

1. Create the implementation and set the filter

Create a BAdI implementation for the definition /NLY/BADI_IMPORT inside your project's enhancement implementation. Set the table name, the table type and the field name in the filter. The worked example uses the DSO ZDREXMPL and the company code field /BIC/ZDRCCODE.

2. Implement SET_IMPORT_EXIT

Double-click the method /NLY/IF_IMPORT~SET_IMPORT_EXIT to create the implementation.

The implementing class of the Import BAdI with the SET_IMPORT_EXIT method

3. A simple mapping

The smallest useful implementation replaces one value and says that it did:

DATA ls_validation TYPE /nly/ts_validation.

IF i_step = 1.
  IF i_value = 'Test Company CA'.

    e_value = '9999'.

    ls_validation = VALUE #(
      rowidx = i_tabix
      column = is_fields_info-fname
      hdr    = 'Value was mapped'
      msg    = 'The cost area "Test Company CA" was mapped to "9999" by a custom BAdI exit.'
      type   = /nly/cl_table_rest_v3=>co_msg_type_warning ).

    APPEND ls_validation TO ct_validation.

  ENDIF.
ENDIF.

The target field is CHAR4, so Test Company CA could never have been stored. Mapping it in step 1 is exactly the job this BAdI exists for.

4. A worked example: descriptions to keys, with a fuzzy fallback

The file carries company code descriptions rather than keys. The exit first looks the description up in the text table. If it finds an exact match, it uses that key. If it does not, it runs a fuzzy search against the text table and takes the best-scoring result, so a description that is spelled slightly differently in the file still lands on the right company code.

Both branches append a message, and the second one reports the score, so a reviewer can see how confident the match was.

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 whether the description 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.

      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.

*     Nothing found, so fall back to a 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" |
            && |WHERE CONTAINS(("/B787/S_BUKRS", "TXTMD"), '{ cl_abap_dyn_prg=>escape_quotes( lv_search_term ) }', |
            && |FUZZY(0.7, 'similarCalculationMode=compare'), weight(0.8, 1)) |
            && |ORDER BY score() DESC|.

      TRY.
          DATA(lo_result) = cl_sql_connection=>get_connection(
                              )->create_statement(
                              )->execute_query( lv_sql ).

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

        CATCH cx_sql_exception INTO DATA(err).
          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.

      e_value = <fs_s_table>-ccode.

      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 used instead.|
        type   = /nly/cl_table_rest_v3=>co_msg_type_warning ).

      APPEND ls_validation TO ct_validation.

    ENDIF.

  ENDIF.

ENDMETHOD.

📝 Adapt the names. /B787/OIBUKRS and /B787/TBUKRS are the InfoObject and text table of one demo system. Replace them with the InfoObject your own company code characteristic uses. The description goes into the SQL text inside single quotes, so the example doubles its single quotes with cl_abap_dyn_prg=>escape_quotes( ), and a description such as O'Neil Ltd is searched as written. The fuzzy score of 0.7 is a starting point, not a recommendation: raise it if the fallback matches too eagerly.

5. Activate everything

Activate the implementing class with its methods, the BAdI implementation and the enhancement implementation. Missing one of the three is the usual reason the exit appears to do nothing.

6. Import a test file and read the report

Import a small file that exercises both branches. Every message you appended appears in the validation report against its row and column, so the report is how you confirm the exit ran at all.

Troubleshooting / FAQs

1. My exit also runs when a user edits a row in the grid.

That is expected. The grid validates every row a user adds or changes, and that validation calls the Import BAdI with both steps, as an import does. Keep the exit to mapping values that would fail the type check, and put checks and their messages into the Update BAdI's Validate step.

2. The import is slow since I added the BAdI.

Check whether FIELDNAME is set in the filter. With it empty the exit is called for every field of every row rather than for the one column it is about. Then check whether the logic belongs here at all: everything except the mapping before the type check runs faster in the Update BAdI, which receives the rows together.

3. My mapped value is rejected anyway.

Make sure you set E_VALUE and not just read I_VALUE. E_VALUE is what continues into the type check; I_VALUE is the untouched input.

4. Should validation go here or in the Update BAdI?

In the Update BAdI's Validate step, in almost every case. It sees the whole row, so a check can compare fields, it receives the rows of a request together, and it covers every write. Use the Import BAdI only when the value has to be changed before it can be type checked at all, which is the one thing the Update BAdI cannot do.

5. The fuzzy search returns nothing and the row fails.

The example reads index 1 without checking that the result table has a row. In production, test sy-subrc after the READ TABLE and append an error message when there is no match, instead of leaving E_VALUE unset.

Still need help?

Customers open a support ticket and follow every reply in the Support Portal. Subscribe to product updates and the release notes reach you by email.

Evaluating NextTables? Talk to the team that builds it.