2008 Jun 21 2:58 PM
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.
*********************
2008 Jun 21 4:13 PM
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
2008 Jun 21 3:56 PM
use using instead of changing
like this...
FORM get_name USING <RECORD> type any.
2008 Jun 21 4:06 PM
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
2008 Jun 21 4:13 PM
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
2008 Jun 21 4:33 PM
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.
2008 Jun 21 4:55 PM
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.
2008 Jun 21 7:52 PM
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
2008 Jun 21 4:38 PM
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.