Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

BAPI_PO_CREATE

Actually I don't have much time left so I though of posting this thread again, I am working on an interface where I need to create PO's using BAPI_PO_CREATE but to my bad luck the code that I have written is not working properly and I have just about an hour left to subit the program to my manager so can you please help me to get out of this very critical situation. I have written the following code,

  • internal table to store the data

************************************************************************

data: poheader LIKE bapimepoheader,

poheaderx LIKE bapimepoheaderx,

poitem LIKE bapimepoitem OCCURS 0 WITH HEADER LINE,

poitemx LIKE bapimepoitemx OCCURS 0 WITH HEADER LINE,

return LIKE bapiret2 OCCURS 0 WITH HEADER LINE,

return2 LIKE bapiret2 OCCURS 0 WITH HEADER LINE,

exppurchaseorder LIKE bapimepoheader-po_number,

poschedule LIKE bapimeposchedule OCCURS 0 WITH HEADER LINE,

poschedulex LIKE bapimeposchedulx OCCURS 0 WITH HEADER LINE.

data: begin of it_input_file OCCURS 0,

ref_no(2),

c_code(4),

doc_typ(4),

doc_date(10),

vend_no(10),

purch_org(4),

purch_grp(3),

currency(3),

item_no(3),

material(18),

Plant(4),

Strg_loc(4),

Quantity(13),

Price(10),

ord_unit(7),

del_date(10),

seq_num(5) type n,

end of it_input_file.

data: it_ref_no_old(2) type c.

data: it_record like it_input_file occurs 0 with header line.

data: it_record_x like it_input_file occurs 0 with header line.

data: ld_file LIKE rlgrap-filename.

data: it_ser_data like it_input_file occurs 0 with header line.

data: begin of it_pipe_data occurs 0,

line(1000),

end of it_pipe_data.

data: file1(70) type c value '/tmp/test_file_',

len type i.

data: begin of it_lfa1 occurs 0,

lifnr like lfa1-lifnr,

end of it_lfa1.

data: begin of it_t001 occurs 0,

bukrs like t001-bukrs,

end of it_t001.

data: begin of it_mara occurs 0,

matnr like mara-matnr,

end of it_mara.

data: begin of it_mard occurs 0,

lgort like mard-lgort,

end of it_mard.

data: begin of it_marc occurs 0,

werks like marc-werks,

end of it_marc.

data: begin of it_t024 occurs 0,

ekgrp like t024-ekgrp,

end of it_t024.

data: begin of it_t024e occurs 0,

ekorg like t024e-ekorg,

end of it_t024e.

data: begin of it_read_file occurs 0,

line(1000),

end of it_read_file.

data: it_file like it_input_file occurs 0 with header line.

data: begin of it_err_log occurs 0,

seq_num(10),

bukrs(4),

bukrs_err_desc(50),

lifnr(10),

lifnr_err_desc(50),

ekorg(4),

ekorg_err_desc(50),

ekgrp(3),

ekgrp_err_desc(50),

matnr(18),

matnr_err_desc(50),

werks(4),

werks_err_desc(50),

lgort(4),

lgort_err_desc(50),

end of it_err_log.

data: v_fullpath type string,

v_length type i.

************************************************************************

    • Definition of Variables *

************************************************************************

data: v_semfile like RLGRAP-FILENAME.

************************************************************************

    • Definition of Constants *

************************************************************************

data: c_bukrs_err_desc(50) value 'Invalid company code',

c_currency_err_desc(50) value 'Invalid valid currency',

c_lifnr_err_desc(50) value 'Invalid valid Vendor',

c_ekorg_err_desc(50) value 'Invalid valid purchasing Organization',

c_ekgrp_err_desc(50) value 'Invalid valid Purchasing group',

c_matnr_err_desc(50) value 'Invalid valid Material',

c_werks_err_desc(50) value 'Invalid valid Plant',

