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

Create a new PO using a bdc session and adding multiple line items

Former Member
0 Likes
1,212

I need to create a new PO using ME 21 flat file thru a bdc.This program needs to create a new po and a number of line items (this is done thru table control i understand).Can some one guide me in this .

Thanks in advance

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,130

I have used BDC for ME22 Transaction and moreever it is similar ,do the recording and see the below logic.

report zpochange.

data : i_error like bdcmsgcoll occurs 0 with header line.

data : i_bdcdata like bdcdata occurs 0 with header line.

tables : ekko, ekpo.

data :c1(10) value 'ME22',

c2(1) value ',',c3(10).

data : var1(20). " LIKE EKKO-EBELN.

data : var2 like ekko-ebeln.

data : begin of i_ekko occurs 0,

header(2),

ebeln like ekko-ebeln,

end of i_ekko.

data : begin of i_ekpo occurs 0,

item(2),

ebeln like ekpo-ebeln,

ebelp like ekpo-ebelp,

menge(10), " LIKE EKPO-MENGE,

end of i_ekpo.

data : v like ekpo-ebelp.

data: begin of itab occurs 0,

text(300),

end of itab.

parameters: p_file like ibipparms-path.

*PARAMETERS: PONUMBER LIKE EKPO-EBELN.

at selection-screen on value-request for p_file.

call function 'F4_FILENAME'

exporting

program_name = syst-cprog

dynpro_number = syst-dynnr

  • FIELD_NAME = ' '

importing

file_name = p_file

.

start-of-selection.

*SELECT EBELN FROM EKKO INTO TABLE I_EKKO WHERE EBELN = PONUMBER.

*

  • SELECT EBELN EBELP MENGE FROM EKPO INTO TABLE I_EKPO WHERE EBELN

*= PONUMBER.

*

perform get_data.

*LOOP AT ITAB.

  • WRITE 😕 ITAB.

  • ENDLOOP.

loop at itab.

var1 = itab-text+0(1).

if var1 = 'H'.

*I_EKKO-EBELN = ITAB-TEXT.

split itab at c2 into i_ekko-header

i_ekko-ebeln.

  • SPLIT ITAB AT ',' INTO I_EKKO-EBELN.

    • " I_EKPO-EBELP

    • " I_EKPO-MENGE

    • " var1.

append i_ekko.

else.

split itab at c2 into i_ekpo-item

i_ekpo-ebeln

i_ekpo-ebelp

i_ekpo-menge.

append i_ekpo.

endif.

*var2 = i_ekpo-ebeln.

  • MOVE VAR1 TO I_EKPO-EBELN.

  • MOVE VAR1 TO I_EKKO-EBELN.

  • APPEND: I_EKPO.

*if not var1 is initial.

  • split var1 at ',' into i_ekpo-ebelp

  • i_ekpo-menge.

  • i_ekpo-ebeln = var2.

  • append i_ekpo.

*endif.

endloop.

loop at i_ekpo.

write 😕 i_ekpo.

endloop.

*

loop at i_ekko.

perform fill_data. " TABLES I_EKPO.

endloop.

  • LOOP AT I_EKPO.

  • WRITE 😕 I_EKPO.

  • ENDLOOP.

*&----


*& Form GET_DATA

*&----


  • text

*----


  • --> p1 text

  • <-- p2 text

*----


*LOOP AT I_ERROR.

  • WRITE 😕 I_ERROR.

  • ENDLOOP.

*

form get_data.

call function 'WS_UPLOAD'

exporting

codepage = ' '

filename = p_file

filetype = 'ASC'

headlen = ' '

line_exit = ' '

trunclen = ' '

user_form = ' '

user_prog = ' '

dat_d_format = ' '

  • IMPORTING

  • FILELENGTH =

tables

data_tab = itab

exceptions

conversion_error = 1

file_open_error = 2

file_read_error = 3

invalid_type = 4

no_batch = 5

unknown_error = 6

invalid_table_width = 7

gui_refuse_filetransfer = 8

customer_error = 9

others = 10

.

if sy-subrc <> 0.

