Friday, February 8, 2008

Dynamic Code Execution

The following code is the basis for a report to dynamically execute
ABAP-Code at runtime.

*&---------------------------------------------------------------------*
*& Report ZDynamic_Code_Execution
*&---------------------------------------------------------------------*
report ZDynamic_Code_Execution.

types: line(132) type c.
data: it_tline type standard table of tline with header line,
header type thead,
control type itced,
report(30) type c,
itab type standard table of line initial size 500 with header
line,
l_uname like sy-uname.

concatenate 'ZZAB' sy-uname into l_uname.
concatenate 'report' l_uname '.' into itab separated by space.
append itab to itab.

* check if report already exits
select single * from stxh into corresponding fields of header
where tdobject = 'TEXT'
and tdname = l_uname
and tdid = 'ST'
and tdspras = 'D'.

check sy-subrc = 0.

* create table with 132 lines
do 132 times.
append it_tline to it_tline.
enddo.

header-tdobject = 'TEXT'.
header-tdname = l_uname.
header-tdid = 'ST'.
header-tdspras = 'D'.

call function 'SAVE_TEXT'
exporting
header = header
insert = 'X'
savemode_direct = 'X'
tables
lines = it_tline.

commit work.

* read text and create table
call function 'READ_TEXT'
exporting
id = 'ST'
language = 'D'
name = header-tdname
object = 'TEXT'
tables
lines = it_tline.

header-tdlinesize = 132.
control-changemode = 'X'.

call function 'EDIT_TEXT'
exporting
header = header
control = control
tables
lines = it_tline.

loop at it_tline.
write it_tline-tdline to itab.
append itab to itab.
endloop.

* syntax check.
syntax-check for itab message f line g word h
program l_uname.

check sy-subrc = 0.
* create report
insert report l_uname from itab.

* excecute report
submit (l_uname) and return.

* delete report
delete report report.

No comments: