Saturday, April 09, 2011

Dynamic selection screen

Sometime ago, I started doing research in creating a dynamic selection screen similar to se16n initial screen. Today, I found a forum in sap sdn about creating this type of selection screens. I have to paste this code before someone will delete the page  or I will not be able to locate this again (funny though). This code was contributed by Sumant Sura to give credit to his work. I can't guarantee yet if how this code works. I will try to code this when I get by the office again then I will let you know. For more info about dynamic selection screen, you may visit this forum.

FUNCTION y_ss_test_dynamic_selection.
*"----------------------------------------------------------------------
""Local interface:

*" IMPORTING
*" REFERENCE(TABNAME) LIKE DD02L-TABNAME
*" EXPORTING
*" REFERENCE(DS_CLAUSES) TYPE RSDS_WHERE
*" EXCEPTIONS
*" TABLE_NOT_VALID
*" OTHER_ERROR
*"----------------------------------------------------------------------

DATA texpr TYPE rsds_texpr.
DATA twhere TYPE rsds_twhere.
DATA trange TYPE rsds_trange.

DATA BEGIN OF qcat. "Selections View for
INCLUDE STRUCTURE rsdsqcat. "Free Selectoptions
DATA END OF qcat.

DATA BEGIN OF tabs OCCURS 10.
INCLUDE STRUCTURE rsdstabs.
DATA END OF tabs.

DATA BEGIN OF fields OCCURS 10.
INCLUDE STRUCTURE rsdsfields.
DATA END OF fields.

DATA BEGIN OF efields OCCURS 10.
INCLUDE STRUCTURE rsdsfields.
DATA END OF efields.

DATA selid LIKE rsdynsel-selid.
DATA actnum LIKE sy-tfill.
DATA title LIKE sy-title VALUE 'Selection Screen'.

DATA: maxnum LIKE sy-subrc VALUE '69'.

CLEAR tabs.
tabs-prim_tab = tabname.
COLLECT tabs.

DATA: position LIKE dd03l-position.
DATA: keyflag LIKE dd03l-keyflag.

CLEAR fields.

fields-tablename = tabname.
fields-sign = 'I'.

DATA: step LIKE sy-subrc.

SELECT fieldname keyflag position
INTO (fields-fieldname, keyflag, position)
FROM dd03l
WHERE tabname = tabname
AND fieldname NOT LIKE '.INCLU%'
AND datatype NE 'CLNT'
ORDER BY position.
ADD 1 TO step.
CHECK step LE maxnum.
IF keyflag 'X'.
efields = fields.
APPEND efields.
ENDIF.
APPEND fields.
ENDSELECT.

IF sy-subrc 0.
RAISE table_not_valid.
ENDIF.

CALL FUNCTION 'FREE_SELECTIONS_INIT'
EXPORTING
expressions = texpr
kind = 'F'
IMPORTING
selection_id = selid
expressions = texpr
where_clauses = twhere
field_ranges = trange
number_of_active_fields = actnum
TABLES
tables_tab = tabs
fields_tab = fields
fields_not_selected = efields
EXCEPTIONS
fields_incomplete = 01
fields_no_join = 02
field_not_found = 03
no_tables = 04
table_not_found = 05
expression_not_supported = 06
incorrect_expression = 07
illegal_kind = 08
area_not_found = 09
inconsistent_area = 10
kind_f_no_fields_left = 11
kind_f_no_fields = 12
too_many_fields = 13.

IF sy-subrc = 0.
CALL FUNCTION 'FREE_SELECTIONS_DIALOG'
EXPORTING
selection_id = selid
title = title
IMPORTING
where_clauses = twhere
expressions = texpr
field_ranges = trange
number_of_active_fields = actnum
TABLES
fields_tab = fields
EXCEPTIONS
internal_error = 01
no_action = 02
no_fields_selected = 03
no_tables_selected = 04
selid_not_found = 05.

IF sy-subrc = 0.
CLEAR ds_clauses.
MOVE tabname TO ds_clauses-tablename.
READ TABLE twhere WITH KEY ds_clauses-tablename INTO ds_clauses.
IF sy-subrc 0.
RAISE other_error.
ENDIF.
ELSE.
RAISE other_error.
ENDIF.
ELSE.
RAISE other_error.
ENDIF.