message id sy-msgid type sy-msgty number sy-msgno

with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

endif.

endform. " GET_DATA

*&----


*& Form FILL_DATA

*&----


  • text

*----


  • -->P_I_EKPO text

*----


form fill_data. " tables i_ekpo .

perform bdcscreen using 'SAPMM06E' '0105'.

perform bdcfield using 'RM06E-BSTNR' i_ekko-ebeln.

perform bdcfield using 'BDC_OKCODE' '/00'.

loop at i_ekpo where ebeln = i_ekko-ebeln.

  • V = I_EKPO-EBELP.

perform bdcscreen using 'SAPMM06E' 0120.

perform bdcfield using 'BDC_CURSOR' 'RM06E-EBELP'.

perform bdcfield using 'RM06E-EBELP' i_ekpo-ebelp.

perform bdcfield using 'BDC_OKCODE' '/00'.

perform bdcscreen using 'SAPMM06E' 0120.

perform bdcfield using 'BDC_CURSOR' 'EKPO-MENGE(01)'.

perform bdcfield using 'RM06E-EBELP' i_ekpo-ebelp.

perform bdcfield using 'EKPO-MENGE(01)' i_ekpo-menge.

perform bdcfield using 'BDC_OKCODE' '/00'.

*PERFORM BDCSCREEN USING 'SAPMM06E' 0120.

*

*PERFORM BDCFIELD USING 'BDC_CURSOR' 'RMO6E-EBELP'.

*

*CLEAR V.

endloop.

perform bdcfield using 'BDC_OKCODE' '=BU'.

call transaction c1 using i_bdcdata mode 'A'

messages into i_error.

refresh i_bdcdata.

endform. " FILL_DATA

*&----


*& Form BDCSCREEN

*&----


  • text

*----


  • -->P_0140 text

  • -->P_0120 text

*----


form bdcscreen using p_program p_screen.

i_bdcdata-program = p_program.

i_bdcdata-dynpro = p_screen.

i_bdcdata-dynbegin = 'X'.

append i_bdcdata.

clear i_bdcdata.

endform. " BDCSCREEN

*&----


*& Form BDCFIELD

*&----


  • text

*----


  • -->P_0145 text

  • -->P_I_EKPO_EBELN text

*----


form bdcfield using fnam fval.

i_bdcdata-fnam = fnam.

i_bdcdata-fval = fval.

append i_bdcdata.

clear i_bdcdata.

endform. " BDCFIELD

Thanks

seshu

8 REPLIES 8
Read only

Former Member
0 Likes
1,130

Hi,

PO can be created from t code me21n it is an enjoy transaction .

Hence BAPI is prefered to BDC.

use BAPI_PO_CREATE1.

check this Sample code,

REPORT zpo_create .

PARAMETERS: p-file LIKE rlgrap-filename DEFAULT

'c:\demo3.txt'.

DATA: BEGIN OF itab OCCURS 0,

line(1000),

END OF itab.

DATA l_item(5) TYPE n.

TABLES : zerror.

DATA lcounter(3) TYPE n.

DATA : l_errflag.

DATA : l_po_header LIKE bapimepoheader.

DATA : l_po_headerx LIKE bapimepoheaderx.

DATA : t_po_account LIKE bapimepoaccount OCCURS 0 WITH HEADER LINE.

DATA : t_po_accountx LIKE bapimepoaccountx OCCURS 0 WITH HEADER LINE.

DATA : l_new_po(10), l_last_po(10).

DATA : l_purchaseorder LIKE bapimepoheader-po_number.

*DATA : L_HEADER LIKE BAPIMEPOHEADER STRUCTURE BAPIMEPOHEADER.

DATA : BEGIN OF t_po_items OCCURS 0.

INCLUDE STRUCTURE bapimepoitem.

DATA : END OF t_po_items.

DATA : BEGIN OF t_po_itemsx OCCURS 0.

INCLUDE STRUCTURE bapimepoitemx.

DATA : END OF t_po_itemsx.

DATA : BEGIN OF t_return OCCURS 0.

