Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamically determine line type

former_member201275
Active Contributor

I have a field ls_text type any.

Sometimes ls_text will contain 'ID12243 Glenn Anjikovic' i.e. (userid, first name, lastname),

other times ls_text will have 'XYZCompany Los Angeles USA' i.e. (Company name, City, Country).

Is there any way to determine the fields in the structure during runtime? There could be more possibilities than just the 2 listed above.

1 ACCEPTED SOLUTION

FredericGirod
Active Contributor
data o_struct_descr TYPE REF TO cl_abap_structdescr.
o_struct_descr ?= cl_abap_typedescr=>describe_by_data( ls_text ).
data(structure_fields) = o_struct_descr->get_components( ).

something like that could works

2 REPLIES 2

FredericGirod
Active Contributor
data o_struct_descr TYPE REF TO cl_abap_structdescr.
o_struct_descr ?= cl_abap_typedescr=>describe_by_data( ls_text ).
data(structure_fields) = o_struct_descr->get_components( ).

something like that could works

UBrand251
Participant

Whilst Frederic's code fragment goes the right way, I would like to share a link to a previous post:

Answer To: How to get datatype of fields in dynamic structures (2005)

(https://answers.sap.com/questions/952106/how-to-get-datatype-of-fields-in-dynamic-structure.html)

In there, you find an expanded code segment that can easily be completed into a small report, which will accept the name of a structure and print properties of each field, identified dynamically at run-time.

My own added value is a friendly reminder to Unicode. Since release NW 6.10 Unicode has been an option, but with NW 7.x and S/4HANA it's become mandatory.

The name of your field "ls_text" suggests that your various structures contain only character-like fields, e.g. 'C','N','D' or 'T'; in this case you're fine. Structures with number-like types, e.g. 'P', 'I' or 'F', or byte-like types, e.g. 'X', require consideration of fragment view and alignment gaps. Have a look at the following link:

SAP Help: ABAP and Unicode

(https://help.sap.com/viewer/cfae740a0a21455dbe6e510c2d86e36a/LATEST/en-US/623f2cadb35311d5993800508b6b8b11.html)