Application Development 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: 

Create a purchase order by uploading an Excel File.

matheusmaximo
Discoverer
0 Kudos
1,465

Hi Guys!

I trying to make a program to create a Purchase Order using BAPI_PO_CREATE1 by uploading an Excel File using the function ALSM_EXCEL_TO_INTERNAL_TABLE, and showing in a ALV. I'm new in this 'ABAP World' so i searched for how to use the functions, but when I upload the file, just show the ALV like the Excel, it isn't creating the purchase order.

My code below:

*&---------------------------------------------------------------------*
*& Report Z14MM_PEDIDOS
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT z14mm_pedidos.

TABLES: ekko, ekpo.

PARAMETERS: p_file TYPE rlgrap-filename.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

PERFORM leitura_excel.

FORM leitura_excel.


DATA: ti_filetable TYPE filetable,
vl_rc TYPE i.

CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
window_title = 'Selecione um arquivo'

CHANGING
file_table = ti_filetable
rc = vl_rc

EXCEPTIONS
file_open_dialog_failed = 1
cntl_error = 2
error_no_gui = 3
not_supported_by_gui = 4
OTHERS = 5.
IF sy-subrc <> 0.
MESSAGE 'ERRO AO SELECIONAR ARQUIVO' TYPE 'E'.
ENDIF.

READ TABLE ti_filetable INTO DATA(wa_filetable) INDEX 1.

IF sy-subrc IS INITIAL.
p_file = wa_filetable-filename.
ENDIF.

endform.

START-OF-SELECTION.





TYPES : BEGIN OF str_final,

comp_code TYPE bapimepoheader-comp_code,

item_intvl TYPE bapimepoheader-item_intvl,

vendor TYPE bapimepoheader-vendor,

pmnttrms TYPE bapimepoheader-pmnttrms,

purch_org TYPE bapimepoheader-purch_org,

pur_group TYPE bapimepoheader-pur_group,

currency TYPE bapimepoheader-currency,

po_item TYPE bapimepoitem-po_item,

material TYPE bapimepoitem-material,

plant TYPE bapimepoitem-plant,

stge_loc TYPE bapimepoitem-stge_loc,

quantity TYPE bapimepoitem-quantity,

tax_code TYPE bapimepoitem-tax_code,

item_cat TYPE bapimepoitem-item_cat,

acctasscat TYPE bapimepoitem-acctasscat,

sched_line TYPE bapimeposchedule-sched_line,

delivery_date TYPE bapimeposchedule-delivery_date,

serial_no TYPE bapimepoaccount-serial_no,

gl_account TYPE bapimepoaccount-gl_account,

costcenter TYPE bapimepoaccount-costcenter,

co_area TYPE bapimepoaccount-co_area,

doc_type TYPE bapimepoheader-doc_type,

doc_date TYPE bapimepoheader-doc_date,

v_ebeln TYPE ekko-ebeln,

matl_group TYPE bapimepoitem-matl_group,

po_unit TYPE bapimepoitem-po_unit,

net_price TYPE bapimepoitem-po_price,

orderpr_un TYPE bapimepoitem-orderpr_un,

short_text TYPE bapimepoitem-short_text,

END OF str_final.

DATA : ti_final TYPE TABLE OF str_final,

wa_final TYPE str_final.