c_lgort_err_desc(50) value 'Invalid valid Storage Location'.

************************************************************************

  • Selection Screen *

************************************************************************

selection-screen begin of block b1 with frame title text-001.

parameters: p_ifname LIKE rlgrap-filename OBLIGATORY

DEFAULT 'C:\'.

parameter : p_lfile TYPE c RADIOBUTTON GROUP g1, " local file

p_ufile TYPE c RADIOBUTTON GROUP g1 DEFAULT 'X'. " Unix file

selection-screen end of block b1 .

selection-screen begin of block b2 with frame. " text-002.

parameters: p_user like sy-uname MODIF ID md1 default syst-uname.

selection-screen end of block b2.

selection-screen begin of block b3 with frame title text-003.

parameters: p_upld radiobutton group upld default 'X' ,

p_upldex radiobutton group upld.

selection-screen end of block b3.

  • at selection screen

at selection-screen on value-request for p_ifname.

call function 'F4_FILENAME' "allows user to select path/file

EXPORTING

program_name = 'Z_PO_Creation'

dynpro_number = syst-dynnr

field_name = 'p_ifname'

IMPORTING

file_name = p_ifname.

************************************************************************

  • START-OF-SELECTION *

************************************************************************

start-of-selection.

if p_upld = 'X'.

perform build_table.

perform validate_data.

perform disp_error_report.

else.

if p_ufile = 'X'.

perform open_unix_dataset.

else.

perform open_files.

endif.

perform data_process.

endif.

  • perform put_data.

end-of-selection.

&----


*& Form call_bapi

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM call_bapi .

LOOP AT it_input_file.

  • moving header data.

MOVE: it_input_file-vend_no TO poheader-vendor,

it_input_file-doc_typ TO poheader-doc_type,

it_input_file-purch_org TO poheader-purch_org,

it_input_file-purch_grp TO poheader-pur_group,

it_input_file-c_code TO poheader-comp_code,

it_input_file-doc_date TO poheader-doc_date.

  • updating header data.

if poheader-vendor <> ' '.

poheaderx-vendor = 'X'.

endif.

if poheader-doc_type <> ' '.

poheaderx-doc_type = 'X'.

endif.

if poheader-doc_date <> ' '.

poheaderx-doc_date = 'X'.

endif.

if poheader-purch_org <> ' '.

poheaderx-purch_org = 'X'.

endif.

if poheader-pur_group <> ' '.

poheaderx-pur_group = 'X'.

endif.

if poheader-comp_code <> ' '.

poheaderx-comp_code = 'X'.

endif.

  • moving item data.

MOVE: it_input_file-item_no TO poitem-po_item,

it_input_file-plant TO poitem-plant,

it_input_file-Strg_loc TO poitem-stge_loc,

it_input_file-material TO poitem-material,

it_input_file-quantity TO poitem-quantity,

it_input_file-price TO poitem-price_unit,

it_input_file-ref_no TO poitem-ref_doc,

it_input_file-ord_unit TO poitem-po_unit.

  • updating Item data.

if poitem-plant <> ' '.

poitemx-plant = 'X'.

endif.

if poitem-material <> ' '.

poitemx-material = 'X'.

endif.

if poitem-stge_loc <> ' '.

poitemx-stge_loc = 'X'.

endif.

if poitem-quantity <> ' '.

poitemx-quantity = 'X'.

endif.

  • if poitem-price_unit <> ' '.

  • poitemx-price_unit = 'X'.

  • endif.

*

  • if poitem-ref_doc <> ' '.

  • poitemx-ref_doc = 'X'.

  • endif.

*

  • if poitem-po_unit <> ' '.

  • poitemx-po_unit = 'X'.

  • endif.

poitemx-po_item = it_input_file-item_no.

move: it_input_file-del_date TO poschedule-delivery_date.

move: 'X' TO poschedulex-delivery_date.

APPEND: poitem, poitemx.