INCLUDE STRUCTURE bapiret2.

DATA : END OF t_return.

CLEAR : l_po_header.

CALL FUNCTION 'WS_UPLOAD'

EXPORTING

filename = p-file

filetype = 'DAT'

TABLES

data_tab = itab

EXCEPTIONS

file_open_error = 1

file_read_error = 2

no_batch = 3

gui_refuse_filetransfer = 4

invalid_type = 5

OTHERS = 6.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

EXIT.

ENDIF.

LOOP AT itab.

IF itab-line(1) EQ 'K'.

l_new_po = itab-line(10).

IF l_last_po NE l_new_po AND l_last_po NE space.

PERFORM create_po.

PERFORM refresh_table.

ENDIF.

IF itab-line+13(2) EQ '40'.

PERFORM append_header.

ENDIF.

IF itab-line+13(2) EQ '81'.

PERFORM append_item.

ENDIF.

l_last_po = l_new_po.

ENDIF.

ENDLOOP.

IF sy-subrc EQ 0.

PERFORM create_po.

PERFORM refresh_table.

ENDIF.

&----


*& Form CREATE_PO

&----


  • PO Create

----


  • --> p1 text

  • <-- p2 text

----


FORM create_po .

CLEAR : l_purchaseorder.

CALL FUNCTION 'BAPI_PO_CREATE1'

EXPORTING

poheader = l_po_header

poheaderx = l_po_headerx

  • POADDRVENDOR =

  • TESTRUN =

  • MEMORY_UNCOMPLETE =

  • MEMORY_COMPLETE =

  • POEXPIMPHEADER =

  • POEXPIMPHEADERX =

  • VERSIONS =

  • NO_MESSAGING =

  • NO_MESSAGE_REQ =

  • NO_AUTHORITY =

  • NO_PRICE_FROM_PO =

IMPORTING

exppurchaseorder = l_purchaseorder

  • EXPHEADER =

  • EXPPOEXPIMPHEADER =

TABLES

return = t_return

poitem = t_po_items

poitemx = t_po_itemsx

  • POADDRDELIVERY =

  • POSCHEDULE =

  • POSCHEDULEX =

poaccount = t_po_account

  • POACCOUNTPROFITSEGMENT =

poaccountx = t_po_accountx

  • POCONDHEADER =

  • POCONDHEADERX =

  • POCOND =

  • POCONDX =

  • POLIMITS =

  • POCONTRACTLIMITS =

  • POSERVICES =

  • POSRVACCESSVALUES =

  • POSERVICESTEXT =

  • EXTENSIONIN =

  • EXTENSIONOUT =

  • POEXPIMPITEM =

  • POEXPIMPITEMX =

  • POTEXTHEADER =

  • POTEXTITEM =

  • ALLVERSIONS =

  • POPARTNER =

.

l_errflag = space.

lcounter = 1.

LOOP AT t_return .

IF t_return-type = 'E'.

l_errflag = 'X'.

GET TIME.

PERFORM append_error.

ENDIF.

WRITE : /1 t_return-message.

ENDLOOP.

IF l_errflag EQ space.

CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

  • EXPORTING

  • WAIT =

  • IMPORTING

  • RETURN =

.

ENDIF.

ENDFORM. " CREATE_PO

&----


*& Form APPEND_HEADER

&----


  • PO Header print

----


  • --> p1 text

  • <-- p2 text

----


FORM append_header .

l_po_header-comp_code = '1000'." Company Code

l_po_header-doc_type = 'NB'. " Order Typr BSART

l_po_header-creat_date = sy-datum. " P.O Dt - BEDAT

*CONCATENATE '0000' ITAB+72(6) INTO L_PO_HEADER-VENDOR .

l_po_header-vendor = itab-line+71(6).

*L_PO_HEADER-VENDOR = ITAB+72(6) " 'ABC'. "Vendor - EKKO-LIFNR

IF itab-line(4) NE 'K034'.

l_po_header-purch_org = '2000'. " Purch. Org - EKKO-EKORG

l_po_header-pur_group = 'F01'. " Purch Group - EKKO-EKGRP

