10-08-2007 8:44 PM
I am working on PO creation and for this I have written the following code, PO is meant for multiple line items, and each PO should be differentiate from the other on the basis of the refrence field. The code is as follows:
&----
*& Form get_data
&----
text
----
--> p1 text
<-- p2 text
----
FORM get_data .
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. " get_data
&----
*& 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.
*
&----
*& 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.
poheaderx-vendor = 'X'.
poheaderx-doc_type = 'X'.
poheaderx-doc_date = 'X'.
poheaderx-purch_org = 'X'.
poheaderx-pur_group = 'X'.
poheaderx-comp_code = 'X'.
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.
poitemx-plant = 'X'.
poitemx-stge_loc = 'X'.
poitemx-material = 'X'.
poitemx-quantity = 'X'.
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:/ exppurchaseorder.
ENDLOOP.
ENDFORM. " call_bapi
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 : <b>refno., compnaycode,doc-typ,doc_date,vendor,purch_org,purch_grp,currency
tem no, material, plant, quantity, price, order unit, del_date</b>.
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 to get out of this issue.
Thanks
Rajeev Gupta
10-08-2007 8:51 PM
Try to debug in debugging mode. Place breakpoint at loop and try to obeserve how the records are processed from input file to BAPI. I think then you will find where the problem lies.
Hope it helps you.
10-08-2007 8:52 PM
Hi Rajeev,
Keep a Break point before the BAPI starting anf see the population of fields in debugging.
Also check the Append POitem and PoitemX..
See the sample code and do accordingly it will work
REPORT zpo_bapi_purchord_tej.
----
DATA DECLARATIONS *
----
TYPE-POOLS slis.
TYPES: BEGIN OF ty_table,
v_legacy(8),
vendor TYPE bapimepoheader-vendor,
purch_org TYPE bapimepoheader-purch_org,
pur_group TYPE bapimepoheader-pur_group,
material TYPE bapimepoitem-material,
quantity(13),
delivery_date TYPE bapimeposchedule-delivery_date,
net_price(23),
plant TYPE bapimepoitem-plant,
END OF ty_table.
TYPES: BEGIN OF ty_alv,
v_legs(8),
success(10),
v_legf(8),
END OF ty_alv.
TYPES: BEGIN OF ty_alv1,
v_legf1(8),
v_msg(500),
END OF ty_alv1.
*-----Work area declarations.
DATA: x_table TYPE ty_table,
x_header TYPE bapimepoheader,
x_headerx TYPE bapimepoheaderx,
x_item TYPE bapimepoitem,
x_itemx TYPE bapimepoitemx,
x_sched TYPE bapimeposchedule,
x_schedx TYPE bapimeposchedulx,
x_commatable(255),
x_alv TYPE ty_alv,
x_alv1 TYPE ty_alv1,
x_alv2 TYPE ty_alv1.
*-----Internal table declarations.
DATA: it_table TYPE TABLE OF ty_table,
it_commatable LIKE TABLE OF x_commatable,
it_item TYPE TABLE OF bapimepoitem,
it_itemx TYPE TABLE OF bapimepoitemx,
it_sched TYPE TABLE OF bapimeposchedule,
it_schedx TYPE TABLE OF bapimeposchedulx,
it_alv TYPE TABLE OF ty_alv,
it_alv1 TYPE TABLE OF ty_alv1,
it_alv2 TYPE TABLE OF ty_alv1.
DATA: po_number TYPE bapimepoheader-po_number,
x_return TYPE bapiret2,
it_return TYPE TABLE OF bapiret2,
v_file TYPE string,
v_temp(8),
v_succsount TYPE i VALUE 0,
v_failcount TYPE i VALUE 0,
v_total TYPE i.
DATA: v_temp1(5) TYPE n VALUE 0.
DATA: x_event TYPE slis_t_event,
x_fieldcat TYPE slis_t_fieldcat_alv,
x_list_header TYPE slis_t_listheader,
x_event1 LIKE LINE OF x_event,
x_layout1 TYPE slis_layout_alv,
x_variant1 TYPE disvariant,
x_repid2 LIKE sy-repid.
DATA : it_fieldcat TYPE TABLE OF slis_t_fieldcat_alv.
----
SELECTION-SCREEN *
----
SELECTION-SCREEN BEGIN OF BLOCK v_b1 WITH FRAME.
*-----To fetch the flat file.
PARAMETERS: p_file TYPE rlgrap-filename.
SELECTION-SCREEN END OF BLOCK v_b1.
----
AT SELECTION-SCREEN *
----
AT SELECTION-SCREEN.
IF p_file IS INITIAL.
MESSAGE text-001 TYPE 'E'.
ENDIF.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
*-----To use F4 help to find file path.
CALL FUNCTION 'F4_FILENAME'
EXPORTING
program_name = syst-cprog
dynpro_number = syst-dynnr
IMPORTING
file_name = p_file.
v_file = p_file.
----
START-OF-SELECTION *
----
START-OF-SELECTION.
PERFORM gui_upload.
LOOP AT it_table INTO x_table.
PERFORM header_details.
v_temp = x_table-v_legacy.
LOOP AT it_table INTO x_table WHERE v_legacy = v_temp.
PERFORM lineitem.
PERFORM schedule.
ENDLOOP.
DELETE it_table WHERE v_legacy = v_temp.
PERFORM bapicall.
MOVE po_number TO x_alv-success.
APPEND x_alv TO it_alv.
CLEAR x_alv.
*-----To clear the item details in internal table after the operation for a header.
REFRESH: it_item,
it_itemx,
it_sched,
it_schedx.
CLEAR: v_temp1.
ENDLOOP.
v_total = v_succsount + v_failcount.
PERFORM display_alv.
----
FORM GUI_UPLOAD *
----
FORM gui_upload .
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = v_file
filetype = 'ASC'
TABLES
data_tab = it_commatable
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.
*-----To fetch the comma seperated flat file into an internal table.
LOOP AT it_commatable INTO x_commatable.
IF x_commatable IS NOT INITIAL.
SPLIT x_commatable AT ',' INTO
x_table-v_legacy
x_table-vendor
x_table-purch_org
x_table-pur_group
x_table-material
x_table-quantity
x_table-delivery_date
x_table-net_price
x_table-plant.
APPEND x_table TO it_table.
ENDIF.
CLEAR x_table.
ENDLOOP.
ENDIF.
ENDFORM. " gui_upload
----
FORM HEADER_DETAILS *
----
FORM header_details .
MOVE 'NB' TO x_header-doc_type.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = x_table-vendor
IMPORTING
output = x_table-vendor
.
MOVE x_table-vendor TO x_header-vendor.
MOVE x_table-purch_org TO x_header-purch_org.
MOVE x_table-pur_group TO x_header-pur_group.
x_headerx-doc_type = 'X'.
x_headerx-vendor = 'X'.
x_headerx-purch_org = 'X'.
x_headerx-pur_group = 'X'.
ENDFORM. " header_details
----
FORM LINEITEM *
----
FORM lineitem .
v_temp1 = v_temp1 + 10.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = v_temp1
IMPORTING
output = v_temp1.
MOVE v_temp1 TO x_item-po_item.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = x_table-material
IMPORTING
output = x_table-material.
MOVE x_table-material TO x_item-material.
MOVE x_table-quantity TO x_item-quantity.
MOVE x_table-net_price TO x_item-net_price.
MOVE x_table-plant TO x_item-plant.
x_itemx-po_item = v_temp1.
x_itemx-material = 'X'.
x_itemx-quantity = 'X'.
x_itemx-net_price = 'X'.
x_itemx-plant = 'X'.
APPEND x_item TO it_item.
APPEND x_itemx TO it_itemx.
CLEAR: x_item, x_itemx.
ENDFORM. " lineitem1
----
FORM SCHEDULE *
----
FORM schedule .
MOVE x_table-delivery_date TO x_sched-delivery_date.
MOVE v_temp1 TO x_sched-po_item.
x_schedx-delivery_date = 'X'.
x_schedx-po_item = v_temp1.
APPEND x_sched TO it_sched.
APPEND x_schedx TO it_schedx.
CLEAR: x_sched, x_schedx.
ENDFORM. " schedule
----
FORM BAPICALL *
----
FORM bapicall .
CALL FUNCTION 'BAPI_PO_CREATE1'
EXPORTING
poheader = x_header
poheaderx = x_headerx
IMPORTING
exppurchaseorder = po_number
TABLES
return = it_return
poitem = it_item
poitemx = it_itemx
poschedule = it_sched
poschedulex = it_schedx.
IF po_number IS NOT INITIAL.
v_succsount = v_succsount + 1.
MOVE x_table-v_legacy TO x_alv-v_legs.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
ELSE.
v_failcount = v_failcount + 1.
MOVE x_table-v_legacy TO x_alv-v_legf.
MOVE x_table-v_legacy TO x_alv1-v_legf1.
LOOP AT it_return INTO x_return.
IF x_alv1-v_msg IS INITIAL.
MOVE x_return-message TO x_alv1-v_msg.
ELSE.
CONCATENATE x_alv1-v_msg x_return-message INTO x_alv1-v_msg SEPARATED BY space.
ENDIF.
ENDLOOP.
APPEND x_alv1 TO it_alv1.
CLEAR x_alv1.
ENDIF.
ENDFORM. " bapicall
----
FORM DISPLAY_ALV *
----
FORM display_alv .
PERFORM x_list_header.
PERFORM build_fieldcat CHANGING x_fieldcat.
x_repid2 = sy-repid.
x_event1-name = 'TOP_OF_PAGE'.
x_event1-form = 'TOP_OF_PAGE'.
APPEND x_event1 TO x_event.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = x_repid2
is_layout = x_layout1
it_fieldcat = x_fieldcat
i_callback_user_command = 'USER_COMMAND'
i_callback_top_of_page = 'TOP_OF_PAGE'
i_save = 'A'
is_variant = x_variant1
it_events = x_event
TABLES
t_outtab = it_alv
EXCEPTIONS
program_error = 1
OTHERS = 2.
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. " display_master_data
----
FORM USER_COMMAND *
----
FORM user_command USING ucomm LIKE sy-ucomm selfield
TYPE slis_selfield.
READ TABLE it_alv INTO x_alv INDEX selfield-tabindex.
CLEAR : x_alv2,it_alv2[].
LOOP AT it_alv1 INTO x_alv1 WHERE v_legf1 = x_alv-v_legf.
x_alv2 = x_alv1.
APPEND x_alv2 TO it_alv2 .
ENDLOOP.
DATA : it_fieldcat TYPE slis_t_fieldcat_alv.
DATA : x3_fieldcat LIKE LINE OF it_fieldcat.
CLEAR : x3_fieldcat,it_fieldcat[].
CLEAR x3_fieldcat.
x3_fieldcat-col_pos = '1'.
x3_fieldcat-fieldname = 'V_LEGF1'.
x3_fieldcat-reptext_ddic = text-111.
x3_fieldcat-ref_tabname = 'IT_ALV2'.
APPEND x3_fieldcat TO it_fieldcat.
CLEAR x3_fieldcat.
CLEAR x3_fieldcat.
x3_fieldcat-col_pos = '1'.
x3_fieldcat-fieldname = 'V_MSG'.
x3_fieldcat-reptext_ddic = text-112.
x3_fieldcat-ref_tabname = 'IT_ALV2'.
APPEND x3_fieldcat TO it_fieldcat.
CLEAR x3_fieldcat.
x_layout1-colwidth_optimize = 'X'.
x_layout1-zebra = 'X'.
IF it_alv2[] IS NOT INITIAL.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = x_repid2
is_layout = x_layout1
it_fieldcat = it_fieldcat
i_save = 'A'
i_callback_top_of_page = 'TOP'
is_variant = x_variant1
it_events = x_event
TABLES
t_outtab = it_alv2
EXCEPTIONS
program_error = 1
OTHERS = 2.
ENDIF.
ENDFORM.
----
FORM USER_COMMAND *
----
FORM top.
CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
it_list_commentary = 'Commentry'.
ENDFORM.
----
FORM BUILD_FIELDCAT *
----
FORM build_fieldcat CHANGING et_fieldcat TYPE slis_t_fieldcat_alv.
DATA: x1_fieldcat TYPE slis_fieldcat_alv.
CLEAR x1_fieldcat.
x1_fieldcat-col_pos = '1'.
x1_fieldcat-fieldname = 'V_LEGS'.
x1_fieldcat-reptext_ddic = text-108.
x1_fieldcat-ref_tabname = 'IT_ALV'.
APPEND x1_fieldcat TO et_fieldcat.
CLEAR x1_fieldcat.
x1_fieldcat-col_pos = '2'.
x1_fieldcat-fieldname = 'SUCCESS'.
x1_fieldcat-key = 'X'.
x1_fieldcat-reptext_ddic = text-109.
x1_fieldcat-ref_tabname = 'IT_ALV'.
APPEND x1_fieldcat TO et_fieldcat.
CLEAR x1_fieldcat.
x1_fieldcat-col_pos = '3'.
x1_fieldcat-fieldname = 'V_LEGF'.
x1_fieldcat-key = 'X'.
x1_fieldcat-reptext_ddic = text-110.
x1_fieldcat-ref_tabname = 'IT_ALV'.
APPEND x1_fieldcat TO et_fieldcat.
CLEAR x1_fieldcat.
ENDFORM. " build_fieldcat
----
FORM BUILD_LIST_HEADER *
----
FORM x_list_header.
DATA: x_list_header1 TYPE slis_listheader.
*-----List Header: type H
CLEAR x_list_header1 .
x_list_header1-typ = 'H'.
x_list_header1-info = text-105.
APPEND x_list_header1 TO x_list_header.
*-----List Key: type S
x_list_header1-typ = 'S'.
x_list_header1-key = text-106.
x_list_header1-info = v_total.
APPEND x_list_header1 TO x_list_header.
*-----List Key: Type S
CLEAR x_list_header1 .
x_list_header1-typ = 'S'.
x_list_header1-key = text-107.
x_list_header1-info = v_succsount.
APPEND x_list_header1 TO x_list_header.
ENDFORM. " build_list_header
----
FORM TOP_OF_PAGE *
----
FORM top_of_page.
CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
it_list_commentary = x_list_header.
ENDFORM. " TOP_OF_PAGE
Regards
Anji
10-08-2007 10:11 PM
Hi,
Make the following changes to your code.
1) Declare a work are l_record just similar to it_record
2) LOOP AT it_input_file into it_record.
l_record = it_record.
AT NEW REF_NO.
*Fill your Header Data
MOVE: l_record-vend_no TO poheader-vendor,
l_record-doc_typ TO poheader-doc_type,
l_record-purch_org TO poheader-purch_org,
l_record-purch_grp TO poheader-pur_group,
l_record-c_code TO poheader-comp_code,
l_record-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.
ENDAT
Fill the item tables
MOVE: l_record-item_no TO poitem-po_item,
l_record-plant TO poitem-plant,
l_record-Strg_loc TO poitem-stge_loc,
l_record-material TO poitem-material,
l_record-quantity TO poitem-quantity,
l_record-price TO poitem-price_unit,
l_record-ref_no TO poitem-ref_doc,
l_record-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.
poitemx-po_item = l_record-item_no.
move: l_record-del_date TO poschedule-delivery_date.
move: 'X' TO poschedulex-delivery_date.
AT END OF REF_NO.
Call the BAPI.
CALL FUNCTION 'BAPI_PO_CREATE1'
EXPORTING
poheader = poheader
poheaderx = poheaderx
IMPORTING
exppurchaseorder = exppurchaseorder
TABLES
poitem = poitem
poitemx = poitemx
poschedule = poschedule
poschedulex = poschedulex
return = return.
if exppurchaseorder is not initial.
WRITE:/ exppurchaseorder.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
IMPORTING
return = return2.
else.
*Write logic to format error message as per your requirment
******************************************
******************************************
******************************************
endif.
clear: poheader, poheaderx, poitem, poitemx, poschedule, poschedulex, exppurchaseorder.
ENDAT.
clear l_record.
ENDLOOP.
Hope this will solve your problem.
Rewad points if useful
Enjoy SAP.
Rajasekhar