CALL FUNCTION 'BAPI_PO_CREATE1'

EXPORTING

poheader = poheader

poheaderx = poheaderx

IMPORTING

exppurchaseorder = exppurchaseorder

TABLES

poitem = poitem

poitemx = poitemx

poschedule = poschedule

poschedulex = poschedulex

return = return.

CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

IMPORTING

return = return2.

WRITE:/ 'PO created with PO number:' ,

exppurchaseorder.

ENDLOOP.

ENDFORM. " call_bapi

&----


*& Form data_process

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM data_process .

if it_input_file[] is initial.

message E319 with 'There is no data to process!!'.

else.

sort it_input_file by ref_no.

clear: it_ref_no_old.

loop at it_input_file into it_record.

if it_record-ref_no ne it_ref_no_old .

loop at it_input_file into it_record_x

where ref_no = it_record-ref_no .

endloop.

if syst-subrc = 0 .

PERFORM call_bapi.

endif.

endif.

it_ref_no_old = it_record-ref_no.

endloop.

endif.

ENDFORM. " data_process

&----


*& Form put_data

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM put_data .

it_ser_data-ref_no = '1'.

it_ser_data-c_code = 'AEM'.

it_ser_data-doc_typ = 'AB'.

it_ser_data-vend_no = '100004'.

it_ser_data-purch_org = 'A001'.

it_ser_data-purch_grp = '522'.

it_ser_data-currency = 'USD'.

it_ser_data-item_no = '10'.

it_ser_data-material = '1000003'.

it_ser_data-Plant = '1005'.

  • it_ser_data-Strg_loc

it_ser_data-Quantity = '100'.

it_ser_data-Price = '2000'.

it_ser_data-ord_unit = 'EA'.

  • it_ser_data-del_date =

append it_ser_data.

clear it_ser_data.

loop at it_ser_data.

concatenate it_ser_data-ref_no it_ser_data-c_code it_ser_data-doc_typ it_ser_data-vend_no it_ser_data-purch_org it_ser_data-purch_grp

it_ser_data-currency it_ser_data-item_no it_ser_data-material it_ser_data-Plant it_ser_data-Quantity it_ser_data-Price

it_ser_data-ord_unit into it_pipe_data separated by '|'.

append it_pipe_data.

endloop.

clear it_pipe_data.

open dataset file1 in text mode for output encoding default.

if syst-subrc eq 0.

loop at it_pipe_data.

transfer it_pipe_data to file1.

endloop.

close dataset file1.

endif.

ENDFORM. " put_data

&----


*& Form validate_data

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM validate_data .

loop at it_input_file.

read table it_t001 with key bukrs = it_input_file-c_code.

if syst-subrc <> 0.

move: it_input_file-c_code to it_err_log-bukrs,

it_input_file-seq_num to it_err_log-seq_num,

c_bukrs_err_desc to it_err_log-bukrs_err_desc.

endif.

read table it_t024 with key ekgrp = it_input_file-purch_grp.

if syst-subrc <> 0.

move: it_input_file-purch_grp to it_err_log-ekgrp,

it_input_file-seq_num to it_err_log-seq_num,

c_ekgrp_err_desc to it_err_log-ekgrp_err_desc.

endif.

read table it_t024e with key ekorg = it_input_file-purch_org.

if syst-subrc <> 0.

move: it_input_file-purch_org to it_err_log-ekorg,

it_input_file-seq_num to it_err_log-seq_num,

c_ekorg_err_desc to it_err_log-ekorg_err_desc.

endif.

read table it_lfa1 with key lifnr = it_input_file-vend_no.

if syst-subrc <> 0.

move: it_input_file-vend_no to it_err_log-lifnr,

it_input_file-seq_num to it_err_log-seq_num,

c_lifnr_err_desc to it_err_log-lifnr_err_desc.

endif.

read table it_mara with key matnr = it_input_file-material.

if syst-subrc <> 0.

