Sub topic 8
Creating and Managing Purchase Orders
This chapter assumes you know the purchasing organizational units (company code, plant, purchasing organization, purchasing group), the supplier Business Partner, and the material master. It also assumes you have read the chapters on info records and purchase requisitions.
What a purchase order does
A purchase order (PO) is a binding request to a supplier to deliver materials or services on stated terms. It is the central document of procure-to-pay. Goods receipt, invoice verification and the PO history all refer back to it.
A PO has a header and one or more items. The header holds the supplier, company code, purchasing organization, purchasing group and document type (for example NB, the standard PO). Each item holds the material, plant, quantity, price and account assignment. Each item has at least one schedule line, which gives the delivery date and quantity.
Ways to create a PO
- Manually, with transaction ME21N or the Fiori app Manage Purchase Orders.
- From a purchase requisition, by converting it with the Fiori app Manage Purchase Requisitions or the app Create Purchase Orders – Automatically. Source determination (info record or contract) supplies the supplier and price.
- From a contract, as a release order that refers to the contract.
- Through a program, with BAPIPOCREATE1 or the OData API.
Worked example: creating a PO with a BAPI
BAPIPOCREATE1 uses paired structures. For every data structure, such as POHEADER, there is a matching "X" structure, such as POHEADERX. A field is only processed if you also set its flag in the "X" structure. A field you forget to flag is silently ignored.
DATA: lv_ebeln TYPE bapimepoheader-po_number,
lt_return TYPE STANDARD TABLE OF bapiret2 WITH EMPTY KEY,
lt_item TYPE STANDARD TABLE OF bapimepoitem WITH EMPTY KEY,
lt_itemx TYPE STANDARD TABLE OF bapimepoitemx WITH EMPTY KEY,
lt_sched TYPE STANDARD TABLE OF bapimeposchedule WITH EMPTY KEY,
lt_schedx TYPE STANDARD TABLE OF bapimeposchedulx WITH EMPTY KEY.
" Values below are placeholders; use data that exists in your system.
DATA(ls_header) = VALUE bapimepoheader(
comp_code = '1710' doc_type = 'NB' vendor = '0017300001'
purch_org = '1710' pur_group = '001' ).
DATA(ls_headerx) = VALUE bapimepoheaderx(
comp_code = abap_true doc_type = abap_true vendor = abap_true
purch_org = abap_true pur_group = abap_true ).
lt_item = VALUE #( ( po_item = '00010' material = 'TG11' plant = '1710' quantity = 10 ) ).
lt_itemx = VALUE #( ( po_item = '00010' po_itemx = abap_true material = abap_true
plant = abap_true quantity = abap_true ) ).
lt_sched = VALUE #( ( po_item = '00010' sched_line = '0001'
delivery_date = sy-datum + 14 quantity = 10 ) ).
lt_schedx = VALUE #( ( po_item = '00010' sched_line = '0001' po_itemx = abap_true
sched_linex = abap_true delivery_date = abap_true quantity = abap_true ) ).
CALL FUNCTION 'BAPI_PO_CREATE1'
EXPORTING
poheader = ls_header
poheaderx = ls_headerx
IMPORTING
exppurchaseorder = lv_ebeln
TABLES
return = lt_return
poitem = lt_item
poitemx = lt_itemx
poschedule = lt_sched
poschedulex = lt_schedx.
IF line_exists( lt_return[ type = 'E' ] ) OR line_exists( lt_return[ type = 'A' ] ).
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = abap_true.
ENDIF.The BAPI does not save anything by itself. The PO exists only after BAPITRANSACTIONCOMMIT. The code checks RETURN for message types E (error) and A (abort) and rolls back if it finds one.
Managing a PO after creation
Changes happen in ME22N or the same Fiori app. Some changes have consequences:
- Changing quantity or price after goods receipt or invoice receipt is restricted. You cannot reduce the quantity below what was already delivered.
- If a release strategy applies, a relevant change can reset the release and send the PO for approval again.
- Deleting an item sets a deletion indicator. Closing it with the delivery completed indicator stops further goods receipts.
- Output (print, email or EDI) sends the PO to the supplier through message determination.
Common errors
| Symptom | Likely cause |
|---|---|
| BAPI returns no error but a field is empty | The matching field in the "X" structure was not set |
| PO number returned but the PO cannot be found | BAPI_TRANSACTION_COMMIT was not called |
| Item will not save | Missing account assignment, or the material is blocked for the plant |
| PO is blocked for goods receipt | The release strategy has not been completed |
Exercise
Your program calls BAPIPOCREATE1 and RETURN has no error messages, but the delivery date on the PO is empty. Name two things to check.
Show answer
First, check that the deliverydate field is set to abaptrue in POSCHEDULEX, together with the matching poitem and schedline keys. Second, check that the schedule line in POSCHEDULE has the same poitem and schedline values. Without the "X" flag the BAPI ignores the field and reports no error.