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: 

FIELD-SYMBOLS

Former Member
0 Kudos
193

Hi Experts,

Please modify the below code accordingly to work properly. Solution is surely rewarded....... Thank you.

For example, I have a requiremet like to get NAME1 of either KUNNR or LIFNR form KNA1 or LFA1. I have to get the name of vendor or customer from this GET_NAME. Please give me the exact code for this.


*********************
DATA: BEGIN OF rec_kna1,
        kunnr   TYPE kna1-kunnr,
        name1   TYPE kna1-name1,
      END OF rec_kna1,
      BEGIN OF rec_lfa1,
        lifnr   TYPE lfa1-lifnr,
        name1   TYPE lfa1-name1,
      END OF rec_lfa1.

PERFORM get_name CHANGING <rec_kna1 / rec_lfa1>.
*********************
FORM get_name    CHANGING <RECORD> type any.
  SELECT SINGLE
         <KUNNR/LIFNR>
         NAME1
    FROM <KNA1/LFA1>
    INTO RECORD.
ENDFORM.
*********************

1 ACCEPTED SOLUTION

Clemenss
Active Contributor
0 Kudos
133

Hi Priyanka,

it looks as if you want to create a generic FORM that can decide from where it should select depending on the input parameter.

It may be easier to use two FORMs, get_name_kna1 and get_name_lfa1, but still you may do like this:


*********************
DATA: BEGIN OF rec_kna1,
        kunnr   TYPE kna1-kunnr,
        name1 TYPE kna1-name1,
      END OF rec_kna1,
      BEGIN OF rec_lfa1,
        lifnr   TYPE lfa1-lifnr,
        name1   TYPE lfa1-name1,
      END OF rec_lfa1.
 
PERFORM get_name CHANGING <rec_kna1 / rec_lfa1>.
*********************
FORM get_name    CHANGING <RECORD> type any.
  DATA:
    lv_tabname   TYPE dd03l-tabname value 'KNA1',
    lv_fieldname TYPE dd03l-fieldname value 'KUNNR.,

  FIELD-SYMBOLS:
    <any> TYPE any.
  ASSIGN COMPONENT lv_fieldname OF STRUCTURE <RECORD> TO <any>.
  IF sy-subrc NE 0.
    lv_tabname = 'LFA1'.
    lv_fieldname = 'LIFNR'
    ASSIGN COMPONENT lv_fieldname OF STRUCTURE <RECORD> to <any>.
  ENDIF.
  SELECT SINGLE
         (lv_fieldname)
         NAME1
    INTO <RECORD>
    FROM (lv_tabname)
    WHERE (lv_fieldname) = <any>.
ENDFORM.
*********************

Regards,

Clemens

7 REPLIES 7

Former Member
0 Kudos
133

use using instead of changing

like this...

FORM get_name USING <RECORD> type any.

0 Kudos
133

Thanks for silly answer venkata, I am not asking about Usings and Changing. I am asking for how to work with field-symbols is the given scenario. I hope you can give answer. Could you please........

Edited by: Priyanka Chowdry on Jun 21, 2008 5:14 PM

Clemenss
Active Contributor
0 Kudos
134

Hi Priyanka,

it looks as if you want to create a generic FORM that can decide from where it should select depending on the input parameter.

It may be easier to use two FORMs, get_name_kna1 and get_name_lfa1, but still you may do like this:


*********************
DATA: BEGIN OF rec_kna1,
        kunnr   TYPE kna1-kunnr,
        name1 TYPE kna1-name1,
      END OF rec_kna1,
      BEGIN OF rec_lfa1,
        lifnr   TYPE lfa1-lifnr,
        name1   TYPE lfa1-name1,
      END OF rec_lfa1.
 