move: it_input_file-material to it_err_log-matnr,

it_input_file-seq_num to it_err_log-seq_num,

c_matnr_err_desc to it_err_log-matnr_err_desc.

endif.

read table it_marc with key werks = it_input_file-plant.

if syst-subrc <> 0.

move: it_input_file-plant to it_err_log-werks,

it_input_file-seq_num to it_err_log-seq_num,

c_werks_err_desc to it_err_log-werks_err_desc.

endif.

read table it_mard with key lgort = it_input_file-Strg_loc.

if syst-subrc <> 0.

move: it_input_file-strg_loc to it_err_log-lgort,

it_input_file-seq_num to it_err_log-seq_num,

c_lgort_err_desc to it_err_log-lgort_err_desc.

endif.

endloop.

ENDFORM. " validate_data

&----


*& Form open_unix_dataset

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM open_unix_dataset .

open dataset ld_file for input in text mode encoding default.

do.

read dataset ld_file into it_read_file.

if sy-subrc NE 0.

exit.

endif.

append it_read_file.

clear it_read_file.

enddo.

close dataset ld_file.

if NOT it_read_file[] is initial.

loop at it_read_file.

split it_read_file at '|' into it_file-item_no

it_file-ref_no

it_file-vend_no

it_file-material

it_file-quantity

it_file-price

it_file-ord_unit

it_file-plant

it_file-strg_loc

it_file-purch_org

it_file-purch_grp

it_file-c_code

it_file-doc_typ.

append it_file.

clear it_file.

endloop.

endif.

ENDFORM. " open_unix_dataset

&----


*& Form open_files

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM open_files .

v_semfile = p_ifname.

CALL FUNCTION 'WS_UPLOAD'

EXPORTING

filename = v_semfile

filetype = 'DAT'

TABLES

data_tab = it_input_file

EXCEPTIONS

conversion_error = 1

file_open_error = 2

file_read_error = 3

invalid_table_width = 4

invalid_type = 5

no_batch = 6

unknown_error = 7

gui_refuse_filetransfer = 8

OTHERS = 9.

ENDFORM. " open_files

&----


*& Form build_table

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM build_table .

select bukrs into table it_t001

from t001

for all entries in it_input_file

where bukrs = it_input_file-c_code.

select ekgrp into table it_t024

from t024

for all entries in it_input_file

where ekgrp = it_input_file-purch_grp.

select ekorg into table it_t024e

from t024e

for all entries in it_input_file

where ekorg = it_input_file-purch_org.

select lifnr into table it_lfa1

from lfa1

for all entries in it_input_file

where lifnr = it_input_file-vend_no.

select matnr into table it_mara

from mara

for all entries in it_input_file

where matnr = it_input_file-material.

select werks into table it_marc

from marc

for all entries in it_input_file

where werks = it_input_file-plant.

select lgort into table it_mard

from mard

for all entries in it_input_file

where lgort = it_input_file-Strg_loc.

ENDFORM. " build_table

&----


*& Form disp_error_report

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM disp_error_report .

if NOT it_err_log[] is initial.

move: '0' to it_err_log-seq_num,

'Vendor' to it_err_log-lifnr,

'Vendor Error' to it_err_log-lifnr_err_desc,

'Company Code' to it_err_log-bukrs,

'Company Code Error' to it_err_log-bukrs_err_desc,

'Purchase Org' to it_err_log-ekorg,

'Purchase Org Error' to it_err_log-ekorg_err_desc,

'Purchase Group' to it_err_log-ekgrp,

'Purchase Group Error' to it_err_log-ekgrp_err_desc,

'Plant' to it_err_log-werks,

'Plant Error' to it_err_log-werks_err_desc,

'Storage Loc' to it_err_log-lgort,

'Storage Loc Error' to it_err_log-lgort_err_desc,

'Material' to it_err_log-matnr,

'Material Error' to it_err_log-matnr_err_desc.