ENDFUNCTION.

Other related links:
FM free_selection_dialog

4 comments:

Unknown said...

Yahoo! I made this thing run now and it is cool! Try to check my revised code:

report ZTEST_DYN_SEL_SCR.

TYPE-pools: rsds.

DATA texpr TYPE rsds_texpr.
DATA twhere TYPE rsds_twhere.
DATA trange TYPE rsds_trange.

DATA: tabname TYPE string.

DATA BEGIN OF qcat. "Selections View for
INCLUDE STRUCTURE rsdsqcat. "Free Selectoptions
DATA END OF qcat.

DATA BEGIN OF tabs OCCURS 10.
INCLUDE STRUCTURE rsdstabs.
DATA END OF tabs.

DATA BEGIN OF fields OCCURS 10.
INCLUDE STRUCTURE rsdsfields.
DATA END OF fields.

DATA BEGIN OF efields OCCURS 10.
INCLUDE STRUCTURE rsdsfields.
DATA END OF efields.

DATA selid LIKE rsdynsel-selid.
DATA actnum LIKE sy-tfill.
DATA title LIKE sy-title VALUE 'Selection Screen'.

DATA: maxnum LIKE sy-subrc VALUE '69'.

tabname = 'T001'.

CLEAR tabs.
tabs-prim_tab = tabname.
COLLECT tabs.

DATA: position LIKE dd03l-position.
DATA: keyflag LIKE dd03l-keyflag.

CLEAR fields.

fields-tablename = tabname.
fields-sign = 'I'.

DATA: step LIKE sy-subrc.

SELECT fieldname keyflag position
INTO (fields-fieldname, keyflag, position)
FROM dd03l
WHERE tabname = tabname
AND fieldname NOT LIKE '.INCLU%'
AND datatype NE 'CLNT'
ORDER BY position.
ADD 1 TO step.
CHECK step LE maxnum.
IF keyflag = 'X'.
efields = fields.
APPEND efields.
ENDIF.
APPEND fields.
ENDSELECT.

IF sy-subrc = 0.
RAISE table_not_valid.
ENDIF.

CALL FUNCTION 'FREE_SELECTIONS_INIT'
EXPORTING
expressions = texpr
kind = 'F'
IMPORTING
selection_id = selid
expressions = texpr
where_clauses = twhere
field_ranges = trange
number_of_active_fields = actnum
TABLES
tables_tab = tabs
fields_tab = fields
fields_not_selected = efields
EXCEPTIONS
fields_incomplete = 01
fields_no_join = 02
field_not_found = 03
no_tables = 04
table_not_found = 05
expression_not_supported = 06
incorrect_expression = 07
illegal_kind = 08
area_not_found = 09
inconsistent_area = 10
kind_f_no_fields_left = 11
kind_f_no_fields = 12
too_many_fields = 13.

IF sy-subrc = 0.
CALL FUNCTION 'FREE_SELECTIONS_DIALOG'
EXPORTING
selection_id = selid
title = title
IMPORTING
where_clauses = twhere
expressions = texpr
field_ranges = trange
number_of_active_fields = actnum
TABLES
fields_tab = fields
EXCEPTIONS
internal_error = 01
no_action = 02
no_fields_selected = 03
no_tables_selected = 04
selid_not_found = 05.

IF sy-subrc = 0.
*CLEAR ds_clauses.
*MOVE tabname TO ds_clauses-tablename.
*READ TABLE twhere WITH KEY ds_clauses-tablename INTO ds_clauses.
*IF sy-subrc = 0.
*RAISE other_error.
*ENDIF.
*ELSE.
*RAISE other_error.
*ENDIF.
*ELSE.
*RAISE other_error.
endif.
ENDIF.

Unknown said...

This is only a start-up code wherein you can create your logic after the dynamic selection screen. Please let me know guys if you have any improvement with the program. Tnx!

Luis said...

Hi Riv:

thanks for your code, it's really nice. Just a little bug (I think)
IF sy-subrc = 0.
RAISE table_not_valid.
ENDIF. should be <> 0

Best regards
Luis

Anonymous said...

Hi Luis, I think Raise is used in exception variables and perhaps I havnt shown the declaration in my code. I will try to check this out...

Post a Comment