‎2007 Mar 12 6:38 AM
Hi,
i am trying to generate a program dynamically using function module 'RSS_TEMPLATE_INSTANTIATE'.
can any one help me by sending simple example code to understand this concept.
thanks in advance.
regards,
Bharat.
‎2007 Mar 12 6:53 AM
Hi,
Herewith i am sending the sample dynamic alv report. Kindly go through it.
REPORT YMS_DYNAMICALV.
type-pools: slis.
field-symbols: <dyn_table> type standard table,
<dyn_wa>.
data: alv_fldcat type slis_t_fieldcat_alv,
it_fldcat type lvc_t_fcat.
selection-screen begin of block b1 with frame title text-001.
parameters: p_flds(5) type c.
selection-screen end of block b1.
start-of-selection.
build the dynamic internal table
perform build_dyn_itab.
write 5 records to the alv grid
do 5 times.
perform build_report.
enddo.
call the alv grid.
perform call_alv.
************************************************************************
Build_dyn_itab
************************************************************************
form build_dyn_itab.
data: new_table type ref to data,
new_line type ref to data,
wa_it_fldcat type lvc_s_fcat.
Create fields .
do p_flds times.
clear wa_it_fldcat.
wa_it_fldcat-fieldname = sy-index.
wa_it_fldcat-datatype = 'CHAR'.
wa_it_fldcat-intlen = 5.
append wa_it_fldcat to it_fldcat .
enddo.
Create dynamic internal table and assign to FS
call method cl_alv_table_create=>create_dynamic_table
exporting
it_fieldcatalog = it_fldcat
importing
ep_table = new_table.
assign new_table->* to <dyn_table>.
Create dynamic work area and assign to FS
create data new_line like line of <dyn_table>.
assign new_line->* to <dyn_wa>.
endform.
*********************************************************************
Form build_report
*********************************************************************
form build_report.
data: fieldname(20) type c.
data: fieldvalue(5) type c.
data: index(3) type c.
field-symbols: <fs1>.
do p_flds times.
index = sy-index.
Set up fieldvalue
concatenate 'FLD' index into
fieldvalue.
condense fieldvalue no-gaps.
<b> assign component index of structure <dyn_wa> to <fs1>.
<fs1> = fieldvalue.</b>
enddo.
Append to the dynamic internal table
append <dyn_wa> to <dyn_table>.
endform.
************************************************************************
CALL_ALV
************************************************************************
form call_alv.
data: wa_cat like line of alv_fldcat.
do p_flds times.
clear wa_cat.
wa_cat-fieldname = sy-index.
wa_cat-seltext_s = sy-index.
wa_cat-outputlen = '5'.
append wa_cat to alv_fldcat.
enddo.
Call ABAP List Viewer (ALV)
call function 'REUSE_ALV_GRID_DISPLAY'
exporting
it_fieldcat = alv_fldcat
tables
t_outtab = <dyn_table>.
endform.
Thanks,
Shankar
‎2007 Mar 12 6:55 AM
hi Bharat,
check the following code; it is really useful to understand the concept:
*&---------------------------------------------------------------------*
*& Form generate_program
*&---------------------------------------------------------------------*
FORM generate_program CHANGING p_program TYPE program
syst_info TYPE syst.
DATA: text_line TYPE line.
DATA: t_codings TYPE STANDARD TABLE OF rssource,
ls_coding TYPE rssource,
t_txtpool TYPE STANDARD TABLE OF textpool WITH HEADER LINE.
* constructe program name
WHILE p_program IS INITIAL OR sy-subrc = 0.
WAIT UP TO 1 SECONDS.
p_program+00(5) = 'SBDLS'.
p_program+05(3) = sy-mandt.
p_program+08(8) = sy-datum.
p_program+16(6) = sy-uzeit.
CALL FUNCTION 'SWY_PROGRAM_EXISTS'
EXPORTING
program = p_program
* IMPORTING
* TRDIR_ENTRY =
EXCEPTIONS
program_not_exists = 1
OTHERS = 2.
ENDWHILE.
text_line = 'Programm & wird gerade generiert'(a00).
REPLACE '&' WITH p_program INTO text_line.
CONDENSE text_line.
CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
EXPORTING
text = text_line.
CALL FUNCTION 'RSS_TEMPLATE_INSTANTIATE'
EXPORTING
i_template = c_template
i_program_name = p_program
i_program_type = 'S'
i_meta_object = p_program
* I_UNI_IDC25 = ''
* I_CLIENT = SY-MANDT
* I_GLOBAL_CHECK = ' '
i_global_program = 'X'
i_force_compilation = 'X'
i_no_program_check = ' '
* I_DEBUG_LEVEL =
* IMPORTING
* O_ERROR_LINE =
* O_ERROR_MESSAGE =
* E_ERROR_LINE =
* E_ERROR_MESSAGE =
* E_T_CODE =
* E_UCCHECK =
TABLES
e_t_program_source = t_codings
* CHANGING
* C_TX_TEXTPOOL =
EXCEPTIONS
template_not_found = 1
template_syntax_error = 2
program_syntax_error = 3
internal_error = 4
invalid_input = 5
generation_error = 6
OTHERS = 7
.
IF sy-subrc <> 0.
IF sy-subrc = 3.
sy-tabix = syst-msgv2.
READ TABLE t_codings INTO ls_coding INDEX sy-tabix.
* Syntaxfehler im Programm & Zeile &
syst_info-msgid = 'B1'.
syst_info-msgty = 'E'.
syst_info-msgno = '458'.
syst_info-msgv1 = p_program.
SHIFT ls_coding-line LEFT DELETING LEADING SPACE.
syst_info-msgv2 = ls_coding-line.
ELSE.
MOVE-CORRESPONDING syst TO syst_info.
ENDIF.
EXIT.
ENDIF.
CHECK NOT t_codings[] IS INITIAL.
CALL FUNCTION 'PRETTY_PRINTER'
EXPORTING
inctoo = ''
* IMPORTING
* INDENTATION_MAYBE_WRONG =
TABLES
ntext = t_codings
otext = t_codings
EXCEPTIONS
enqueue_table_full = 1
include_enqueued = 2
include_readerror = 3
include_writeerror = 4
OTHERS = 5
.
IF sy-subrc <> 0.
MOVE-CORRESPONDING syst TO syst_info.
EXIT.
ENDIF.
INSERT REPORT p_program FROM t_codings PROGRAM TYPE 'S'.
IF sy-subrc <> 0.
syst_info-msgty = 'E'.
syst_info-msgno = 457.
syst_info-msgid = 'B1'.
syst_info-msgv1 = p_program.
* Fehler beim Anlegen des Programms &
EXIT.
ENDIF.
t_txtpool-id = 'R'.
t_txtpool-key = ''.
t_txtpool-length = '40'.
t_txtpool-entry = 'Umsetzung logischer Systemnamen'(a01).
APPEND t_txtpool.
INSERT TEXTPOOL p_program FROM t_txtpool LANGUAGE sy-langu.
IF sy-subrc <> 0.
syst_info-msgty = 'E'.
syst_info-msgno = 457.
syst_info-msgid = 'B1'.
syst_info-msgv1 = p_program.
* Fehler beim Anlegen des Programms &
EXIT.
ENDIF.
DATA: output_text(72),
error_line(5) TYPE n,
error_word(72).
SYNTAX-CHECK FOR t_codings MESSAGE output_text
LINE error_line
WORD error_word
PROGRAM p_program.
IF sy-subrc <> 0.
syst_info-msgty = 'E'.
syst_info-msgno = 458.
syst_info-msgid = 'B1'.
syst_info-msgv1 = p_program.
syst_info-msgv2 = error_line.
* Syntaxfehler im Programm & Zeile &
EXIT.
ENDIF.
ENDFORM. " generate_program
Hope this helps,
Sajan Joseph.
‎2007 Mar 12 8:50 AM
hi sajan,
if we r using this FM the generated program will be saved in any program?
can we see the generated code?
thanks in advance.
regards,
Bharat.
‎2007 Mar 12 6:56 AM
Hi,
Herewith i am sending the sample coding for the DYNAMIC ALV report.
Kindly go through it that.
REPORT YMS_DYNAMICALV.
type-pools: slis.
field-symbols: <dyn_table> type standard table,
<dyn_wa>.
data: alv_fldcat type slis_t_fieldcat_alv,
it_fldcat type lvc_t_fcat.
selection-screen begin of block b1 with frame title text-001.
parameters: p_flds(5) type c.
selection-screen end of block b1.
start-of-selection.
build the dynamic internal table
perform build_dyn_itab.
write 5 records to the alv grid
do 5 times.
perform build_report.
enddo.
call the alv grid.
perform call_alv.
************************************************************************
Build_dyn_itab
************************************************************************
form build_dyn_itab.
data: new_table type ref to data,
new_line type ref to data,
wa_it_fldcat type lvc_s_fcat.
Create fields .
do p_flds times.
clear wa_it_fldcat.
wa_it_fldcat-fieldname = sy-index.
wa_it_fldcat-datatype = 'CHAR'.
wa_it_fldcat-intlen = 5.
append wa_it_fldcat to it_fldcat .
enddo.
Create dynamic internal table and assign to FS
call method cl_alv_table_create=>create_dynamic_table
exporting
it_fieldcatalog = it_fldcat
importing
ep_table = new_table.
assign new_table->* to <dyn_table>.
Create dynamic work area and assign to FS
create data new_line like line of <dyn_table>.
assign new_line->* to <dyn_wa>.
endform.
*********************************************************************
Form build_report
*********************************************************************
form build_report.
data: fieldname(20) type c.
data: fieldvalue(5) type c.
data: index(3) type c.
field-symbols: <fs1>.
do p_flds times.
index = sy-index.
Set up fieldvalue
concatenate 'FLD' index into
fieldvalue.
condense fieldvalue no-gaps.
<b> assign component index of structure <dyn_wa> to <fs1>.
<fs1> = fieldvalue.</b>
enddo.
Append to the dynamic internal table
append <dyn_wa> to <dyn_table>.
endform.
************************************************************************
CALL_ALV
************************************************************************
form call_alv.
data: wa_cat like line of alv_fldcat.
do p_flds times.
clear wa_cat.
wa_cat-fieldname = sy-index.
wa_cat-seltext_s = sy-index.
wa_cat-outputlen = '5'.
append wa_cat to alv_fldcat.
enddo.
Call ABAP List Viewer (ALV)
call function 'REUSE_ALV_GRID_DISPLAY'
exporting
it_fieldcat = alv_fldcat
tables
t_outtab = <dyn_table>.
endform.
Thanks,
Shankar