DATA: ti_poheader TYPE STANDARD TABLE OF bapimepoheader,
wa_poheader LIKE LINE OF ti_poheader,
ti_poheaderx TYPE STANDARD TABLE OF bapimepoheaderx,
wa_poheaderx LIKE LINE OF ti_poheaderx,
ti_poitem TYPE TABLE OF bapimepoitem,
wa_poitem LIKE LINE OF ti_poitem,
ti_poitemx TYPE TABLE OF bapimepoitemx,
wa_poitemx LIKE LINE OF ti_poitemx,
ti_bapiret2 TYPE TABLE OF bapiret2,
wa_bapiret2 LIKE LINE OF ti_bapiret2,
v_ebeln TYPE ekko-ebeln,
ponum TYPE bapimepoheader,
ti_poschedule TYPE TABLE OF bapimeposchedule,
wa_poschedule LIKE LINE OF ti_poschedule,
ti_poaccount TYPE TABLE OF bapimepoaccount,
wa_poaccount LIKE LINE OF ti_poaccount,
ti_poschedulex TYPE TABLE OF bapimeposchedulx,
wa_poschedulex LIKE LINE OF ti_poschedulex,
ti_poaccountx TYPE TABLE OF bapimepoaccountx,
wa_poaccountx LIKE LINE OF ti_poaccountx.


CLEAR wa_poheader.
wa_poheader-comp_code = ekko-bukrs.
wa_poheader-doc_type = ekko-bsart.
wa_poheader-vendor = ekko-lifnr.
wa_poheader-purch_org = ekko-ekorg.
wa_poheader-pur_group = ekko-ekgrp.

CLEAR wa_poheaderx.
wa_poheaderx-comp_code = 'X'.
wa_poheaderx-doc_type = 'X'.
wa_poheaderx-vendor = 'X'.
wa_poheaderx-purch_org = 'X'.
wa_poheaderx-pur_group = 'X'.

CLEAR wa_poitem.
wa_poitem-po_item = ekpo-ebelp.
wa_poitem-short_text = ekpo-txz01.
wa_poitem-material = ekpo-matnr.
wa_poitem-plant = ekpo-werks.
wa_poitem-matl_group = ekpo-matkl.
wa_poitem-info_rec = ekpo-infnr.
wa_poitem-quantity = ekpo-menge.
wa_poitem-po_unit = ekpo-meins.
wa_poitem-net_price = ekpo-netpr.
wa_poitem-price_unit = ekpo-peinh.
wa_poitem-item_cat = ekpo-pstyp.
wa_poitem-gr_ind = ekpo-wepos.
wa_poitem-ir_ind = ekpo-repos.
wa_poitem-gr_non_val = ekpo-weunb.
wa_poitem-gr_basediv = ekpo-webre.
wa_poitem-tax_code = ekpo-mwskz.
APPEND wa_poitem TO ti_poitem.

CLEAR wa_poitemx.
wa_poitemx-po_item = 'X'.
wa_poitemx-po_itemx = 'X'.
wa_poitemx-short_text = 'X'.
wa_poitemx-material = 'X'.
wa_poitemx-plant = 'X'.
wa_poitemx-matl_group = 'X'.
wa_poitemx-info_rec = 'X'.
wa_poitemx-quantity = 'X'.
wa_poitemx-po_unit = 'X'.
wa_poitemx-net_price = 'X'.
wa_poitemx-price_unit = 'X'.
wa_poitemx-tax_code = 'X'.
wa_poitemx-item_cat = 'X'.
wa_poitemx-gr_ind = 'X'.
wa_poitemx-gr_non_val = 'X'.
wa_poitemx-ir_ind = 'X'.
wa_poitemx-gr_basediv = 'X'.
APPEND wa_poitemx TO ti_poitemx.

CALL FUNCTION 'BAPI_PO_CREATE1'
EXPORTING
poheader = wa_poheader
poheaderx = wa_poheaderx
IMPORTING
exppurchaseorder = v_ebeln
expheader = ponum
* EXPPOEXPIMPHEADER =
TABLES
return = ti_bapiret2
poitem = ti_poitem
poitemx = ti_poitemx.

IF sy-subrc = 0.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
ENDIF.





DATA: file TYPE rlgrap-filename.

DATA: it_xcel TYPE TABLE OF alsmex_tabline,

wa_xcel TYPE alsmex_tabline.

file = p_file.

CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
filename = file
i_begin_col = '1'
i_begin_row = '2'
i_end_col = '35'
i_end_row = '65536'
TABLES
intern = it_xcel
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3.

