Online ABAP code generator

    Often during ABAP coding, a typical problem arises - to initialize other fields according to the values ​​of some fields in the internal table (select from the database table, via the FM call, subroutines). And the code in this case is very simple in terms of the algorithm, but it is quite a lot. I always wanted to reduce the time I was killed for such routine operations. And he even wrote a method based on the dynamic creation of programs for retrieving reference values ​​by their keys from database tables .

    In the comments pointed to the

    " not readable code " - a micro language that you need to learn.
    " dynamic calls " - dynamic calls are not welcomed, including later that in this case the use log does not find a place to use the corresponding tables / fields.

    As an alternative, I was still offering an option with automatic code generation, but just now I brought this business to the finished tool . Who cares, I ask under the cat.

    The idea is simple: there is a set of parameters according to which an ABAP code is generated according to a given pattern.

    Let us take a simple example: in the internal table put down the name of the company code by the value of the field company. Technically speaking of: in an internal table of the value field BUKRS affix field value BUTXT of database table T001 .

    Simple statement in just one sentence. But to do this, you need to either do LOOP on the internal table and SELECT SINGLE in a loop (which is fast, but from the point of view of fast performance is not very welcome, especially if there are a lot of rows in the internal table), or choose unique values ​​BUKRS, query to the database table T001, and then we put down the corresponding values ​​of the BUTXT field via READ TABLE. In this case, the code will be more optimal, but its writing is not so fast, because there is already a lot of this code. At the same time there is nothing difficult in it - practically simple “monkey” work on typing text.

    And here the automatic generator comes to the rescue, because such a code has a sample form, which means you can automate its writing.

    Specify the following parameters:

    DB table:T001
    field for communication: BUKRS
    initialized field: BUTXT

    and we get the following generated code
    *-- Выбрать данные из таблицы БД T001
    FORM values_from_tab_T001
             "-- Входные(ключевые) поля
             uv_in_BUKRS TYPE any
             "-- Выходные поля
             uv_out_BUTXT TYPEany
             "-- Таблица для обработки
             ct_table TYPE ANY TABLE
        "-- А может таблица пустая?CHECKNOT ( ct_table[] IS INITIAL ).
        "-- Определить структуру и данные
           BEGIN OF lty_item,
              BUKRS TYPE T001-BUKRS,
              BUTXT TYPE T001-BUTXT,
           END OF lty_item.
        DATA ls_item TYPE lty_item.
        DATA lt_item LIKE TABLE OF ls_item.
        FIELD-SYMBOLS <ls_item> LIKE LINE OF lt_item.
        FIELD-SYMBOLS <ls_table> TYPE any.
        FIELD-SYMBOLS <lv_BUKRS> TYPE any.
        FIELD-SYMBOLS <lv_BUTXT> TYPE any.  
        "-- Выбрать список ключейLOOP AT ct_table ASSIGNING <ls_table>.
          CLEAR ls_item.
          ASSIGN COMPONENT uv_in_BUKRS OF STRUCTURE <ls_table> TO <lv_BUKRS>.
          ls_item-BUKRS = <lv_BUKRS>.
          APPEND ls_item TO lt_item.
        "-- Оставить уникальные значения ключей
        SORT lt_item BY BUKRS.
              COMPARING BUKRS.
        "-- Выбор значений из таблицыSELECT
          FROM T001
          INTOTABLE lt_item
           FORALL ENTRIES IN lt_item
         WHERE BUKRS = lt_item-BUKRS
        "-- Сортировать для быстрого поиска
        SORT lt_item BY BUKRS.
        "-- Инициализируем поля в таблицеLOOP AT ct_table ASSIGNING <ls_table>.
          "-- Инициализировать ключевые поля
          ASSIGN COMPONENT uv_in_BUKRS OF STRUCTURE <ls_table> TO <lv_BUKRS>.
          ls_item-BUKRS = <lv_BUKRS>.
          "-- Привязать выходные поля
          ASSIGN COMPONENT uv_out_BUTXT OF STRUCTURE <ls_table> TO <lv_BUTXT>.
          "-- По ключевым полям выбрать данные
          READ TABLE lt_item ASSIGNING <ls_item>
             WITH KEY
               BUKRS = ls_item-BUKRS
             BINARY SEARCH.  
          IF sy-subrc = 0.
            IF <lv_BUTXT> IS ASSIGNED. <lv_BUTXT> = <ls_item>-BUTXT. ENDIF.

    Now it is enough in your program to call the generated subroutine.


    View the code on the ABAP generator site.
    As parameters, the names of the fields in the internal table and the internal table are transferred to the subroutine. After the call, the BUTXT field will have the desired value.

    The names of the fields in the internal table are made in the form of parameters so that the same subroutine can be called for different fields. Those. if you have two BU fields in the internal table (for example, BUKRS1 and BUKRS2), then you just need to generate one subroutine and call it two times

    PERFORM values_from_tab_T001 USING'BUKRS1''BUTXT1' CHANGING lt_TABLE.
    PERFORM values_from_tab_T001 USING'BUKRS2''BUTXT1' CHANGING lt_TABLE.

    In this case, “non-optimality” appears, since there will be two SELECTs from the database table T001, although in the ideal case you can do everything in one. But this is the cost of automation. It is unlikely that this will slow down the execution of the program, but if necessary, you can always “finish” the subprogram for your particular needs.

    You can initialize several fields at once, and in the conditions specify constants (for example, sy-langu, which is often used when selecting texts). In the standard example, the names of the UE are selected according to the table T006A.

    When generating the code, a link is also created, according to which the created example can be opened in the browser.

    At the moment there are several templates in the generator:

    1. Define structure [+ table type] - define structure + table type + table of this type + table working area + table field-symbol. It is not always necessary, but it is easier to delete the generated one than to type it manually.
    2. Delete duplicate entries from the internal table by fields - a very simple generator and in general, such code can be written manually, but if there are many fields, then, in my opinion, it is better to use this template
    3. Select reference values ​​from the database table - template described above
    4. Select reference values - the template allows you to set the values ​​of the fields through a call to the FM, and subroutines. In this case, calls are cached.
    5. Select reference values ​​from the domain - this template has no parameters and was added just to be able to copy this code into your program.

    I think it makes no sense to describe in detail the work of each template, as it is easier to see the result of its work on the site . Each template has default values ​​for its parameters and if you are an abap developer, then you will quickly figure out the code.

    Also popular now: