08-29-2007 11:53 AM
Hi Folks,
I want to create a PO using BAPI. Which BAPI we are suppose to use and what are the mandatory fields for the same?
Sample code will be helpful.
Thanks
Siddarth
08-29-2007 12:01 PM
Hi,
&----
*& Report ZBAPI_CREATE_PO *
*& *
&----
*& Program demonstrates the BAPI call to create Purchase Order *
*& Minimum required parameters are used are as per the current *
*& system configuration *
&----
REPORT ZBAPI_CREATE_PO .
&----
*DATA DECLARATION
CONSTANTS : C_X VALUE 'X'.
*Structures to hold PO header data
DATA : HEADER LIKE BAPIMEPOHEADER ,
HEADERX LIKE BAPIMEPOHEADERX .
*Internal Tables to hold PO ITEM DATA
DATA : ITEM LIKE BAPIMEPOITEM OCCURS 0 WITH HEADER LINE,
ITEMX LIKE BAPIMEPOITEMX OCCURS 0 WITH HEADER LINE,
*Internal table to hold messages from BAPI call
RETURN LIKE BAPIRET2 OCCURS 0 WITH HEADER LINE.
data : w_header(40) value 'PO Header'.
data : ws_langu like sy-langu.
*text-001 = 'PO Header' - define as text element
selection-screen begin of block b1 with frame title text-001.
parameters : company like header-comp_code default '2700' ,
doctyp like HEADER-DOC_TYPE default 'NB' ,
cdate like HEADER-CREAT_DATE default sy-datum ,
vendor like HEADER-VENDOR default '0010000023',
pur_org like HEADER-PURCH_ORG default '2700' ,
pur_grp like HEADER-PUR_GROUP default '001' .
selection-screen end of block b1.
selection-screen begin of block b2 with frame title text-002.
parameters : item_num like ITEM-PO_ITEM default '00001',
material like ITEM-MATERIAL default 'CRANE' ,
plant like ITEM-PLANT default '2700' ,
quantity like ITEM-QUANTITY default 100.
selection-screen end of block b2.
&----
START-OF-SELECTION.
&----
*DATA POPULATION
&----
ws_langu = sy-langu. "Language variable
*POPULATE HEADER DATA FOR PO
HEADER-COMP_CODE = company .
HEADER-DOC_TYPE = doctyp .
HEADER-CREAT_DATE = cdate .
HEADER-VENDOR = vendor .
HEADER-LANGU = ws_langu .
HEADER-PURCH_ORG = pur_org .
HEADER-PUR_GROUP = pur_grp .
&----
*POPULATE HEADER FLAG.
&----
HEADERX-comp_code = c_x.
HEADERX-doc_type = c_x.
HEADERX-creat_date = c_x.
HEADERX-vendor = c_x.
HEADERX-langu = c_x.
HEADERX-purch_org = c_x.
HEADERX-pur_group = c_x.
HEADERX-doc_date = c_x.
&----
*POPULATE ITEM DATA.
&----
ITEM-PO_ITEM = item_num.
ITEM-MATERIAL = material.
ITEM-PLANT = plant.
ITEM-QUANTITY = quantity.
APPEND ITEM.
&----
*POPULATE ITEM FLAG TABLE
&----
ITEMX-PO_ITEM = item_num.
ITEMX-MATERIAL = C_X.
ITEMX-PLANT = C_X .
ITEMX-STGE_LOC = C_X .
ITEMX-QUANTITY = C_X .
ITEMX-TAX_CODE = C_X .
ITEMX-ITEM_CAT = C_X .
ITEMX-ACCTASSCAT = C_X .
APPEND ITEMX.
&----
*BAPI CALL
&----
CALL FUNCTION 'BAPI_PO_CREATE1'
EXPORTING
POHEADER = HEADER
POHEADERX = HEADERX
POADDRVENDOR =
TESTRUN =
IMPORTING
EXPPURCHASEORDER =
EXPHEADER =
EXPPOEXPIMPHEADER =
TABLES
RETURN = RETURN
POITEM = ITEM
POITEMX = ITEMX.
&----
*Confirm the document creation by calling database COMMIT
&----
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'
IMPORTING
RETURN =
.
end-of-selection.
&----
*Output the messages returned from BAPI call
&----
LOOP AT RETURN.
WRITE / RETURN-MESSAGE.
ENDLOOP.
________________________________________________________________
Create a purchase order with configuration using the BAPI
This took some time to figure out how to setup. It may be useful.
call function 'BAPI_CFG_CREATE'
exporting
object_id = 'MYMATCODE'
root_type = 'MARA'
root_name = 'MYMATCODE'
plant = 'MYPLANT'
importing
cfg_handle = l_handle
root_instance = l_instance
exceptions
error = 1
others = 2.
l_characts_vals-atnam = 'WIDTH'.
l_characts_vals-atwtb = '500'.
append l_characts_vals to it_characts_vals.
call function 'BAPI_CFGINST_CHARCS_VALS_SET'
exporting
object_id = 'MYMATCODE'
instance = l_instance
tables
characts_vals = it_characts_vals
characts_vals_err = it_characts_vals_err.
call function 'CE_C_SET_CBASE'.
...
l_poitem-int_obj_no = l_handle.
...
call function 'BAPI_PO_CREATE1'
exporting
poheader = l_poheader
poheaderx = l_poheaderx
importing
expheader = l_expheader
exppurchaseorder = l_exppurchaseorder
tables
poitem = lt_poitem
poitemx = lt_poitemx
poschedule = lt_poschedule
poschedulex = lt_poschedulex
return = lt_return.
call function 'BAPI_TRANSACTION_COMMIT'.
Regards
Sudheer
08-29-2007 11:56 AM
Hi Siddarth,
The BAPI for PO create is BAPI_PO_CREATE or BAPI_PO_CREATE1 for enjoy transaction.
Companycode, PO number, Vendor, Document type, Purchasing organization, Purchasing group, currency are few important fields to the export parameter POHEADER and POHEADERX.
Thanks,
Vinay
08-29-2007 12:00 PM
Hi Vinay,
What is the difference between both the BAPIs. you mentioned something "enjoy transaction" what is that??
thanks
Siddarth
08-29-2007 12:34 PM
Hi,
Function module BAPI_PO_CREATE1 enables you to create purchase orders. The BAPI uses the technology behind the Enjoy purchase order transaction
( ME21N).
Alternatively, the IDoc type PORDCR1 is available. The data from this IDoc automatically populates the interface parameters of the function module BAPI_PO_CREATE1.
The following authorization objects are checked when an Enjoy purchase order is created (activity 01):
M_BEST_BSA (document type in PO)
M_BEST_EKG (purchasing group in PO)
M_BEST_EKO (purchasing organization in PO)
M_BEST_WRK (plant in PO)
reward if helpful.
thanks
08-29-2007 11:58 AM
Hi,
use FM BAPI_PO_CREATE and check out th documentation for mandatory fields.
Regards.
08-29-2007 12:01 PM
Hi,
&----
*& Report ZBAPI_CREATE_PO *
*& *
&----
*& Program demonstrates the BAPI call to create Purchase Order *
*& Minimum required parameters are used are as per the current *
*& system configuration *
&----
REPORT ZBAPI_CREATE_PO .
&----
*DATA DECLARATION
CONSTANTS : C_X VALUE 'X'.
*Structures to hold PO header data
DATA : HEADER LIKE BAPIMEPOHEADER ,
HEADERX LIKE BAPIMEPOHEADERX .
*Internal Tables to hold PO ITEM DATA
DATA : ITEM LIKE BAPIMEPOITEM OCCURS 0 WITH HEADER LINE,
ITEMX LIKE BAPIMEPOITEMX OCCURS 0 WITH HEADER LINE,
*Internal table to hold messages from BAPI call
RETURN LIKE BAPIRET2 OCCURS 0 WITH HEADER LINE.
data : w_header(40) value 'PO Header'.
data : ws_langu like sy-langu.
*text-001 = 'PO Header' - define as text element
selection-screen begin of block b1 with frame title text-001.
parameters : company like header-comp_code default '2700' ,
doctyp like HEADER-DOC_TYPE default 'NB' ,
cdate like HEADER-CREAT_DATE default sy-datum ,
vendor like HEADER-VENDOR default '0010000023',
pur_org like HEADER-PURCH_ORG default '2700' ,
pur_grp like HEADER-PUR_GROUP default '001' .
selection-screen end of block b1.
selection-screen begin of block b2 with frame title text-002.
parameters : item_num like ITEM-PO_ITEM default '00001',
material like ITEM-MATERIAL default 'CRANE' ,
plant like ITEM-PLANT default '2700' ,
quantity like ITEM-QUANTITY default 100.
selection-screen end of block b2.
&----
START-OF-SELECTION.
&----
*DATA POPULATION
&----
ws_langu = sy-langu. "Language variable
*POPULATE HEADER DATA FOR PO
HEADER-COMP_CODE = company .
HEADER-DOC_TYPE = doctyp .
HEADER-CREAT_DATE = cdate .
HEADER-VENDOR = vendor .
HEADER-LANGU = ws_langu .
HEADER-PURCH_ORG = pur_org .
HEADER-PUR_GROUP = pur_grp .
&----
*POPULATE HEADER FLAG.
&----
HEADERX-comp_code = c_x.
HEADERX-doc_type = c_x.
HEADERX-creat_date = c_x.
HEADERX-vendor = c_x.
HEADERX-langu = c_x.
HEADERX-purch_org = c_x.
HEADERX-pur_group = c_x.
HEADERX-doc_date = c_x.
&----
*POPULATE ITEM DATA.
&----
ITEM-PO_ITEM = item_num.
ITEM-MATERIAL = material.
ITEM-PLANT = plant.
ITEM-QUANTITY = quantity.
APPEND ITEM.
&----
*POPULATE ITEM FLAG TABLE
&----
ITEMX-PO_ITEM = item_num.
ITEMX-MATERIAL = C_X.
ITEMX-PLANT = C_X .
ITEMX-STGE_LOC = C_X .
ITEMX-QUANTITY = C_X .
ITEMX-TAX_CODE = C_X .
ITEMX-ITEM_CAT = C_X .
ITEMX-ACCTASSCAT = C_X .
APPEND ITEMX.
&----
*BAPI CALL
&----
CALL FUNCTION 'BAPI_PO_CREATE1'
EXPORTING
POHEADER = HEADER
POHEADERX = HEADERX
POADDRVENDOR =
TESTRUN =
IMPORTING
EXPPURCHASEORDER =
EXPHEADER =
EXPPOEXPIMPHEADER =
TABLES
RETURN = RETURN
POITEM = ITEM
POITEMX = ITEMX.
&----
*Confirm the document creation by calling database COMMIT
&----
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'
IMPORTING
RETURN =
.
end-of-selection.
&----
*Output the messages returned from BAPI call
&----
LOOP AT RETURN.
WRITE / RETURN-MESSAGE.
ENDLOOP.
________________________________________________________________
Create a purchase order with configuration using the BAPI
This took some time to figure out how to setup. It may be useful.
call function 'BAPI_CFG_CREATE'
exporting
object_id = 'MYMATCODE'
root_type = 'MARA'
root_name = 'MYMATCODE'
plant = 'MYPLANT'
importing
cfg_handle = l_handle
root_instance = l_instance
exceptions
error = 1
others = 2.
l_characts_vals-atnam = 'WIDTH'.
l_characts_vals-atwtb = '500'.
append l_characts_vals to it_characts_vals.
call function 'BAPI_CFGINST_CHARCS_VALS_SET'
exporting
object_id = 'MYMATCODE'
instance = l_instance
tables
characts_vals = it_characts_vals
characts_vals_err = it_characts_vals_err.
call function 'CE_C_SET_CBASE'.
...
l_poitem-int_obj_no = l_handle.
...
call function 'BAPI_PO_CREATE1'
exporting
poheader = l_poheader
poheaderx = l_poheaderx
importing
expheader = l_expheader
exppurchaseorder = l_exppurchaseorder
tables
poitem = lt_poitem
poitemx = lt_poitemx
poschedule = lt_poschedule
poschedulex = lt_poschedulex
return = lt_return.
call function 'BAPI_TRANSACTION_COMMIT'.
Regards
Sudheer
08-29-2007 12:49 PM
see these PO related BApi's
<b>BAPI_PO_CREATE Create Purchase Order</b>
BAPI_PO_GETDETAIL Display Purchase Order Details
BAPI_PO_GETITEMS List Purchase Order Items
BAPI_PO_GETITEMSREL List Purchase Orders for Release (Approval): New as of 4.0A
BAPI_PO_GETRELINFO Display Detailed Release (Approval) Information on Purchase Order
BAPI_PO_GET_LIST List Purchase Orders - Only up to 4.0A
regards,
srinivas
<b>*reward for useful answers*</b>