📜 ⬆️ ⬇️

Online ABAP code generator

Often, when ABAP coding, a typical task 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 .

The comments pointed to

" not readable code " - your own micro language, which 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 still offered 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 by 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. In technical terms of the developer: in the internal table, according to the value of the BUKRS field, enter the value of the BUTXT field in the database table T001 .

Simple setting 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 this code is already a lot. At the same time, there is nothing difficult in it - a practically simple “monkey” job of typing.

And here comes the automatic generator 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
communication fields: BUKRS
initialized fields: BUTXT

and we get the following generated code
*--      T001 FORM values_from_tab_T001 USING "-- ()  uv_in_BUKRS TYPE any "--   uv_out_BUTXT TYPE any CHANGING "--    ct_table TYPE ANY TABLE . "--    ? CHECK NOT ( ct_table[] IS INITIAL ). "--     TYPES: 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. ENDLOOP. "--     SORT lt_item BY BUKRS. DELETE ADJACENT DUPLICATES FROM lt_item COMPARING BUKRS. "--     SELECT BUKRS BUTXT FROM T001 INTO TABLE lt_item FOR ALL 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. ELSE. IF <lv_BUTXT> IS ASSIGNED. CLEAR <lv_BUTXT>. ENDIF. ENDIF. ENDLOOP. ENDFORM. 

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

 PERFORM values_from_tab_T001 USING 'BUKRS' 'BUTXT' CHANGING lt_TABLE. 

View code on ABAP generator site
As parameters, the names of the fields in the internal table and the internal table itself 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 twice

 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 choosing 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] - we 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. At the same time 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.

Source: https://habr.com/ru/post/426967/


All Articles