append it_err_log.

sort it_err_log by seq_num.

loop at it_err_log where seq_num eq '0'.

move 'Seq Num' to it_err_log-seq_num.

modify it_err_log.

endloop.

concatenate 'c:\inventory_error_log' syst-datum syst-uzeit '.xls'

into v_fullpath.

CALL FUNCTION 'GUI_DOWNLOAD'

EXPORTING

  • BIN_FILESIZE =

FILENAME = v_fullpath

FILETYPE = 'DAT'

  • APPEND = ' '

  • WRITE_FIELD_SEPARATOR = ' '

  • HEADER = '00'

  • TRUNC_TRAILING_BLANKS = ' '

  • WRITE_LF = 'X'

  • COL_SELECT = ' '

  • COL_SELECT_MASK = ' '

  • DAT_MODE = ' '

  • CONFIRM_OVERWRITE = ' '

  • NO_AUTH_CHECK = ' '

  • CODEPAGE = ' '

  • IGNORE_CERR = ABAP_TRUE

  • REPLACEMENT = '#'

  • WRITE_BOM = ' '

  • TRUNC_TRAILING_BLANKS_EOL = 'X'

  • WK1_N_FORMAT = ' '

  • WK1_N_SIZE = ' '

  • WK1_T_FORMAT = ' '

  • WK1_T_SIZE = ' '

IMPORTING

FILELENGTH = v_length

TABLES

DATA_TAB = it_err_log

  • FIELDNAMES =

EXCEPTIONS

FILE_WRITE_ERROR = 1

NO_BATCH = 2

GUI_REFUSE_FILETRANSFER = 3

INVALID_TYPE = 4

NO_AUTHORITY = 5

UNKNOWN_ERROR = 6

HEADER_NOT_ALLOWED = 7

SEPARATOR_NOT_ALLOWED = 8

FILESIZE_NOT_ALLOWED = 9

HEADER_TOO_LONG = 10

DP_ERROR_CREATE = 11

DP_ERROR_SEND = 12

DP_ERROR_WRITE = 13

UNKNOWN_DP_ERROR = 14

ACCESS_DENIED = 15

DP_OUT_OF_MEMORY = 16

DISK_FULL = 17

DP_TIMEOUT = 18

FILE_NOT_FOUND = 19

DATAPROVIDER_EXCEPTION = 20

CONTROL_FLUSH_ERROR = 21

OTHERS = 22

.

IF SY-SUBRC <> 0.

write:/30 'Error in generating purchase order error log'.

write:/30 syst-uline(44).

else.

write:/30 'Purchase Order error log'.

write:/30 syst-uline(24).

skip 2.

write:/10 'Please check the error loag file at:', v_fullpath.

ENDIF.

else.

write:/30 'Purchase Order data l log'.

write:/30 syst-uline(24).

skip 2.

write:/10 'There is no error in the input file data, so please proceed to execute the load!!'.

endif.

ENDFORM. " disp_error_report

and my input file looks like this:

1 1000 NB 20071210 V1 AE01 501 USD 10 M5 1003 100 1000 EA 20071110

1 1000 NB 20071210 V1 AE01 507 USD 10 P10 1003 100 2000 EA 20071110

2 1000 NB 20071210 V1 AE01 501 USD 10 M5 1003 50 1000 EA 20071110

the name of the fields in order are : refno., compnaycode,doc-typ,doc_date,vendor,purch_org,purch_grp,currency

tem no, material, plant, quantity, price, order unit, del_date.

The ref No. field in the text file acts as a unique identifier So basically my Program should create 2 PO's but to my surprise it created 5 or 6 po's with only first record.

Can you please help me out....

Thanks

Rajeev Gupta

Message was edited by:

Rajeev Gupta

Title and Message were edited by:

Alvaro Tejada Galindo

Tags:
Former Member
Not what you were looking for? View more on this topic or Ask a question