SAP ABAP Advanced Cookbook
上QQ阅读APP看书,第一时间看更新

Dynamic program generation

The ABAP language also allows the creation of ABAP programs at runtime. An INSERT REPORT statement may be used for creating a new program specified by the a name in the statement. Dynamic program generation should only be used when there is no alternate option.

Also, you may generate a subroutine pool using a GENERATE subroutine pool statement. A subroutine pool is generated, and is of a temporary nature. The subroutines in the pool may be called from within the generation program.

On the other hand, the programs created via the INSERT REPORT statement are permanent and overwrite any prior existing programs with the same name. Within the generation program, the new program may be called via a SUBMIT statement.

Getting ready

We will use the concepts of dynamic program generation in order to show how a simple requirement may be solved. We will take as input two program names. The code of the first program will be read and then any comments or unwanted blank lines will be removed and a new program will be created by the name specified on the input screen.

How to do it...

  1. Two parameters, origprog and newprog, are declared for taking the input names of the existing and converted program respectively.
    How to do it...
  2. Internal tables, itab and itab2, are also declared for storing the source code of the original program and converted program respectively. These are based on string type. A work area of string type is also declared along with a temporary variable tempwa.
    How to do it...
  3. The read report is then used with the original program name and the internal table, itab. We will check the value of sy-subrc in order to proceed further.
    How to do it...
  4. The LOOP statement is then added for reading itab into wa. The APPEND statement is used in conjunction with the IF and SPLIT statements in order to process all non-blank lines without having asterisk at the beginning. The SPLIT statement splits the source code line under consideration at the occurrence of a quotation mark ("). The work area, wa, contents are then appended to the internal table, itab2.
    How to do it...
  5. The SELECT SINGLE statement is then written in order to check newprog in trdir table.
    How to do it...
  6. If the return code value is not equal to zero (0), the insert report statement is added and on success a message Program Converted by Name message is displayed. If the insert fails, a message Program Conversion failed message is displayed.

    If the return code value after the SELECT statement is equal to 0, the message saying that a program having the desired destination program name already exists, is displayed.

    How to do it...

How it works...

The program statements collectively allow the conversion of a program. The new converted program is free from all types of comments and blank lines.

Let us see how the program works.

The selection screen displays two fields for taking input for Original Program that is to be converted and New Program that is to be generated.

How it works...

For example, we can enter the Original Program value as YTEST_TO_BE_GENERATED whereas the new converted program is YTEST_TO_BE_GENERATED3.

How it works...

The code of the YTEST_TO_BE_EXECUTED program is read into the itab internal table using the READ REPORT statement.

How it works...

The loop is then run, and any blank lines are removed. Also the lines with asterisk (*) are removed, and anything written after the quotation mark (") is ignored. The new lines are appended to the itab2 internal table.

How it works...

The last step is to check the trdir table's program directory. The SELECT statement checks the table to see if a program already exists with the new program name entered by the user. If a program already exists, a message is displayed saying "Program not converted since destination program already exists". This is necessary to avoid overwriting of the existing program. If the name is unique, the INSERT REPORT statement is called and the new program is generated.

How it works...

There's more...

While using the insert statement, the CX_SY_WRITE_SRC_LINE_TOO_LONG exception may occur if the program line to be inserted is too long. We may catch the exception using the try and catch statements as shown in the following screenshot:

There's more...