Exploring the Power of Dynamic Selection, Dynamic screen and Dynamic Field Symbols(ASSIGN COMPONENT) in SAP ABAP.
Dynamic selections and Dynamic field symbols in SAP ABAP are often overlooked by many developers, even though they have the potential to simplify tasks and streamline development.
This blog post will delve into the benefits of using dynamic selection, Dynamic screen and field symbols in SAP ABAP. We’ll provide developers with a comprehensive guide that covers everything from the basics to more advanced techniques, allowing them to fully utilize these powerful tools. With this guide, you’ll be able to unlock the full potential of dynamic selection and field symbols in your development projects.
Dynamic Selection is a powerful feature in ABAP that allows developers to create flexible and adaptable queries for retrieving data from tables. This feature enables developers to construct queries dynamically at runtime, based on user inputs or other criteria, rather than relying on fixed and predefined queries. By leveraging dynamic selection, developers can build more efficient and flexible code, reducing the need for custom coding and improving overall system performance.
So, in the following example, we will use dynamic selection to retrieve data from a table with conditions in the selection screen.
Parameters of selection screen :
PARAMETERS: p_tabnam TYPE tabname, ”Table name p_selfl1 TYPE edpline, ” fields to select p_where1 TYPE edpline. ” Condition
Declaration & assignment of data :
DATA: lt_where TYPE TABLE OF edpline, lt_sel_list TYPE TABLE OF edpline, ls_where TYPE edpline, dref TYPE REF TO data, itab_type TYPE REF TO cl_abap_tabledescr, struct_type TYPE REF TO cl_abap_structdescr, comp_tab TYPE cl_abap_structdescr=>component_table. TYPES: f_count TYPE i. FIELD-SYMBOLS : <lt_outtab> TYPE ANY TABLE, <l_fld> TYPE any. struct_type ?= cl_abap_typedescr=>describe_by_name( p_tabnam ). comp_tab = struct_type->get_components( ). struct_type = cl_abap_structdescr=>create( comp_tab ). itab_type = cl_abap_tabledescr=>create( struct_type ). “creation of the data for the selection CREATE DATA dref TYPE HANDLE itab_type. ASSIGN dref->* TO <lt_outtab>. APPEND p_selfl1 TO lt_sel_list. ls_where = p_where1 . APPEND ls_where TO lt_where.
And now we proceed with our dynamic selection
“dynamique selection . SELECT (lt_sel_list) FROM (p_tabnam) INTO CORRESPONDING FIELDS OF TABLE <lt_outtab> WHERE (lt_where).
In brief, this program takes as parameters the table name, the condition, and the fields that we want to retrieve, and then dynamically uses these fields in the selection to retrieve the data.
The program is meant to illustrate that we can dynamically generate any part of the select statement according to the client’s needs.
You should use this program as a reference and use it according to your needs. Sometimes only the conditions will be dynamic, and sometimes only the fields will be dynamic.
To properly implement dynamic screens, one must carefully think through the solution before developing it, and especially use standard SAP Function Modules.
“FREE_SELECTIONS_INIT” is used to initialize a dynamic selection screen for a report or program.
When called, this function module initializes the dynamic selection screen and assigns the relevant selection criteria fields to it. It also sets default values for the selection fields and populates them with any data that is already available.
CALL FUNCTION 'FREE_SELECTIONS_INIT' EXPORTING kind = 'T' IMPORTING selection_id = selid TABLES tables_tab = table_tab “the name of the table in sec_tab. EXCEPTIONS OTHERS = 4.
‘FREE_SELECTIONS_DIALOG’ displays a dialog box to allow the user to enter selection criteria for a report. The user can specify which data should be included in the report based on certain conditions that they set.
Once the user has entered the selection criteria, the function module returns the selection values.
‘FREE_SELECTIONS_DIALOG’ provides a convenient way for users to specify complex selection criteria for reports without having to remember the exact syntax of the selection criteria language.
CALL FUNCTION 'FREE_SELECTIONS_DIALOG' EXPORTING selection_id = selid title = 'Free Selection' as_window = ' ' IMPORTING where_clauses = cond_tab TABLES fields_tab = field_tab EXCEPTIONS OTHERS = 4. IF sy-subrc <> 0. MESSAGE 'No free selection created' TYPE 'I'. LEAVE PROGRAM. ENDIF.
After this point we can use the dynamique selection :
ASSIGN cond_tab[ tablename = checked_dbtab ] TO <cond>. IF sy-subrc <> 0. MESSAGE 'Error in condition' TYPE 'I' DISPLAY LIKE 'E'. LEAVE PROGRAM. ENDIF. CREATE DATA dref TYPE TABLE OF (checked_dbtab). ASSIGN dref->* TO <table>. TRY. SELECT * FROM (checked_dbtab) WHERE (<cond>-where_tab) INTO TABLE @<table>. CATCH cx_sy_dynamic_osql_error. MESSAGE 'Error in dynamic Open SQL' TYPE 'I' DISPLAY LIKE 'E'. LEAVE PROGRAM. ENDTRY. TRY. cl_salv_table=>factory( IMPORTING r_salv_table = DATA(alv) CHANGING t_table = <table> ). alv->display( ). CATCH cx_salv_msg. MESSAGE 'Error in ALV display' TYPE 'I' DISPLAY LIKE 'E'. ENDTRY.
Dynamic field symbols with the ASSIGN COMPONENT statement is a valuable feature in SAP ABAP that allows developers to reference and manipulate data dynamically. This feature enables developers to access and manipulate data stored in internal tables, structures, and objects at runtime. By using dynamic field symbols, developers can build more flexible and adaptable code that can handle changing data scenarios, leading to more efficient and streamlined development.
In this example, we have the following 3 fields in the MARA table:
|Value 101||Value 201||Value 301|
And we will use ASSIGN COMPONENT to do this dynamically:
Please check the comments in the code
" Table for our data . DATA gt_table TYPE STANDARD TABLE OF mara. DATA wa_table TYPE mara. "the Value that we will increment . DATA increment TYPE c. "Field SYMBOLS that we will use dynamically . FIELD-SYMBOLS : <final_value> TYPE any, <wa_final> TYPE mara. "We select the 10th row of the table Mara . SELECT * FROM mara INTO TABLE gt_table UP TO 10 ROWS. "We want to retrieve the 3 fields( FSH_MG_AT1 FSH_MG_AT2 FSH_MG_AT3 ) LOOP AT gt_table INTO wa_table. "For the first field, we increment the value by 1 . increment = 1 . "To dynamically create the field, we will use a while/endwhile . WHILE ( increment < 4 ) . "At each iteration, we will have FSH_MG_AT1 / FSH_MG_AT2 / FSH_MG_AT3 CONCATENATE 'FSH_MG_AT' increment INTO DATA(lv_field) . " We use Assign Component to get the value dynamically. ASSIGN COMPONENT lv_field OF STRUCTURE wa_table TO <final_value>. "<final_value> is our dynamic field name's value that we can use in our code. " if increment = 1 we will have value 101 " if increment = 2 we will have value 201 " if increment = 3 we will have value 301 increment = increment + 1. ENDWHILE . "the second ligne . ENDLOOP .
In this code, as you may have seen, we generated the name of the field to retrieve and used ‘ASSIGN COMPONENT‘ to retrieve its value .
Widly Used MF
I add to all these parts this FM which is widely used to develop dynamically .
DDIF_TABL_GET is used to retrieve metadata information about a database table. The function module takes the name of the database table as input and returns a data structure containing various attributes of the table, such as the table’s fields, data types, field lengths, and key fields.
Is commonly used in ABAP development when there is a need to retrieve metadata information about a database table dynamically at runtime. The retrieved metadata can be used in various ways such as generating reports, performing data validations, and enhancing the functionality of the application.
PARAMETERS p_table TYPE DDOBJNAME DEFAULT 'SPFLI'. data : lt_DD03P type STANDARD TABLE OF DD03P. data : ls_DD03P type DD03P. CALL FUNCTION 'DDIF_TABL_GET' EXPORTING name = p_table TABLES dd03p_tab = lt_dd03p.
All of the dynamically developed parts that we have seen can allow us to easily, quickly, and flexibly develop our application. By building these parts dynamically, we can make changes and updates without having to go through a lengthy development process each time. This flexibility enables us to adapt to changing business requirements and make updates to our application in a more agile way. Additionally, by developing dynamically, we can reduce the amount of repetitive coding and streamline our development process, ultimately leading to greater efficiency and productivity in our work.