‎2007 Apr 02 2:33 PM
Hello,
Please give abap program making used of the BAPI function
to create purchase order creation using ME21N after importing the data from an external system.
Regards,
Sarath.
‎2007 Apr 02 2:38 PM
‎2007 Apr 02 2:38 PM
data : ITAB(100) occurs 0 with header line.
data : header like bapiekkoc OCCURS 0 WITH HEADER LINE.
data : items like bapiekpoc occurs 0 with header line.
data : schls like bapieket occurs 0 with header line.
data : ret like bapireturn occurs 0 with HEADER line.
data : po_num like bapiekkoc-PO_NUMBER.
CALL FUNCTION 'UPLOAD'
EXPORTING
FILENAME = 'C:\Documents and Settings\Administrator\Desktop\po.txt'
FILETYPE = 'ASC'
TABLES
DATA_TAB = ITAB .
LOOP AT ITAB.
IF ITAB+0(1) = 'H'.
IF SY-TABIX <> 1.
PERFORM CALL_BAPI.
ENDIF.
PERFORM FILL_HEADER.
ELSEIF ITAB+0(1) = 'I'.
PERFORM FILL_ITEMS.
ELSE.
PERFORM FILL_SCHLS.
ENDIF.
ENDLOOP.
PERFORM CALL_BAPI.
COMMIT WORK.
write : / 'po : ', po_num color 3.
loop at ret.
write 😕 ret.
endloop.
&----
*& Form CALL_BAPI
&----
FORM CALL_BAPI.
CALL FUNCTION 'BAPI_PO_CREATE'
EXPORTING
PO_HEADER = header
IMPORTING
PURCHASEORDER = po_num
TABLES
PO_ITEMS = items
PO_ITEM_SCHEDULES = schls
return = ret.
ENDFORM. " CALL_BAPI
&----
*& Form FILL_HEADER
&----
FORM FILL_HEADER.
HEADER-VENDOR = ITAB+1(10).
HEADER-PURCH_ORG = ITAB+11(4).
HEADER-PUR_GROUP = ITAB+15(3).
HEADER-SUPPL_PLNT = ITAB+18(4).
HEADER-DOC_TYPE = ITAB+22(2).
ENDFORM. " FILL_HEADER
&----
*& Form FILL_ITEMS
&----
FORM FILL_ITEMS.
ITEMS-MATERIAL = ITAB+1(18).
ITEMS-DISP_QUAN = ITAB+19(10).
ITEMS-NET_PRICE = ITAB+29(23).
APPEND ITEMS.
CLEAR ITEMS.
ENDFORM. " FILL_ITEMS
&----
*& Form FILL_SCHLS
&----
FORM FILL_SCHLS.
SCHLS-DELIV_DATE = ITAB+1(8).
SCHLS-QUANTITY = ITAB+9(13).
APPEND SCHLS.
CLEAR SCHLS.
ENDFORM. " FILL_SCHLS
reward if helpful...
‎2007 Apr 02 2:41 PM
Hi Naren
Check the below code.
report zmmb_po_upload.
data : begin of itab occurs 0,
collective_no(10) type c,
company_code(4) type c,
doc_type(4) type c,
vend_code(10) type c,
purch_org(4) type c,
purch_grp(3) type c,
currency(3) type c,
wo_date(10) type c,
line_item(5) type c,
itemtext(40) type c,
plant(4) type c,
matgroup(10) type c,
taxcode(2) type c,
itemcat(1) type c,
actasgntcat(1) type c,
deldate(10) type c,
glaccount(10) type c,
internalorder(12) type c,
network(12) type c,
activity(4) type c,
serlineitem(5) type c,
serviuceno(18) type c,
servicetext(40) type c,
quantity(17) type c,
unit(3) type c,
grossprice(17) type c,
end of itab.
*data : begin of itab occurs 0,
collective_no(10) type c,
doc_type(4) type c,
vend_code(10) type c,
purch_org(4) type c,
purch_grp(3) type c,
company_code(4) type c,
wo_date(10) type c,
line_item(5) type c,
actasgntcat(1) type c,
itemcat(1) type c,
itemtext(40) type c,
deldate(10) type c,
plant(4) type c,
matgroup(10) type c,
taxcode(2) type c,
serlineitem(5) type c,
serviuceno(18) type c,
servicedesc(40) type c,
quantity(17) type c,
unit(3) type c,
grossprice(17) type c,
glaccount(10) type c,
costcenter(10) type c,
internalorder(12) type c,
wbselement(24) type c,
conditiontype(4) type c,
amount(17) type c,
currency(3) type c,
end of itab.
data : xfile type string.
data : poheader like bapimepoheader,
poheaderx like bapimepoheaderx,
testrun like bapiflag-bapiflag value 'X',
poitem like bapimepoitem occurs 0 with header line,
poitemx like bapimepoitemx occurs 0 with header line,
poschedule like bapimeposchedule occurs 0 with header line,
poschedulex like bapimeposchedulx occurs 0 with header line,
poservices like bapiesllc occurs 0 with header line,
poaccount like bapimepoaccount occurs 0 with header line,
poaccountx like bapimepoaccountx occurs 0 with header line,
posrvact like bapiesklc occurs 0 with header line,
pocond like bapimepocondheader occurs 0 with header line,
pocondx like bapimepocondx occurs 0 with header line,
return like bapiret2 occurs 0 with header line,
po_number like bapimepoheader-po_number,
expheader like bapimepoheader,
wait like bapita-wait value 'X'.
parameter : fname like ibipparms-path obligatory.
----
at selection-screen on value-request for fname.
----
clear fname.
call function 'F4_FILENAME'
importing
file_name = fname
exceptions
others = 1.
----
at selection-screen.
----
if sy-ucomm = 'ONLI'.
xfile = fname.
clear itab. refresh itab.
call function 'GUI_UPLOAD'
exporting
filename = xfile
filetype = 'ASC'
has_field_separator = 'X'
tables
data_tab = itab.
if sy-subrc <> 0.
message e999(zm) with 'Error: Uploding file'.
endif.
endif.
----
start-of-selection.
----
data : wa_itab like itab.
data : counter(10) type n,
packno like ekpo-packno,
subpackno like bapiesllc-subpckg_no,
serial_no like bapimepoaccount-serial_no,
service_line(2) type n.
clear : poheader, poitem, poschedule, poservices, poaccount,
pocond, return, testrun, po_number, expheader, posrvact.
clear : poheaderx, poitemx, poschedulex, poaccountx, counter,
packno, subpackno, serial_no, service_line.
refresh : poitem, poschedule, poservices, poaccount, pocond,
return, posrvact.
refresh : poitemx, poschedulex, poaccountx.
sort itab by collective_no line_item serlineitem.
loop at itab.
clear wa_itab.
wa_itab = itab.
at new collective_no.
call function 'CONVERSION_EXIT_ALPHA_INPUT'
exporting
input = wa_itab-vend_code
importing
output = wa_itab-vend_code.
poheader-vendor = wa_itab-vend_code.
poheader-doc_type = wa_itab-doc_type.
poheader-purch_org = wa_itab-purch_org.
poheader-pur_group = wa_itab-purch_grp.
poheader-comp_code = wa_itab-company_code.
concatenate wa_itab-wo_date6(4) wa_itab-wo_date3(2)
wa_itab-wo_date+0(2) into wa_itab-wo_date.
poheader-doc_date = wa_itab-wo_date.
poheader-collect_no = wa_itab-collective_no.
poheader-currency = wa_itab-currency.
poheaderx-vendor = 'X'.
poheaderx-doc_type = 'X'.
poheaderx-purch_org = 'X'.
poheaderx-pur_group = 'X'.
poheaderx-comp_code = 'X'.
poheaderx-doc_date = 'X'.
poheaderx-collect_no = 'X'.
poheaderx-currency = 'X'.
endat.
at new line_item.
clear counter.
clear service_line.
counter = counter + 1.
if packno is initial.
select max( packno ) into packno from eslh.
packno = packno + 1.
subpackno = packno + 1.
else.
packno = subpackno + 1.
subpackno = packno + 1.
endif.
poitem-po_item = wa_itab-line_item.
poitem-acctasscat = wa_itab-actasgntcat.
poitem-item_cat = wa_itab-itemcat.
poitem-short_text = wa_itab-itemtext.
poitem-plant = wa_itab-plant.
poitem-matl_group = wa_itab-matgroup.
poitem-tax_code = wa_itab-taxcode.
poitem-quantity = 1.
poitem-pckg_no = packno.
append poitem. clear poitem.
poitemx-po_item = wa_itab-line_item.
poitemx-acctasscat = 'X'.
poitemx-item_cat = 'X'.
poitemx-short_text = 'X'.
poitemx-plant = 'X'.
poitemx-matl_group = 'X'.
poitemx-tax_code = 'X'.
poitemx-quantity = 'X'.
poitemx-pckg_no = 'X'.
append poitemx. clear poitemx.
poschedule-po_item = wa_itab-line_item.
poschedule-sched_line = 1.
poschedule-delivery_date = wa_itab-deldate.
poschedule-quantity = 1.
append poschedule. clear poschedule.
poschedulex-po_item = wa_itab-line_item.
poschedulex-sched_line = 1.
poschedulex-delivery_date = 'X'.
poschedulex-quantity = 'X'.
append poschedulex. clear poschedulex.
serial_no = serial_no + 1.
poaccount-po_item = wa_itab-line_item.
poaccount-serial_no = serial_no.
poaccount-serial_no = 1.
call function 'CONVERSION_EXIT_ALPHA_INPUT'
exporting
input = wa_itab-glaccount
importing
output = wa_itab-glaccount.
poaccount-gl_account = wa_itab-glaccount.
call function 'CONVERSION_EXIT_ALPHA_INPUT'
exporting
input = wa_itab-costcenter
importing
output = wa_itab-costcenter.
poaccount-costcenter = wa_itab-costcenter.
call function 'CONVERSION_EXIT_ALPHA_INPUT'
exporting
input = wa_itab-internalorder
importing
output = wa_itab-internalorder.
poaccount-orderid = wa_itab-internalorder.
call function 'CONVERSION_EXIT_ALPHA_INPUT'
exporting
input = wa_itab-network
importing
output = wa_itab-network.
poaccount-network = wa_itab-network.
call function 'CONVERSION_EXIT_NUMCV_INPUT'
exporting
input = wa_itab-activity
importing
output = wa_itab-activity.
poaccount-activity = wa_itab-activity.
append poaccount. clear poaccount.
poaccountx-po_item = wa_itab-line_item.
poaccountx-serial_no = serial_no.
poaccountx-gl_account = 'X'.
poaccountx-costcenter = 'X'.
poaccountx-orderid = 'X'.
poaccountx-network = 'X'.
poaccountx-activity = 'X'.
append poaccountx. clear poaccountx.
endat.
at new serlineitem.
if counter = 1.
poservices-pckg_no = packno.
poservices-line_no = counter.
poservices-subpckg_no = subpackno.
poservices-short_text = wa_itab-itemtext.
append poservices. clear poservices.
posrvact-pckg_no = packno.
posrvact-line_no = counter.
posrvact-percentage = 100.
posrvact-serial_no = serial_no.
posrvact-serial_no = 1.
append posrvact. clear posrvact.
endif.
counter = counter + 1.
service_line = service_line + 1.
poservices-pckg_no = subpackno.
poservices-line_no = counter.
poservices-ext_line = wa_itab-serlineitem.
if not wa_itab-serviuceno is initial.
call function 'CONVERSION_EXIT_ALPHA_INPUT'
exporting
input = wa_itab-serviuceno
importing
output = wa_itab-serviuceno.
endif.
poservices-service = wa_itab-serviuceno.
poservices-short_text = wa_itab-servicetext.
poservices-quantity = wa_itab-quantity.
poservices-base_uom = wa_itab-unit.
poservices-gr_price = wa_itab-grossprice.
append poservices. clear poservices.
posrvact-pckg_no = subpackno.
posrvact-line_no = counter.
posrvact-SERNO_LINE = service_line.
posrvact-SERNO_LINE = serial_no.
posrvact-percentage = 100.
posrvact-serial_no = serial_no.
posrvact-serial_no = 1.
append posrvact. clear posrvact.
endat.
at end of collective_no.
call function 'BAPI_PO_CREATE1'
exporting
poheader = poheader
poheaderx = poheaderx
importing
exppurchaseorder = po_number
expheader = expheader
tables
return = return
poitem = poitem
poitemx = poitemx
poschedule = poschedule
poschedulex = poschedulex
poaccount = poaccount
poaccountx = poaccountx
poservices = poservices
posrvaccessvalues = posrvact.
if not po_number is initial.
call function 'BAPI_TRANSACTION_COMMIT'
exporting
wait = wait.
endif.
write 😕 wa_itab.
write 😕 sy-uline.
loop at return.
write 😕 return-message.
endloop.
write 😕 sy-uline.
clear : poheader, poitem, poschedule, poservices, poaccount,
pocond, return, testrun, po_number, expheader,
posrvact.
clear : poheaderx, poitemx, poschedulex, poaccountx, counter,
packno, subpackno, serial_no, service_line.
refresh : poitem, poschedule, poservices, poaccount, pocond,
return, posrvact.
refresh : poitemx, poschedulex, poaccountx.
endat.
endloop.
----
end-of-selection.
----
Hope this helps !!!
Britto