Home ERP systems Online ABAP code generator

Online ABAP code generator

by admin

Often in ABAP coding there is a typical task – by values of some fields of internal table to initialize other fields (select from database table, via FM call, subroutines).And the code in this case is very simple in terms of algorithm, but it’s quite a lot.Always wanted to reduce time killed by such routine operations. And even wrote a method based on dynamic program creation for Selecting reference values by their keys from database tables
The comments pointed to
" code illegibility "- its own micro-language to learn.
" dynamic calls " – dynamic calls are not welcomed, also because in this case the usage log does not find a place to use the corresponding tables/fields.
As an alternative, I suggested the automatic code generation option back then, but only now have I brought it up to ready-made tool Whoever is interested, please go under.
The idea is simple: there is a set of parameters, by which ABAP code is generated according to a given template.
Let’s take a simple example : in the internal table, put the BE name field by the value of the BE field. Technically speaking, the developer : in the internal table by the value of the field BUKRS set the value of the field BUTXT by DB table T001
Simple staging in just one sentence. But to implement it you either have to do LOOP over internal table and SELECT SINGLE in loop (which is fast, but not very welcome from speed point of view, especially if there are a lot of rows in internal table), or select unique values BUKRS, query to database table T001and then put corresponding BUTXTfield values via READ TABLE. In this case code will be more optimal, but it’s not so fast to write it, because there is already a lot of it. That said, there’s nothing complicated about it – practically a simple "monkey" typing job.
And this is where the automatic generator comes to the rescue, because this code has a template form, which means that its writing can be automated.
Specify the following parameters :
DB table : T001
communication fields : BUKRS
initializable fields : BUTXT
and get the following generated code

*-- Select data from database table T001FORM values_from_tab_T001USING"--Input (key) fieldsuv_in_BUKRS TYPE any"-- Output fieldsuv_out_BUTXT TYPE anyCHANGING"-- Table for processingct_table TYPE ANY TABLE"-- Maybe the table is empty.CHECK NOT ( ct_table[] IS INITIAL )."-- Define structure and dataTYPES: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."-- Select list of keysLOOP 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."-- Leave unique key valuesSORT lt_item BY BUKRS.DELETE ADJACENT DUPLICATES FROM lt_item.SORT BUKRS."-- Pick values from tableSELECTBUKRSBUTXTFROM T001INTO TABLE lt_itemFOR ALL ENTRIES IN lt_itemWHERE BUKRS = lt_item-BUKRS"-- Sort for quick searchSORT lt_item BY BUKRS."-- Initialize fields in the tableLOOP AT ct_table ASSIGNING <ls_table> ."-- Initialize key fieldsASSIGN COMPONENT uv_in_BUKRS OF STRUCTURE <ls_table> TO <lv_BUKRS> .ls_item-BUKRS = <lv_BUKRS> ."-- Bind output fieldsASSIGN COMPONENT uv_out_BUTXT OF STRUCTURE <ls_table> TO <lv_BUTXT> ."-- Select data by key fieldsREAD TABLE lt_item ASSIGNING <ls_item>WITH KEYBUKRS = ls_item-BUKRSBINARY 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 the code on the website ABAP generator
The names of the fields in the internal table and the internal table itself are passed to the subroutine as parameters. After the call, the BUTXT field will have the desired value.
The names of the fields in the internal table are made as parameters so you can call the same subroutine for different fields. I.e. if you have two BE fields in your internal table (e.g. BUKRS1 and BUKRS2), you only 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 "suboptimality" appears, because there will be two SELECTs from database table T001, although ideally you can do everything through one. But this is the cost of automation. This is unlikely to slow down program execution much, but you can always "tweak" the subroutine to suit your particular needs if necessary.
You can initialize multiple fields at once and specify constants in the conditions (for example sy-langu, which is often used when selecting texts). The standard example is EI names According to Table T006A.
When generating code, it also creates a link where you can open the created example in your browser.
There are currently several templates in the genarator :

  1. Define structure [+ tabular type] – define structure + table type + table of that type + table workspace + table field-symbol. You don’t always need all this, but it’s easier to delete the generated than to type it in manually
  2. Remove duplicate records from internal table by field – is quite a simple generator and in general you can write this code manually, but if you have a lot of fields, in my opinion, it’s better to use this template
  3. Select reference values from the database table – pattern described above
  4. Select reference values – The pattern allows you to set the values of the fields by calling FMs, subroutines. The 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 there is no point in describing each pattern in detail, because it is easier to see how it works on site Each template has default values for its parameters and if you’re an abap developer, you’ll figure it out quickly by code.

You may also like