cancel
Showing results for 
Search instead for 
Did you mean: 

Setting length for dynamically created context attribute

Former Member
0 Kudos

Hi Friends,

My requirement is I need to create the context node and attribute dynamically.The attributes to be created is determined from a ztable where the field names, types and length are maintained.Is it possible to create attributes with the specified length and type dynamically.

I do not want to create the attributes by referring data elements or from structure.

Can this be done?

Thanks in advance.

Regards,

Prabhuraj

Accepted Solutions (1)

Accepted Solutions (1)

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

Yes it can. You will need to create an ABAP RTTI object (RunTime Type Information). I've used this approach to create dynamic UIs off of MDM (which an external tool and you read the metadata via APIs - so there are no ABAP DDic structures to use).

You need to generate an RTTI Structure type using the cl_abap_structdescr=>create. You have to build up the internal table parameter p_components (type CL_ABAP_STRUCTDESCR=>COMPONENT_TABLE) for the individual components of the structure.

So far none of this is really WDA specific. Just normal RTTS stuff to build a dynamic data object.

You cna then use the node info method ADD_NEW_CHILD_NODE. Use the static_element_rtti parameter to pass in the dynamic structure definition you just created.

struct_type = cl_abap_structdescr=>create( comp_tab ).
      dyn_node_info = rootnode_info->add_new_child_node(
         name                   = 'MDM_DATA'
         is_mandatory           = abap_false
         is_mandatory_selection = abap_false
         static_element_rtti    = struct_type
         is_multiple            = abap_true
         is_static              = abap_false ).

If needed you can then dynamically create an internal table that matches this structure:

table_type = cl_abap_tabledescr=>create( p_line_type = struct_type ).
      create data my_table type handle table_type.
      assign my_table->* to <table>.
...
      dyn_node->bind_table( <table> ).

Former Member
0 Kudos

Hi Thomas,

Thanks for giving me the solution.

Currently I am also looking into the RTTI but I am not able to create it. For example, say I wanted to create two attributes of below ones:

1. Field name - STID

Field type - C (char)

Field length - 10

2. Field name - Rank

Field type - i (integer)

Field length - 2

How can I use CL_ABAP_STRUCTDESCR=>CREATE to create these two attributes dynamically. In COMPONENT_TABLE how can I pass field type and length?

Can you help me by giving some sample code for this?

Thanks in advance.

Regards,

Prabhuraj

Former Member
0 Kudos

Thanks Thomas. Achieved like below.

data: lt_comp TYPE cl_abap_structdescr=>component_table,

lo_new_type TYPE REF TO cl_abap_structdescr,

la_comp LIKE LINE OF lt_comp.

la_comp-name = 'STID'.

la_comp-type = cl_abap_elemdescr=>get_c(

p_length = 10 ).

APPEND la_comp to lt_comp.

la_comp-name = 'RANK'.

la_comp-type = cl_abap_elemdescr=>get_n(

p_length = 2 ).

APPEND la_comp to lt_comp.

lo_new_type = cl_abap_structdescr=>create( lt_comp ).

And in the node assigned lo_new_type to static_element_rtti

CALL METHOD lr_node_info->add_new_child_node

EXPORTING

name = lv_node

is_mandatory = abap_false

static_element_rtti = lo_new_type

is_multiple = abap_false

is_multiple_selection = abap_false

is_singleton = abap_false

is_initialize_lead_selection = abap_true

is_static = abap_false

RECEIVING

child_node_info = lr_node_info.

Answers (0)