Application Development and Automation 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: 
Read only

BAPI_PO_Create

Former Member
0 Likes
2,200

Hi all,

I am new to abap and want to create BAPI for creating purchase order. I know which bapi to use but how do i start working on it. Plz give step by step process.

Your help will be appreciated and rewarded accordingly.

Regards,

Poonam

9 REPLIES 9
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,135

Megha, here is some sample code to help you get started using the BAPI. Of course, you will have to substitute valid values for a couple variables.



  constants : c_x value 'X'.

  data: del_date type sy-datum.

  data: pohead  type bapimepoheader.
  data: poheadx type bapimepoheaderx.

  data: exp_head type bapimepoheader.

  data: return  type table of bapiret2 with header line.
  data: poitem  type table of bapimepoitem with header line.
  data: poitemx type table of bapimepoitemx with header line.

  data: posched  type table of bapimeposchedule with header line.
  data: poschedx type table of bapimeposchedulx with header line.

  data: ex_po_number type bapimepoheader-po_number.


* Header Level Data

  pohead-comp_code = '0010'.
  pohead-doc_type   = 'NB'     .
  pohead-creat_date = sy-datum   .

  pohead-vendor = '0000012345'. 
  pohead-purch_org = '1000'.
  pohead-pur_group = 'EMG'.

  pohead-langu      = sy-langu   .
  pohead-doc_date   = sy-datum.

  poheadx-comp_code  = c_x.
  poheadx-doc_type   = c_x.
  poheadx-creat_date = c_x.
  poheadx-vendor     = c_x.
  poheadx-langu      = c_x.
  poheadx-purch_org  = c_x.
  poheadx-pur_group  = c_x.
  poheadx-doc_date   = c_x.


* Item Level Data
  poitem-po_item  = 1.
  poitem-material = '000000000040001000'.
  poitem-plant    = '0006'.
  poitem-stge_loc = '6000'.
  poitem-quantity = 1.
  append poitem.

  poitemx-po_item    = 1.
  poitemx-po_itemx   = c_x.
  poitemx-material   = c_x.
  poitemx-plant      = c_x .
  poitemx-stge_loc   = c_x .
  poitemx-quantity   = c_x .
  poitemx-tax_code   = c_x .
  poitemx-item_cat   = c_x .
  poitemx-acctasscat = c_x .
  append poitemx.


* Schedule Line Level Data
  posched-po_item        = 1.
  posched-sched_line     = 1.
  posched-del_datcat_ext = 'D'.
  del_date = sy-datum + 1.
  write del_date to posched-delivery_date.
  posched-deliv_time     = '000001'.
  posched-quantity       = 1.
  append posched.

  poschedx-po_item        = 1.
  poschedx-sched_line     = 1.
  poschedx-po_itemx       = c_x.
  poschedx-sched_linex    = c_x.
  poschedx-del_datcat_ext = c_x.
  poschedx-delivery_date  = c_x.
  poschedx-quantity       = c_x.
  append poschedx.

  call function 'BAPI_PO_CREATE1'
       EXPORTING
            poheader         = pohead
            poheaderx        = poheadx
            testrun          = ' '
       IMPORTING
            exppurchaseorder = ex_po_number
            expheader        = exp_head
       TABLES
            return           = return
            poitem           = poitem
            poitemx          = poitemx
            poschedule       = posched
            poschedulex      = poschedx.

  call function 'BAPI_TRANSACTION_COMMIT'
       EXPORTING
            wait = 'X'.

  if not ex_po_number is initial.
    call function 'DEQUEUE_ALL'.
  else.
    call function 'DEQUEUE_ALL'.
    message i036.
  endif.

Regards,

Rich Heilman

Read only

Former Member
0 Likes
1,135

Hi Poonam,

Do you have any PO data file with you and upload it into SAP using BAPI, then first goto transaction BAPI to find the suitable BAPI.

Material Managment->Purchasing->Purchase Order, select the CreateFromData1 and click on it. Now in right hand side you can see function module BAPI_PO_CREATE1. double click on it.

Check all Import, Export, Changing & Tables parameters for mandatory parameters which you should pass while executing the function module(You can easily identiy by seeing Optional tab whether it is optional/mandatory).

