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: 

how to access dynamic fields in a field symbol

Former Member
0 Kudos

hi

how do i access the dynamic fields created in side a field-symbol....

wht i mean is i have a table, whose workarea i assign to field symbol. but this table is runtime, altough i have debugged and found the values in this table, I want to accees the field symbol in a generic way.

say the table has 3 fields now fld1 fld2 and fld3 so i want to access the field symbol <fs> as <fs>-(name) where name can be anything fld1 or fld2 whichever i assign....

thanks. Let me know if you have any further questions.

9 REPLIES 9

naimesh_patel
Active Contributor
0 Kudos

Access the field symbol like this

FIELD-SYMBOLS: <wa>   TYPE ANY,   "<< Table work area
               <comp> TYPE ANY.     "<< variable to hold the values

WHILE sy-subrc = 0. 
  ASSIGN COMPONENT sy-index OF STRUCTURE <wa> TO <comp>. 
  WRITE / <comp>. 
ENDWHILE.

Regards,

Naimesh Patel

0 Kudos

naimesh,

i want to assign value to the field symbol itself and that to, to a particular field of it......do u get it now?

field is wht i fix runtime......maybe fld1 or fld2.

and i want to do <fs>-(field) = 20.

Former Member
0 Kudos

Field symbols are placeholders or symbolic names for other fields. They do not physically reserve space for a field, but point to its contents. A field symbol cam point to any data object. The data object to which a field symbol points is assigned to it after it has been declared in the program.

Whenever you address a field symbol in a program, you are addressing the field that is assigned to the field symbol. After successful assignment, there is no difference in ABAP whether you reference the field symbol or the field itself. You must assign a field to a field symbol before you can address it in a program.

Field symbols are similar to de-referenced pointers in the C programming language (that is, pointers to which the content operator * is applied). In ABAP, data references represent a real equivalent to pointers in the sense of variables that contain a memory address and can be used without the contents operator.

All operations programmed with field symbols are applied to the field assigned to it. A MOVE statement between two field symbols, for example, assigns the contents of the field assigned to another source field symbol to the field assigned to the target field symbol. The field symbols themselves point to the same fields after the MOVE statement as they did before the MOVEstatement.

You can create field symbols either without or with type specifications. If you do not specify a type, the field symbol inherits all of the technical attributes of the field assigned to it. If you do specify a type, the system checks during the field assignment whether the assigned field matches the type of field symbol.

Field symbols provide greater flexibility when you address data objects:

· You can assign one field symbol to another, which allows you to address subfields.

· Assignments to field symbols may extend beyond field boundaries. This allows you to address regular sequences of fields in memory efficiently.

· You can also force a field symbol to take different technical properties than those of the field assigned to it (casting).

The flexibility of field symbols provides elegant solutions to certain problems. On the other hand, it does mean that errors can easily occur. Since fields are not assigned to field symbols until runtime, the effectiveness of syntax and security checks is very limited for operations involving field symbols. This can lead to runtime errors or incorrect data assignments.

While runtime errors indicate an obvious problem, incorrect data assignments are dangerous because they can be very difficult to detect. For this reason, you should only use field symbols if you cannot achieve the same result using other ABAP statements.

For example, you may want to process part of a string where the offset and length depend on the contents of the field. You could use field symbols in this case. The MOVE statement (with your own auxiliary variables, if required) is much safer than using field symbols, since it cannot address memory beyond the boundary of a field. However, field symbols may improve performance in some cases.

To declare a field symbol, use the statement

FIELD-SYMBOLS .

For field symbols, the angle brackets are part of the syntax. They identify field symbols in the program code.

If you do not specify any additions, the field symbol.

in a static ASSIGN and:

ASSIGN (dobj) TO from the second loop pass onwards.

0 Kudos

thanks. but still my problem is not solved.

let me put this way....

i have a field-symbol into which i populate the contents of one record of type material master (MARA). i have this contents in an itab for MARA. now i want to assign field material type in the field symbol with value 'FIN'. Next time my table may be VBAK. the catch here is i know which table is there and it's fields. only thing is i need to assign the field symbol in a dynamic way. the next catch is the field symbol itself is a structure type like mara or vbak and not just one field.

hope this helps.

thanks

swanand

0 Kudos
FIELD-SYMBOLS: <wa>   TYPE ANY,   "<< Table work area
               <comp> TYPE ANY.     "<< variable to hold the values

data: wa_mara type mara.
assign wa_mara to <wa>.
 
  ASSIGN COMPONENT 'MATKL' OF STRUCTURE <wa> TO <comp>. 
  <comp> = 'FIN'.   " << your value

Check the strcture <wa>, and wa_mara now after the execution of this code . it should get updated with the value FIN

Regards,

Naimesh Patel

0 Kudos

Naimesh

Here is my code

ASSIGN COMPONENT '0FISCPER3' OF STRUCTURE <wa_data> TO <data>.

IF <data> IS ASSIGNED.

IF <data> IS INITIAL.

ASSIGN v_per TO <data>.

ENDIF.

COLLECT <wa_data> INTO c_th_data.

ENDIF.

The field )FISCPER3 from <WA_DATA> is not getting changed, although <data> is getting the value. v_per = '012'.

0 Kudos

Small change is required..!!

ASSIGN COMPONENT '0FISCPER3' OF STRUCTURE <wa_data> TO <data>.
IF <data> IS ASSIGNED.
IF <data> IS INITIAL.
*  ASSIGN v_per TO <data>. " << 
<data> = v_per   " << you need to set the value to fieldsymbol, not ASSIGN
ENDIF.
COLLECT <wa_data> INTO c_th_data.
ENDIF.

Regards,

Naimesh Patel

0 Kudos

Naimesh,

I tried that before putting in my new code. here is what I get when i do that...A dump....!!!

With the message

A new value is to be assigned to the field "<WA_TEMP>", although this field is

entirely or partly protected against changes.

The following are protected against changes:

- Character literals or numeric literals

- Constants (CONSTANTS)

- Parameters of the category IMPORTING REFERENCE for functions and

methods

- Untyped field symbols not yet assigned a field using ASSIGN

- TABLES parameters if the actual parameter is protected against changes

- USING reference parameters and CHANGING parameters for FORMs, if the

actual parameter is protected against changes and

- Accesses using field symbols if the field assigned using ASSIGN is

protected (or partially protected, e.g. key components of an internal

table with the type SORTED or HASHED TABLE) against changes

- Accesses using references, if the field bound to the reference is

protected (or partially protected) against changes

- External write accesses to READ-ONLY attributes,

- Content of a shared object area instance accessed using a shared lock

(ATTACH_FOR_READ).

Since the table c_th_data is a hashed table. And this table is a dynamic structure, but I know for sure it has a field called '0FISCPER3'.

Thanks.

matt
Active Contributor
0 Kudos

Naimesh's solution will work for standard tables, but for other types, you need to be careful about what you're changing. Remember that with field-symbols, you are directly working with the data in memory. 0FISCPER is part of the key of your hashed table - <data> points to it, so you can't change it. You need another work area to make the change. This should work.


DATA: lp_data TYPE REF TO DATA.
CREATE DATA lp_data LIKE <wa_data>.
ASSIGN lp_data->* TO <wa_temp>.

<wa_temp> = <wa_data>.

ASSIGN COMPONENT '0FISCPER3' OF STRUCTURE <wa_temp> TO <data>.
IF <data> IS ASSIGNED.
  IF <data> IS INITIAL.
    <data> = v_per   
  ENDIF.
  COLLECT <wa_data> INTO c_th_data.
ENDIF.

matt