ELSE.

l_po_header-purch_org = '1000'. " Purch. Org - EKKO-EKORG

l_po_header-pur_group = '001'. " Purch Group - EKKO-EKGRP

ENDIF.

l_po_header-currency_iso = 'USD'. " Currency Default field for Enjoy

l_po_header-ref_1 = itab-line+1(9).

*L_PO_HEADER-DOC_CAT = 'F'. " Account Assign Cat EKPO-KNTTP

*_PO_HEADER-CREATED_BY = SY-UNAME.

CLEAR : l_po_headerx.

l_po_headerx-po_number = 'X'.

l_po_headerx-comp_code = 'X'." Company Code

l_po_headerx-doc_type = 'X'. " Order Typr BSART

l_po_headerx-vendor = 'X'. "Vendor - EKKO-LIFNR

l_po_headerx-purch_org = 'X'. " Purch. Org - EKKO-EKORG

l_po_headerx-pur_group = 'X'. " Purch Group - EKKO-EKGRP

l_po_headerx-currency_iso = 'X'. " Currency Default field for Enjoy

l_po_headerx-ref_1 = 'X'.

ENDFORM. " APPEND_HEADER

&----


*& Form APPEND_ITEM

&----


  • Material Number and Item print

----


  • --> p1 text

  • <-- p2 text

----


FORM append_item .

  • PO Line Items

l_item = l_item + 10.

t_po_items-po_item = l_item.

*T_PO_ITEMS-PO_ITEM = '00010'." Line Item No - EKPO-EBELP

*T_PO_ITEMS-SHORT_TEXT = 'KSR TEXT'." Line Item TEXT

CONCATENATE '00000000' itab-line+32(10) INTO t_po_items-material.

*T_PO_ITEMS-MATERIAL = '000000000000000043'. " Material No - EKPO-EMATN

CONCATENATE '00000000' itab-line+32(10) INTO t_po_items-ematerial.

t_po_items-plant = '2001'. " Plant - EKPO-WERKS

t_po_items-quantity = '1.000'.

t_po_items-net_price = '10.00'. " EKPO-NETPR

t_po_items-price_unit = '10 '.

*T_PO_ITEMS-TAX_CODE = 'A2'.

IF itab-line+0(4) EQ 'K034'.

t_po_items-acctasscat = 'R'.

t_po_items-plant = '1001'.

t_po_account-po_item = l_item.

t_po_account-profit_ctr = 'BR034'.

t_po_account-gl_account = '0000135075'.

  • T_PO_ACCOUNT-CO_AREA = '1000'.

APPEND t_po_account.

CLEAR t_po_account.

t_po_accountx-po_item = l_item.

t_po_accountx-profit_ctr = 'X'.

t_po_accountx-gl_account = 'X'.

  • T_PO_ACCOUNTX-CO_AREA = 'X'.

APPEND t_po_accountx.

CLEAR t_po_accountx.

ENDIF.

APPEND t_po_items.

CLEAR t_po_items.

t_po_itemsx-po_item = l_item.

t_po_itemsx-po_itemx = 'X'.

t_po_itemsx-acctasscat = 'X'.

t_po_itemsx-material = 'X'.

t_po_itemsx-ematerial = 'X'.

t_po_itemsx-plant = 'X'.

t_po_itemsx-quantity = 'X'.

t_po_itemsx-net_price = 'X'.

t_po_itemsx-price_unit = 'X'.

*T_PO_ITEMSX-TAX_CODE = 'X'.

APPEND t_po_itemsx.

CLEAR t_po_itemsx.

      • Second Line Item

ENDFORM. " APPEND_ITEM

&----


*& Form REFRESH_TABLE

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM refresh_table .

REFRESH : t_po_items, t_po_itemsx.

CLEAR l_item.

ENDFORM. " REFRESH_TABLE

&----


*& Form append_error

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM append_error .

MOVE : sy-datum TO zerror-zdate,

sy-uzeit TO zerror-ztime,

itab+3(5) TO zerror-document_no,

t_return-message TO zerror-message.