LOOP AT it_xcel INTO wa_xcel.

CASE

wa_xcel-col.

WHEN '0001'.

wa_final-comp_code = wa_xcel-value.

WHEN '0002'.

wa_final-item_intvl = wa_xcel-value.

WHEN '0003'.

wa_final-vendor = wa_xcel-value.

WHEN '0004'.

wa_final-pmnttrms = wa_xcel-value.

WHEN '0005'.

wa_final-purch_org = wa_xcel-value.

WHEN '0006'.

wa_final-pur_group = wa_xcel-value.

WHEN '0007'.

wa_final-currency = wa_xcel-value.

WHEN '0008'.

wa_final-po_item = wa_xcel-value.

WHEN '0009'.

wa_final-material = wa_xcel-value.

WHEN '00010'.

wa_final-plant = wa_xcel-value.

WHEN '00011'.

wa_final-stge_loc = wa_xcel-value.

WHEN '00012'.

wa_final-quantity = wa_xcel-value.

WHEN '00013'.

wa_final-tax_code = wa_xcel-value.

WHEN '00014'.

wa_final-item_cat = wa_xcel-value.

WHEN '00015'.

wa_final-acctasscat = wa_xcel-value.

WHEN '00016'.

wa_final-sched_line = wa_xcel-value.

WHEN '00017'.

wa_final-delivery_date = wa_xcel-value.

WHEN '00018'.

wa_final-serial_no = wa_xcel-value.

WHEN '00019'.

wa_final-gl_account = wa_xcel-value.

WHEN '00020'.

wa_final-costcenter = wa_xcel-value.

WHEN '00021'.

wa_final-co_area = wa_xcel-value.

ENDCASE.

AT END OF row.

APPEND wa_final TO ti_final.

CLEAR wa_final.

ENDAT.


ENDLOOP.





DATA: obj_alv TYPE REF TO cl_salv_table,
lx_msg TYPE REF TO cx_salv_msg.


TRY.
CALL METHOD cl_salv_table=>factory
IMPORTING
r_salv_table = obj_alv
CHANGING
t_table = ti_final.


CATCH cx_salv_msg INTO lx_msg.

ENDTRY.



DATA(lo_functions) = obj_alv->get_functions( ).
lo_functions->set_all( abap_true ).

obj_alv->display( ).
4 REPLIES 4

former_member736527
Active Participant
0 Kudos
1,339

If your POs are not getting created, that means your BAPI is failing. Put a breakpoint on the BAPI, and see what you get in the return structure TI_BAPIRET2.

Also, it seems that your code structure is wrong. You want to put the Excel file reading above the BAPI. And you would also need to put the file data into the BAPI structures instead of EKKO-.

FredericGirod
Active Contributor
1,339
  CALL FUNCTION 'BAPI_PO_CREATE1'
EXPORTING
poheader = wa_poheader
poheaderx = wa_poheaderx
IMPORTING
exppurchaseorder = v_ebeln
expheader = ponum
* EXPPOEXPIMPHEADER =
TABLES
return = ti_bapiret2
poitem = ti_poitem
poitemx = ti_poitemx.

IF sy-subrc = 0.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.

Where is the SY-SUBRC returning value of your BAPI ? Didn't you forget to manage the RETURN tables from this BAPI ?

raymond_giuseppi
Active Contributor
1,339
  • You fill the BAPI parameter and called it BEFORE reading the Excel Sheet
  • Youd didn't LOOP the RETURN table for an error message (and set sy-subrc)

Could you modularize your program, create a local class, a method to read Excel, a method to copy the read data to the BAPI parameters, one to display the read detail and the success or error messages, etc.

Sandra_Rossi
Active Contributor
0 Kudos
1,339

What Frederic means is that if you don't indicate EXCEPTIONS with CALL FUNCTION, sy-subrc has an undetermined value. (see official ABAP documentation)