PERFORM get_name CHANGING <rec_kna1 / rec_lfa1>.
*********************
FORM get_name    CHANGING <RECORD> type any.
  DATA:
    lv_tabname   TYPE dd03l-tabname value 'KNA1',
    lv_fieldname TYPE dd03l-fieldname value 'KUNNR.,

  FIELD-SYMBOLS:
    <any> TYPE any.
  ASSIGN COMPONENT lv_fieldname OF STRUCTURE <RECORD> TO <any>.
  IF sy-subrc NE 0.
    lv_tabname = 'LFA1'.
    lv_fieldname = 'LIFNR'
    ASSIGN COMPONENT lv_fieldname OF STRUCTURE <RECORD> to <any>.
  ENDIF.
  SELECT SINGLE
         (lv_fieldname)
         NAME1
    INTO <RECORD>
    FROM (lv_tabname)
    WHERE (lv_fieldname) = <any>.
ENDFORM.
*********************

Regards,

Clemens

Former Member
0 Kudos
133

Thanks Clemens Li,

in the Code you are populating 'LFA1' and 'LIFNR'. This subroutine Can be called dynamically. I do not really know whether I have to retrieve KUNNR from KNA1 or LIFNR from LFA1. It has to be decided by the changing parameter structure.

Former Member
0 Kudos
133

Thanks Clemens Li,

I got the answer from your code. Thanks a lot.


PERFORM get_name CHANGING rec_lfa1.
 WRITE: / 'LIFNR:',rec_lfa1-lifnr, '- NAME1:', rec_lfa1-name1.
PERFORM get_name CHANGING rec_kna1.
 WRITE: / 'KUNNR:', rec_kna1-kunnr, '- NAME1:', rec_kna1-name1.
********************* 
FORM get_name    CHANGING RECORD type any.
  DATA:
    lv_tabname type dd03l-tabname value 'LFA1',
    lv_fieldname type dd03l-fieldname value 'LIFNR',
    lv_fields  type char100 value 'LIFNR NAME1'.
  FIELD-SYMBOLS:
    <any> TYPE any.
  ASSIGN COMPONENT lv_fieldname OF STRUCTURE RECORD TO <any>.
  IF sy-subrc <> 0.
    lv_tabname = 'KNA1'.
    REPLACE 'LIFNR' with 'KUNNR' INTO lv_fields.
  ENDIF.

  SELECT SINGLE
         (lv_fields)
    INTO RECORD
    FROM (lv_tabname).
ENDFORM.

Clemenss
Active Contributor
0 Kudos
133

Hi Priyanka,

sorry I did not explain further in my original answer.

My plan was:

- Initialize table as LFA1 and field as LIFNR in data declaration.

- Try to assign field LIFNR in the structure passed as parameter. If this is successful, we know that the structure has a component named LIFNR. If not, it must be KUNNR and the table must be KNA1.

Then select the field LIFNR or KUNNR and the field NAME1 from the respective table.

I did not try if a mixture of dynamically suppied fields (lv_field) and static field NAME1 works with SELECT - obviously not. So your slight change is good. Interesting that it works this way. According to documentation the parentheses must include the name of an internal table itab that is either empty or contains s1 s2 u2026 ... to specify the columns. This way I learned something too.

I should have written


data:
...
  lt_fieldname TYPE TABLE OF dd03l-fieldname.
...
ASSIGN ...
IF SY_SUBRC = 0.
  APPEND 'LIFNR' to lt_fields.
ELSE.
  APPEND 'KUNNR' to lt_fields.
ENDIF.
APPEND 'NAME1' to lt_fields.
SELECT (lt_fields) INTO ...

This is correct according to syntax and will work in any releases.

Regards,

Clemens

Former Member
0 Kudos
133

FORM get_name CHANGING <RECORD> type any.

SELECT SINGLE

<KUNNR/LIFNR>

NAME1

FROM KNA1

INTO RECORD.

if sy-subrc <> 0.

SELECT SINGLE

<KUNNR/LIFNR>

NAME1

FROM LFA1

INTO RECORD.

endif.

ENDFORM.