Application Development and Automation 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: 
Read only

Dynamic creation of data types and objects with selection screen

Former Member
0 Likes
1,174

Hi,

I have the following scenario:

I have a selection screen from where I get the names of several tables and fields, e.g.:

- Field 'Table1' has the value 'KNA1' and field 'Field1' has the value 'KUNNR'.

I want to use those variables in several ways. I want to use the individual "pieces" - to adress the table KNA1 and the field KUNNR, respectively -

as well as concatenations "KNA1-KUNNR" and "KNA1~KUNNR" - I have already done those concatenations.

Now I want to use the variables in my declarations: I am declaring an internal table, with a linetype containing all the fields I get from the selection screen.

Thus, the declaration looks like this:

BEGIN OF line01_type,

     KUNNR TYPE KNA1-KUNNR,

     ...

END OF line01_type.

I have read quite a number of help sites and tried out everything I could think of, from using and assigning field-symbols to declaring reference_data_types, e.g. referring to the DB object "KNA1-KUNNR" - but whereas the field I cannot get the field names at all - when I use either Field1 or a field-symbol <fld1>, that is interpreted as a string literal - I could only get the data_type C by referring to the DB objects - but then my SELECT statement with an INTO CORRESPONDING FIELDS clause failed. I don't understand why, when I had it all hard-coded (writing KUNNR TYPE KNA1-KUNNR), the data_types were all correct. I can't see the difference.

Please, can someone help me out here?

Thanks a lot!

Best regards,

Sapperdapper

Hi,

I have the following scenario:

I have a selection screen from where I get the names of several tables and fields, e.g.:

- Field 'Table1' has the value 'KNA1' and field 'Field1' has the value 'KUNNR'.

I want to use those variables in several ways. I want to use the individual "pieces" - to adress the table KNA1 and the field KUNNR, respectively -

as well as concatenations "KNA1-KUNNR" and "KNA1~KUNNR" - I have already done those concatenations.

Now I want to use the variables in my declarations: I am declaring an internal table, with a linetype containing all the fields I get from the selection screen.

Thus, the declaration looks like this:

BEGIN OF line01_type,

     KUNNR TYPE KNA1-KUNNR,

     ...

END OF line01_type.

I have read quite a number of help sites and tried out everything I could think of, from using and assigning field-symbols to declaring reference_data_types, e.g. referring to the DB object "KNA1-KUNNR" - but whereas the field I cannot get the field names at all - when I use either Field1 or a field-symbol <fld1>, that is interpreted as a string literal - I could only get the data_type C by referring to the DB objects - but then my SELECT statement with an INTO CORRESPONDING FIELDS clause failed. I don't understand why, when I had it all hard-coded (writing KUNNR TYPE KNA1-KUNNR), the data_types were all correct. I can't see the difference.

Please, can someone help me out here?

Thanks a lot!

Best regards,

Sapperdapper

9 REPLIES 9
Read only

Former Member
0 Likes
1,133

Hi,

have you tried using ASSIGN COMPONENT xyv-fieldname OF STRUCTURE ls_data TO
<lv_data>.

type declaration should by any/data.

to create a internal table for the result of your table you can make use of the classes CL_ABAP_TYPEDESCR (or another one in the inheritance hierarchy).

Best regards

Robert

Read only

0 Likes
1,133

Hi,

I will make sure to try out your proposal. I am rather new to SAP and I know that, so I will not discard any help that is offered here.

However, I have tried out something else: The data type of the fields in my internal table is not that important actually since in the end, the data from there will be exported from SAP as an XML structure. I got several runtime_exceptions because of the data type, so I tried just formatting all the fields in my internal table as STRING types, that way I have one problem less with the input from the selection screen - however, that does not work for a date field (in my example, ERDAT from table KNA1) - and I could not find anything helpful in my ABAP reference book: According to that, a source field of type d is treated like a source field type c to convert into STRING type, and a C-type source field seems to be treated by ABAP automatically, or not at all - there is nothing on whether I have to do anything, so I assume it happens automatically. But it doesn't.

Can someone tell me what I have to do to convert a date-type field to a STRING-type field (to fill the contents of a DATE-type field from a DB table into a STRING-type field in an internal table)?

Thanks a lot!

Regards,

Sapperdapper

Read only

0 Likes
1,133

Hi Robert,

if I understand your code correctly, you are proposing to ASSIGN a component (field) of a structure (which would be the line_declaration for my internal table itab?) to a FIELD-SYMBOL?

Forgive me, but I don't quite get it: That would IMHO require that I already have a structure (the linetype of my itab)? But I don't have that to begin with, I want to build it using the input from the selection screen.

To be more precise: I imagine that a FIELD-SYMBOL is something generic (a placeholder) and the thing I assign to it must be a specific object, thus I need to have that - but in the case of my structure, I don't.

Read only

0 Likes
1,133

HI,

maybe this helps you:

data lv_date type ERDAT_RF.

data lv_string type string.



lv_date = sy-datum.

lv_string = lv_date.

BR

Robert

Read only

0 Likes
1,133

Hi Robert,

perfect! So I just have to know (my program has to "know", that is) whether a field entered by the user is a DATE-type field. I'm sure there is a command in ABAP to find out the data type.

Two questions on that if you don't mind:

- Can I somehow write something like (continuing your example)  >> lv_date = F1 (F1 being an input
   field from my selection screen) <<?

-  I would then have to use that field >lv_string< as a component in my linetype declaration, along
  with other fields like > KUNNR, BUKRS, GJAHR > and so on - can I somehow assign it the 
  technical name it has in the DB table (e.g. ERDAT)? Otherwise my SELECT which uses the clause
  INTO CORRESPONDING FIELDS will fail.

Thanks a lot!

Best Regards,

Sapperdapper

P.S.: Actually, I can do it simpler than that: When I find that one of the fields the user has selected via the selection screen is a DATE-type field, I can just state TYPE D in the declaration of my internal-table-line-type - otherwise I'd have to convert that field into a STRING in every line I fetch from the DB table, that would cost a lot of performance. Everything else can be STRING, regardless whether there are alphanumeric or numeric ciphers in the DB-table-field.

Great, that solves the problem of data types.

Btw., the two questions I have posted can be considered done in this case. All that remains is to learn how I can test the data type of a field (that is supposedly in a DB table) when I know its name (from the selection screen).

P.S.: Okay, I have to distinguish between DATE-type fields, NUM-type fields (I can always assign >>TYPE p DECIMALS 2<< to those) and STRING - but that does not make much difference. If I can test fields for their datatype, then Thistle do nicely.

Read only

0 Likes
1,133

sure, should be possible also without using a string instead of a date field

BR

Robert

Read only

0 Likes
1,133

Hi Robert,

yes, that should be doable - only I haven't yet found anything anywhere - in my ABAP reference book or online - about a way to test fields for their datatype?! I am sure there is a way in ABAP, that is a very important aspect, after all.

Can you help me out there?

Thanks a lot!

Best regards,

Sapperdapper

Read only

0 Likes
1,133

Hi,

please try one of the RTD classes like CL_ABAP_TYPEDESCR.

BR

Robert

Read only

0 Likes
1,133

Hi all,

I'll close this and mark it as "Assumed answered".

Thanks for all the help!

Best regards,

Sapperdapper