lcounter = lcounter + 1.

zerror-counter = lcounter .

zerror-rec_type = itab-line(1).

MODIFY zerror. CLEAR zerror.

COMMIT WORK.

ENDFORM. " append_error

Reward points if useful,

Aleem.

Read only

0 Likes
1,130

hi aleem mohiuddin,

thanks for the sample program that u have given for me21n bapi_po_create1. can u pls do me a favour by sending the file format on which ur logic is working. it's very urgent. i'll really be very grateful to u.

ritu

Read only

0 Likes
1,130

Hi Aleem,

Thanks for the sample program that u have given for me21n bapi_po_create1. can you pls do me a favour by sending the file format on which ur logic is working.

Read only

Former Member
0 Likes
1,130

Hi Anjali,

You may be thinking you asked for bdc and i gave bapi.

Actually

1. ME21N

is an ENJOY transaction

it means that it uses

complex controls on screen,

(which require classes and other things).

2. due to this,

BDC is not fully possible in this tcode.

3. Hence,

SAP suggests to use BAPI

for putting data in the transaction !

Reward points if useful,

Aleem.

Read only

0 Likes
1,130

hi aleem,

i've got ur program code. what i need is the way u have maintined ur file for that. like in which order header n line items are coming. hope u got my question. pls answer at ur earliest.

Read only

Former Member
0 Likes
1,130

Thanks a lot for that .I realise that a BAPI would be more helpful but the func guys here want it to be a BDC with transaction me21 cause they are used to using BDC's.

i will keep that as a reference and use it when needed .

Thanks again

anjali

Read only

Former Member
0 Likes
1,131

I have used BDC for ME22 Transaction and moreever it is similar ,do the recording and see the below logic.

report zpochange.

data : i_error like bdcmsgcoll occurs 0 with header line.

data : i_bdcdata like bdcdata occurs 0 with header line.

tables : ekko, ekpo.

data :c1(10) value 'ME22',

c2(1) value ',',c3(10).

data : var1(20). " LIKE EKKO-EBELN.

data : var2 like ekko-ebeln.

data : begin of i_ekko occurs 0,

header(2),

ebeln like ekko-ebeln,

end of i_ekko.

data : begin of i_ekpo occurs 0,

item(2),

ebeln like ekpo-ebeln,

ebelp like ekpo-ebelp,

menge(10), " LIKE EKPO-MENGE,

end of i_ekpo.

data : v like ekpo-ebelp.

data: begin of itab occurs 0,

text(300),

end of itab.

parameters: p_file like ibipparms-path.

*PARAMETERS: PONUMBER LIKE EKPO-EBELN.

at selection-screen on value-request for p_file.

call function 'F4_FILENAME'

exporting

program_name = syst-cprog

dynpro_number = syst-dynnr

  • FIELD_NAME = ' '

importing

file_name = p_file

.

start-of-selection.

*SELECT EBELN FROM EKKO INTO TABLE I_EKKO WHERE EBELN = PONUMBER.

*

  • SELECT EBELN EBELP MENGE FROM EKPO INTO TABLE I_EKPO WHERE EBELN

*= PONUMBER.

*

perform get_data.

*LOOP AT ITAB.

  • WRITE 😕 ITAB.

  • ENDLOOP.

loop at itab.

var1 = itab-text+0(1).

if var1 = 'H'.

*I_EKKO-EBELN = ITAB-TEXT.

split itab at c2 into i_ekko-header

i_ekko-ebeln.

  • SPLIT ITAB AT ',' INTO I_EKKO-EBELN.

    • " I_EKPO-EBELP

    • " I_EKPO-MENGE

    • " var1.

append i_ekko.

else.

split itab at c2 into i_ekpo-item

i_ekpo-ebeln

i_ekpo-ebelp

i_ekpo-menge.

append i_ekpo.

endif.

*var2 = i_ekpo-ebeln.

  • MOVE VAR1 TO I_EKPO-EBELN.

  • MOVE VAR1 TO I_EKKO-EBELN.

  • APPEND: I_EKPO.