*

You need to find the required fields import, export, tables parameters for which you need to pass data. For example Company code field you can find in import tab and in POHEADER parameter.

You need to declare the variables with reference to BAPI datatypes. For example POHEADER's reference data type is BAPIMEPOHEADER. Declare one variable with reference to BAPIMEPOHEADER to assign Company code(which is from file or which needs to be uploaded into SAP).

ex: data: PO_HEAD like BAPIMEPOHEADER.

and you assign the data to the variable.

ex: PO_HEAD-COMP_CODE = < >.

If you have any internal tables to be passed to function module declare the internal tables and adde the entries into internal tables. And you must need to pass return table.

*

Call the function module and pass all variables(declared by you) to the relevant parameters.

I hope this will help you. or you refer include LBBP_BAPI_POF20 for futher understanding.

Regards

Mohammad.

Read only

Former Member
0 Likes
1,135

Hi Rich,

Thanks a lot for your help.

I am modifying this cord according to my need but few things i am not clear about.

1. constants : c_x value 'X'.

2. is what does bapimepoitemx stand for is it item or

thanks in adv

megha

Read only

Former Member
0 Likes
1,135

Hi,

The structure BAPIMEPOITEMX indicates which fields must be modified/update. The required fields are marked here with an 'X' and the values must be passed in the structure BAPIMEPOITEM.

One small change, I would like to suggest to the code sample provided by Rich: Before calling the BAPI_TRANSACTION_COMMIT, check for the PO number in field "ex_po_number". If not, call BAPI_TRANSACTION_ROLLBACK.

Sample Code:


 IF ex_po_number IS INITIAL.
  call function 'BAPI_TRANSACTION_ROLLBACK'
 ELSE.
  call function 'BAPI_TRANSACTION_COMMIT'
       EXPORTING
            wait = 'X'.
 ENDIF.

Regards,

Sumant.

Message was edited by: Sumant Sura

Read only

0 Likes
1,135

Sumant is correct in his answer and also about the ROLLBACK. Good call, Sumant. About the constant, you can just substitute an 'X' for this, you don't need to use a constant to move to the fields.

Instead of this...


poschedx-po_itemx       = c_x.

You can just do this.....

poschedx-po_itemx       = 'X'.

Regards,

Rich Heilman

Read only

Former Member
0 Likes
1,135

Hi Rich,

i am getting my data for bapi from ztable.

Data: itab like ztable occurs 0.

select <fields reqd header data and item data> from ztable into itab.

is this right.

and after that as rich suggested sld write code for header level and item level.

But where should i loop the structure. i am not exactly clear with what to do.

please help.

Regards,

Poonam

Read only

0 Likes
1,135

Hi amol,

I suggest you do it as follow:

1.Get PO header data from a zstructure or ztable.

2.Get PO item data from ztable1,loop ztable1,use condition ztable1-ebeln = ztable-ebeln.

3.If needed,get schedule line data from ztable2,loop ztable2,use condition ztable2-ebeln = ztable1-ebeln and ztable2-ebelp = ztable1-ebelp.

4.Transfer these data to BAPI.

Read only

Former Member
0 Likes
1,135

hi poonam,

I saw ur question regarding bapi po create .I am creating PO using FM BAPI_PO_CREATE1. It works fine when I am taking one line item but it don't work for more then one line item.Can u do one favour for Me plz send me sample code for this and format of ur external file like text file in which ur data is present. Can u tell me ur making a transperent table first to upload data then send to internal table. Plz tell me I shall be very thankful to u.

mail me on hem_libra@rediffmail.com

waiting for reply

regards,

hemlata

Read only

Former Member
0 Likes
1,135

Somehow I got the similar problem too, but even more interesting.

When I call the BAPI_PO_CREATE1 to create a PO with single item, it works fine. However, when I try to create with more than one item, I got "Document contains no data".

I try to understand the problem and so I create a loop to call the BAPI_PO_CREATE1 couple times, I found out if the first call of the BAPI to create a PO with single item only, then the later call can create PO with multiple items.

If, the first call is a PO with multiple items, then all call of BAPI will become "Document contains no data".

Since I don't know what happen, I end up use the old verison, BAPI_PO_CREATE.