‎2008 Jul 07 6:53 AM
hi,
how to upload the master data from flat file to sap system
using bapi's.please give me step by step.
‎2008 Jul 07 6:57 AM
Hi,
Go through this
REPORT zmmr_po_upload_mat NO STANDARD PAGE HEADING.
***********************************************************************
*& DATA DECLARATION
************************************************************************
DATA : BEGIN OF gt_header OCCURS 0,
vend_code(10) TYPE c,
ponum(10) TYPE c,
po_date(10) TYPE c,
purch_org(4) TYPE c,
purch_grp(3) TYPE c,
pmnttrms(4) TYPE c,
incoterms1(3) TYPE c,
incoterms2(28) TYPE c,
currency(3) TYPE c,
your_ref(12) TYPE c, "IHREZ
our_ref(12) TYPE c, "UNSEZ
END OF gt_header.
DATA : BEGIN OF gt_item OCCURS 0,
ponum(10) TYPE c,
line_item(5) TYPE c,
acctasscat(1) TYPE c,
item_cat(1) TYPE c,"PSTYP
plant(4) TYPE c,
sloc(4) TYPE c,
material(18) TYPE c,
stext(40) TYPE c,
po_qty(17) TYPE c,
po_unit(3) TYPE c,
net_price(13) TYPE c,
tax_code(2) TYPE c,
delivery_date(10) TYPE c,
requester(12) TYPE c,
costcenter(10) TYPE c,
wbselement(24) TYPE c,
network_no(12) TYPE c,"NPLNR
activity(4) TYPE c,"VORNR
order_no(12) TYPE c,"aufnr
loc_vendor(10) TYPE c,"LIFNR
loc_customer(10) TYPE c,"KUNNR
END OF gt_item.
DATA : BEGIN OF gt_price OCCURS 0,
ponum(10) TYPE c,
line_item(5) TYPE c,
cond_type(4) TYPE c,
cond_val(28) TYPE c,
rate_unit(5) TYPE c,
cond_unit(3) TYPE c,
numerator(13) TYPE c,
denominator(13) TYPE c,
del_cost_vndr(10) TYPE c,
END OF gt_price.
DATA : poheader LIKE bapimepoheader,
poheaderx LIKE bapimepoheaderx,
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,
poaccount LIKE bapimepoaccount OCCURS 0 WITH HEADER LINE,
poaccountx LIKE bapimepoaccountx OCCURS 0 WITH HEADER LINE,
pocond LIKE bapimepocondheader OCCURS 0 WITH HEADER LINE,
pocondx LIKE bapimepocondx OCCURS 0 WITH HEADER LINE,
poservice LIKE bapiesllc OCCURS 0 WITH HEADER LINE,
poheadertext LIKE bapimepotextheader OCCURS 0 WITH HEADER LINE,
return LIKE bapiret2 OCCURS 0 WITH HEADER LINE,
po_number LIKE bapimepoheader-po_number, "#EC NEEDED
expheader LIKE bapimepoheader, "#EC NEEDED
wait LIKE bapita-wait VALUE 'X'.
************************************************************************
*& SELECTION-SCREEN DECLARATION
************************************************************************
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETER : p_bukrs TYPE ekko-bukrs DEFAULT '1000',
p_doc TYPE ekko-bsart DEFAULT 'ZDOM',
fhead LIKE ibipparms-path OBLIGATORY,
fitem LIKE ibipparms-path OBLIGATORY,
fserv LIKE ibipparms-path OBLIGATORY,
fprice LIKE ibipparms-path.
SELECTION-SCREEN SKIP 1.
SELECTION-SCREEN END OF BLOCK b1.
----
AT SELECTION-SCREEN ON VALUE-REQUEST FOR fhead.
----
CLEAR fhead.
CALL FUNCTION 'F4_FILENAME'
IMPORTING
file_name = fhead
EXCEPTIONS "#EC *
OTHERS = 1.
----
AT SELECTION-SCREEN ON VALUE-REQUEST FOR fitem.
----
CLEAR fitem.
CALL FUNCTION 'F4_FILENAME'
IMPORTING
file_name = fitem
EXCEPTIONS "#EC *
OTHERS = 1.
----
AT SELECTION-SCREEN ON VALUE-REQUEST FOR fprice.
----
CLEAR fprice.
CALL FUNCTION 'F4_FILENAME'
IMPORTING
file_name = fprice
EXCEPTIONS "#EC *
OTHERS = 1.
----
START-OF-SELECTION.
----
DATA : gs_header LIKE gt_header,
gs_item LIKE gt_item,
gs_price LIKE gt_price.
DATA : lv_cnt TYPE stunr.
CLEAR : poheader, poitem, poschedule,poaccount,pocond, poservice,
return, po_number, expheader.
CLEAR : poheaderx, poitemx, poschedulex, poaccountx, pocondx.
REFRESH : poitem, poschedule,poaccount,
return,pocond, poservice.
REFRESH : poitemx, poschedulex, poaccountx,pocondx .
PERFORM upload_file TABLES gt_header
USING fhead.
PERFORM upload_file TABLES gt_item
USING fitem.
PERFORM upload_file TABLES gt_service
USING fserv.
IF NOT fprice IS INITIAL.
PERFORM upload_file TABLES gt_price
USING fprice.
ENDIF.
**Append the Header data to PO Header table(poheader)
LOOP AT gt_header INTO gs_header.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = gs_header-vend_code
IMPORTING
output = gs_header-vend_code.
poheader-po_number = gs_header-ponum.
poheader-vendor = gs_header-vend_code.
poheader-doc_type = p_doc.
poheader-purch_org = gs_header-purch_org.
poheader-pur_group = gs_header-purch_grp.
poheader-comp_code = p_bukrs.
CONCATENATE gs_header-po_date6(4) gs_header-po_date3(2)
gs_header-po_date+0(2) INTO gs_header-po_date.
poheader-doc_date = gs_header-po_date.
poheader-pmnttrms = gs_header-pmnttrms.
poheader-incoterms1 = gs_header-incoterms1.
poheader-incoterms2 = gs_header-incoterms2.
poheader-ref_1 = gs_header-your_ref.
poheader-our_ref = gs_header-our_ref.
POHEADER-COLLECT_NO = GS_HEADER-COLLECT_NO.
IF gs_header-ponum IS NOT INITIAL.
poheaderx-po_number = 'X'.
ENDIF.
IF gs_header-vend_code IS NOT INITIAL.
poheaderx-vendor = 'X'.
ENDIF.
if GS_HEADER-doc_type is not initial.
poheaderx-doc_type = 'X'.
endif.
IF gs_header-purch_org IS NOT INITIAL.
poheaderx-purch_org = 'X'.
ENDIF.
IF gs_header-purch_grp IS NOT INITIAL.
poheaderx-pur_group = 'X'.
ENDIF.
if GS_HEADER-company_code is not initial.
poheaderx-comp_code = 'X'.
endif.
IF gs_header-po_date IS NOT INITIAL.
poheaderx-doc_date = 'X'.
ENDIF.
IF gs_header-pmnttrms IS NOT INITIAL.
poheaderx-pmnttrms = 'X'.
ENDIF.
IF gs_header-incoterms1 IS NOT INITIAL.
poheaderx-incoterms1 = 'X'.
ENDIF.
IF gs_header-incoterms2 IS NOT INITIAL.
poheaderx-incoterms2 = 'X'.
ENDIF.
IF gs_header-your_ref IS NOT INITIAL.
poheaderx-ref_1 = 'X'.
ENDIF.
IF gs_header-our_ref IS NOT INITIAL.
poheaderx-our_ref = 'X'.
ENDIF.
if GS_HEADER-COLLECT_NO is not initial.
POHEADERX-COLLECT_NO = 'X'.
endif.
**Append the Item data to PO Item table(poitem)
DATA: lv_header_text(132) TYPE c.
REFRESH : poheadertext.
poheadertext-po_number = gs_header-ponum.
poheadertext-text_id = 'F01'.
poheadertext-text_form = '/'.
CLEAR: lv_header_text.
CONCATENATE text-002 text-003 gs_header-ponum
INTO lv_header_text SEPARATED BY space.
poheadertext-text_line = lv_header_text .
APPEND poheadertext. CLEAR : poheadertext.
LOOP AT gt_item INTO gs_item WHERE ponum = gs_header-ponum.
CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
EXPORTING
input = gs_item-material
IMPORTING
output = gs_item-material.
poitem-po_item = gs_item-line_item.
poitem-material = gs_item-material.
poitem-short_text = gs_item-stext.
poitem-MATL_GROUP = GS_ITEM-MATL_GROUP.
poitem-plant = gs_item-plant.
poitem-stge_loc = gs_item-sloc.
poitem-item_cat = gs_item-item_cat.
poitem-acctasscat = gs_item-acctasscat.
poitem-quantity = gs_item-po_qty.
poitem-po_unit = gs_item-po_unit.
poitem-net_price = gs_item-net_price.
poitem-tax_code = gs_item-tax_code.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = gs_item-loc_vendor
IMPORTING
output = poitem-supp_vendor.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = gs_item-loc_customer
IMPORTING
output = poitem-customer.
*
loc_customer(10) TYPE c,"KUNNR
APPEND poitem. CLEAR poitem.
poitemx-po_item = gs_item-line_item.
IF gs_item-material IS NOT INITIAL.
poitemx-material = 'X'.
ENDIF.
poitem-SHORT_TEXT = 'X'.
poitem-MATL_GROUP = 'X'.
IF gs_item-plant IS NOT INITIAL.
poitemx-plant = 'X'.
ENDIF.
IF gs_item-sloc IS NOT INITIAL.
poitemx-stge_loc = 'X'.
ENDIF.
IF gs_item-item_cat IS NOT INITIAL.
poitemx-item_cat = 'X'.
ENDIF.
IF gs_item-acctasscat IS NOT INITIAL.
poitemx-acctasscat = 'X'.
ENDIF.
IF gs_item-po_qty IS NOT INITIAL.
poitemx-quantity = 'X'.
ENDIF.
IF gs_item-po_unit IS NOT INITIAL.
poitemx-po_unit = 'X'.
ENDIF.
IF gs_item-net_price IS NOT INITIAL.
poitemx-net_price = 'X'.
ENDIF.
IF gs_item-tax_code IS NOT INITIAL.
poitemx-tax_code = 'X'.
ENDIF.
IF gs_item-loc_customer IS NOT INITIAL.
poitemx-customer = 'X'.
ENDIF.
IF gs_item-loc_vendor IS NOT INITIAL.
poitemx-supp_vendor = 'X'.
ENDIF.
APPEND poitemx. CLEAR poitemx.
poschedule-po_item = gs_item-line_item.
poschedule-delivery_date = gs_item-delivery_date.
APPEND poschedule. CLEAR poschedule.
poschedulex-po_item = gs_item-line_item.
IF gs_item-delivery_date IS NOT INITIAL.
poschedulex-delivery_date = 'X'.
ENDIF.
APPEND poschedulex. CLEAR poschedulex.
serial_no = serial_no + 1.
poaccount-po_item = gs_item-line_item.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = gs_item-costcenter
IMPORTING
output = gs_item-costcenter.
poaccount-costcenter = gs_item-costcenter.
poaccount-gr_rcpt = gs_item-requester.
poaccount-wbs_element = gs_item-wbselement.
poaccount-network = gs_item-network_no.
poaccount-activity = gs_item-activity.
poaccount-ASSET_NO = GS_ITEM-ASSET_NO.
poaccount-orderid = gs_item-order_no.
poaccount-GR_RCPT = GS_ITEM-GR_RCPT.
poaccount-UNLOAD_PT = GS_ITEM-UNLOAD_PT.
APPEND poaccount. CLEAR poaccount.
poaccountx-po_item = gs_item-line_item.
IF gs_item-costcenter IS NOT INITIAL.
poaccountx-costcenter = 'X'.
ENDIF.
IF gs_item-requester IS NOT INITIAL.
poaccountx-gr_rcpt = 'X'.
ENDIF.
IF gs_item-wbselement IS NOT INITIAL.
poaccountx-wbs_element = 'X'.
ENDIF.
IF gs_item-network_no IS NOT INITIAL.
poaccountx-network = 'X'.
ENDIF.
IF gs_item-activity IS NOT INITIAL.
poaccountx-activity = 'X'.
ENDIF.
IF gs_item-order_no IS NOT INITIAL.
poaccountx-orderid = 'X'.
ENDIF.
APPEND poaccountx. CLEAR poaccountx.
CLEAR: lv_cnt.
ADD 1 TO lv_cnt.
LOOP AT gt_price INTO gs_price WHERE
ponum = gs_item-ponum AND
line_item = gs_item-line_item.
pocond-itm_number = gs_price-line_item.
pocond-cond_st_no = lv_cnt.
ADD 1 TO lv_cnt.
pocond-cond_type = gs_price-cond_type.
pocond-cond_value = gs_price-cond_val.
pocond-currency = gs_price-rate_unit.
pocond-currency_iso = gs_price-rate_unit.
pocond-cond_unit = gs_price-cond_unit.
pocond-numconvert = gs_price-numerator.
pocond-denominato = gs_price-denominator.
pocond-vendor_no = gs_price-del_cost_vndr.
pocond-COND_P_UNT = 1.
pocond-change_id = 'I'.
APPEND pocond. CLEAR pocond.
pocondx-cond_st_no = 000.
pocondx-currency_iso = 'X'.
pocondx-change_id = 'X'.
IF NOT gs_price-line_item IS INITIAL.
pocondx-itm_number = gs_price-line_item.
ENDIF.
IF NOT gs_price-line_item IS INITIAL.
pocondx-itm_numberx = 'X'.
ENDIF.
IF NOT gs_price-cond_type IS INITIAL.
pocondx-cond_type = 'X'.
ENDIF.
IF NOT gs_price-cond_val IS INITIAL.
pocondx-cond_value = 'X'.
ENDIF.
IF NOT gs_price-rate_unit IS INITIAL.
pocondx-currency = 'X'.
ENDIF.
IF NOT gs_price-cond_unit IS INITIAL.
pocondx-cond_unit = 'X'.
ENDIF.
IF NOT gs_price-numerator IS INITIAL.
pocondx-numconvert = 'X'.
ENDIF.
IF NOT gs_price-denominator IS INITIAL.
pocondx-denominato = 'X'.
ENDIF.
IF NOT gs_price-del_cost_vndr IS INITIAL.
pocondx-vendor_no = 'X'.
ENDIF.
APPEND pocondx. CLEAR pocondx.
ENDLOOP.
LOOP AT gt_service INTO gs_service WHERE
ponum = gs_item-ponum and
line_item = gs_item-line_item.
*
*
ENDLOOP.
ENDLOOP.
**Call the BAPI PO Create Function Module
CALL FUNCTION 'BAPI_PO_CREATE1'
EXPORTING
poheader = poheader
poheaderx = poheaderx
no_price_from_po = 'X'
IMPORTING
exppurchaseorder = po_number
expheader = expheader
TABLES
return = return
poitem = poitem
poitemx = poitemx
poschedule = poschedule
poschedulex = poschedulex
poaccount = poaccount
poaccountx = poaccountx
pocond = pocond
pocondx = pocondx
poservices = poservice
potextheader = poheadertext.
**Call the BAPI Commit Function Module
if not po_number is initial.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = wait.
endif.
WRITE 😕 gs_header.
WRITE 😕 sy-uline.
LOOP AT return WHERE type = 'E' OR
type = 'S'.
WRITE 😕 return-message.
ENDLOOP.
CLEAR: return[], return.
WRITE 😕 sy-uline.
CLEAR : poheader, poitem, poschedule, poaccount,
return, po_number, expheader,pocond.
CLEAR : poheaderx, poitemx, poschedulex, poaccountx,
pocondx, poservice.
REFRESH : poitem, poschedule,poaccount,return,pocond, poservice.
REFRESH : poitemx, poschedulex, poaccountx,pocondx.
ENDLOOP.
&----
*& Form upload_file
&----
FORM upload_file TABLES p_t_tab TYPE table
USING p_file TYPE localfile.
DATA : xfile TYPE string.
if sy-ucomm = 'ONLI'.
xfile = p_file.
CLEAR p_t_tab. REFRESH p_t_tab.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = xfile
filetype = 'ASC'
has_field_separator = 'X'
TABLES
data_tab = p_t_tab
EXCEPTIONS
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
OTHERS = 17.
IF sy-subrc <> 0.
MESSAGE e398(00) WITH text-e01. "#EC *
ENDIF.
ENDFORM. " upload_file
‎2008 Jul 07 7:15 AM
hi.....
first you will have to upload all the data from your flat file using ws_upload or gui_upload functio module into an internal table. Once you get the data into an internal table then you can use BAPI as per your requirement to update that data into the SAP system.
Direclty you can upload the data using BAP.First get it into some internal table and then use the required BAP.