*if not var1 is initial.

  • split var1 at ',' into i_ekpo-ebelp

  • i_ekpo-menge.

  • i_ekpo-ebeln = var2.

  • append i_ekpo.

*endif.

endloop.

loop at i_ekpo.

write 😕 i_ekpo.

endloop.

*

loop at i_ekko.

perform fill_data. " TABLES I_EKPO.

endloop.

  • LOOP AT I_EKPO.

  • WRITE 😕 I_EKPO.

  • ENDLOOP.

*&----


*& Form GET_DATA

*&----


  • text

*----


  • --> p1 text

  • <-- p2 text

*----


*LOOP AT I_ERROR.

  • WRITE 😕 I_ERROR.

  • ENDLOOP.

*

form get_data.

call function 'WS_UPLOAD'

exporting

codepage = ' '

filename = p_file

filetype = 'ASC'

headlen = ' '

line_exit = ' '

trunclen = ' '

user_form = ' '

user_prog = ' '

dat_d_format = ' '

  • IMPORTING

  • FILELENGTH =

tables

data_tab = itab

exceptions

conversion_error = 1

file_open_error = 2

file_read_error = 3

invalid_type = 4

no_batch = 5

unknown_error = 6

invalid_table_width = 7

gui_refuse_filetransfer = 8

customer_error = 9

others = 10

.

if sy-subrc <> 0.

message id sy-msgid type sy-msgty number sy-msgno

with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

endif.

endform. " GET_DATA

*&----


*& Form FILL_DATA

*&----


  • text

*----


  • -->P_I_EKPO text

*----


form fill_data. " tables i_ekpo .

perform bdcscreen using 'SAPMM06E' '0105'.

perform bdcfield using 'RM06E-BSTNR' i_ekko-ebeln.

perform bdcfield using 'BDC_OKCODE' '/00'.

loop at i_ekpo where ebeln = i_ekko-ebeln.

  • V = I_EKPO-EBELP.

perform bdcscreen using 'SAPMM06E' 0120.

perform bdcfield using 'BDC_CURSOR' 'RM06E-EBELP'.

perform bdcfield using 'RM06E-EBELP' i_ekpo-ebelp.

perform bdcfield using 'BDC_OKCODE' '/00'.

perform bdcscreen using 'SAPMM06E' 0120.

perform bdcfield using 'BDC_CURSOR' 'EKPO-MENGE(01)'.

perform bdcfield using 'RM06E-EBELP' i_ekpo-ebelp.

perform bdcfield using 'EKPO-MENGE(01)' i_ekpo-menge.

perform bdcfield using 'BDC_OKCODE' '/00'.

*PERFORM BDCSCREEN USING 'SAPMM06E' 0120.

*

*PERFORM BDCFIELD USING 'BDC_CURSOR' 'RMO6E-EBELP'.

*

*CLEAR V.

endloop.

perform bdcfield using 'BDC_OKCODE' '=BU'.

call transaction c1 using i_bdcdata mode 'A'

messages into i_error.

refresh i_bdcdata.

endform. " FILL_DATA

*&----


*& Form BDCSCREEN

*&----


  • text

*----


  • -->P_0140 text

  • -->P_0120 text

*----


form bdcscreen using p_program p_screen.

i_bdcdata-program = p_program.

i_bdcdata-dynpro = p_screen.

i_bdcdata-dynbegin = 'X'.

append i_bdcdata.

clear i_bdcdata.

endform. " BDCSCREEN

*&----


*& Form BDCFIELD

*&----


  • text

*----


  • -->P_0145 text

  • -->P_I_EKPO_EBELN text

*----


form bdcfield using fnam fval.

i_bdcdata-fnam = fnam.

i_bdcdata-fval = fval.

append i_bdcdata.

clear i_bdcdata.

endform. " BDCFIELD

Thanks

seshu

Read only

Former Member
0 Likes
1,130

Thanks a lot for that .I will try it with me21 and let you know if i get stuck .

I think it answers my question but i will just keep the thread open for a few more days so that i can post anything else if i have an issue with me21.

Thanks