Creating a variable with dynamic variable type in SAP ABAP



You can use RTTS related API to create a Standard table like RANGE which has components like 'LOW', 'HIGH', 'EQ' and 'OPTION'

data:    rr_data              type ref to data,    rt_range_string      type range of string,    rs_range_string      like line of rt_range_string,    rt_component         type abap_component_tab,    rs_component         type line of abap_component_tab,    rt_range_components  type abap_component_tab,    ro_struc_descr       type ref to cl_abap_structdescr,    ro_table_descr       type ref to cl_abap_tabledescr,    ro_data_descr        type ref to cl_abap_datadescr.

field-symbols

<var_value> type any, <rt_range_type> type standard table,   //<rt_range_type> is your range table <rs_range_type> type any. data(var_type) = 'BU_PARTNER'. create data rr_data type (var_type). ro_struc_descr ?= cl_abap_structdescr=>describe_by_data( p_data = rs_range_string ). rt_component = ro_struc_descr->get_components( ). ro_data_descr ?= cl_abap_elemdescr=>describe_by_name( var_type ). rt_range_components = value #( for comp in rt_component (    name = comp-name    type = cond #(       when comp-name eq 'SIGN'       or comp-name eq 'OPTION'       then comp-type       else ro_data_descr ) ) ). ro_struc_descr ?= cl_abap_structdescr=>create( rt_range_components ). ro_table_descr ?= cl_abap_tabledescr=>create( ro_struc_descr ). create data rr_data type handle ro_table_descr. assign rr_data->* to <rt_range_type>. create data rr_data like line of <rt_range_type>. assign rr_data->* to <rs_range_type>. assign component 'SIGN' of structure <rs_range_type> to <var_value>. <var_value> = 'I'. assign component 'OPTION' of structure <rs_range_type> to <var_value>. <var_value> = 'EQ'. assign component 'LOW' of structure <rs_range_type> to <var_value>. <var_value> = 'X1'. assign component 'HIGH' of structure <rs_range_type> to <var_value>. <var_value> = 'X2'.


Updated on: 2020-02-13T10:12:00+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements