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 Problem......

Former Member
0 Likes
894

Hi Gurus,

I am working on a BAPI to create purchase order and for this I have written the follwoing code:

DATA: poheader LIKE bapimepoheader,

poheaderx LIKE bapimepoheaderx,

poitem LIKE bapimepoitem OCCURS 0 WITH HEADER LINE,

poitemx LIKE bapimepoitemx OCCURS 0 WITH HEADER LINE,

return LIKE bapiret2 OCCURS 0 WITH HEADER LINE,

return2 LIKE bapiret2 OCCURS 0 WITH HEADER LINE,

exppurchaseorder LIKE bapimepoheader-po_number.

DATA: BEGIN OF itab OCCURS 0,

ref_no(2),

vend_no(10),

material(18),

Quantity(13),

Price(10),

ord_unit(7),

Plant(4),

Strg_loc(4),

purch_grp(4),

purch_org(4),

c_code(4),

doc_typ(4),

END OF itab.

Data: it_ref_no_old(2) type c,

it_record like line of itab,

it_record_x like line of itab.

************************************************************************

    • Definition of Variables *

************************************************************************

data: v_semfile like RLGRAP-FILENAME.

************************************************************************

  • Selection Screen *

************************************************************************

selection-screen begin of block b1 with frame title text-001.

parameters: p_ifname like rlgrap-filename obligatory.

selection-screen end of block b1.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_ifname.

CALL FUNCTION 'F4_FILENAME' "allows user to select path/file

EXPORTING

program_name = 'Z_PO_Creation'

dynpro_number = syst-dynnr

field_name = 'p_ifname'

IMPORTING

file_name = p_ifname.

************************************************************************

  • START-OF-SELECTION *

************************************************************************

start-of-selection.

perform get_data.

perform data_process.

end-of-selection.

&----


*& 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 = itab

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 call_bapi

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM call_bapi .

LOOP AT itab.

  • moving header data.

MOVE: itab-vend_no TO poheader-vendor,

  • itab-doc_typ TO poheader-doc_type,

itab-purch_org TO poheader-purch_org,

itab-purch_grp TO poheader-pur_group,

itab-c_code TO poheader-comp_code.

  • updating header data.

poheaderx-vendor = 'X'.

  • poheaderx-doc_type = 'X'.

poheaderx-purch_org = 'X'.

poheaderx-pur_group = 'X'.

poheaderx-comp_code = 'X'.

  • moving item data.

MOVE: itab-plant TO poitem-plant,

itab-Strg_loc TO poitem-stge_loc,

itab-material TO poitem-material,

itab-quantity TO poitem-quantity,

itab-price TO poitem-price_unit,

itab-ref_no TO poitem-ref_doc,

itab-ord_unit TO poitem-po_unit.

  • updating Item data.

poitemx-plant = 'X'.

poitemx-stge_loc = 'X'.

poitemx-material = 'X'.

poitemx-quantity = 'X'.

poitem-price_unit = 'X'.

poitem-ref_doc = 'X'.

poitem-po_unit = 'X'.

APPEND: poitem, poitemx.

CALL FUNCTION 'BAPI_PO_CREATE1'

EXPORTING

poheader = poheader

poheaderx = poheaderx

IMPORTING

exppurchaseorder = exppurchaseorder

TABLES

poitem = poitem

poitemx = poitemx

return = return.

CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

IMPORTING

return = return2.

write:/ exppurchaseorder.

ENDLOOP.

ENDFORM. " call_bapi

&----


*& Form data_process

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM data_process .

sort itab by ref_no.

clear: it_ref_no_old.

loop at itab into it_record.

if it_record-ref_no ne it_ref_no_old .

loop AT itab 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.

ENDFORM. " data_process

and my text file looks like this:

the sequence of the fileds are:

Ref no., Vendor, material,Quantity, Price, Ord_unit, Plant,

Strg_loc, purch_org, purch_grp,c_code, doc_typ

1 101 123 100 10 car 101 121 114 112 222 NB

1 101 234 100 10 buc 101 121 114 112 222 NB

1 101 567 100 10 car 101 121 114 112 222 NB

2 102 123 100 10 car 101 121 114 112 222 NB

2 102 567 100 10 car 101 121 114 112 222 NB

3 103 234 100 10 buc 101 121 114 112 222 NB

3 103 567 100 10 car 101 121 114 112 222 NB

3 103 123 100 10 car 101 121 114 112 222 NB

4 104 567 100 10 car 101 121 114 112 222 NB

To me code looks perfectly fine but somehow its not creating the purchse order, can you please help me to get out of this issue.

Thanks

Rajeev Gupta

7 REPLIES 7
Read only

Former Member
0 Likes
848

HI,

Whats the return value you are getting from the BAPI ?

Regards

Kannaiah

Read only

0 Likes
848

Hi Kannaiah,

Thanks for the reply and I am getting the following retun value:

E |MEPO |085 |Check item number 0 in table POITEM

E |MEPO |085 |Check item number 0 in table POITEMX

Thanks

Rajeev Gupta

Read only

0 Likes
848

You have to create the item number (poitem-po_item).

rob

Read only

0 Likes
848

Like:

FORM call_bapi .

  DATA item(5) TYPE n.                  "<=============

  LOOP AT itab.

    item = item + 1.                    "<=============

* moving header data.
    MOVE: itab-vend_no TO poheader-vendor,
* itab-doc_typ TO poheader-doc_type,
    itab-purch_org TO poheader-purch_org,
    itab-purch_grp TO poheader-pur_group,
    itab-c_code TO poheader-comp_code.

* updating header data.
    poheaderx-vendor = 'X'.
* poheaderx-doc_type = 'X'.
    poheaderx-purch_org = 'X'.
    poheaderx-pur_group = 'X'.
    poheaderx-comp_code = 'X'.

* moving item data.

    MOVE item TO poitem-po_item.          "<=============

    MOVE: itab-plant TO poitem-plant,
    itab-strg_loc TO poitem-stge_loc,
    itab-material TO poitem-material,
    itab-quantity TO poitem-quantity,
    itab-price TO poitem-price_unit,
    itab-ref_no TO poitem-ref_doc,
    itab-ord_unit TO poitem-po_unit.

* updating Item data.

    MOVE item TO poitemx-po_item.         "<=============

    poitemx-plant = 'X'.
    poitemx-stge_loc = 'X'.
    poitemx-material = 'X'.
    poitemx-quantity = 'X'.
    poitem-price_unit = 'X'.
    poitem-ref_doc = 'X'.
    poitem-po_unit = 'X'.

    APPEND: poitem, poitemx.

    CALL FUNCTION 'BAPI_PO_CREATE1'
      EXPORTING
        poheader         = poheader
        poheaderx        = poheaderx
      IMPORTING
        exppurchaseorder = exppurchaseorder
      TABLES
        poitem           = poitem
        poitemx          = poitemx
        return           = return.

    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      IMPORTING
        return = return2.
    WRITE:/ exppurchaseorder.
  ENDLOOP.
ENDFORM. " call_bapi

And after the BAPI call, you ought to loop through the return table to see the results. Not doing that made you miss the current error.

Rob

Message was edited by:

Rob Burbank

Read only

0 Likes
848

Thanks for the reply Rob, now I modified my code and it looks like this:

*

&----


*& Report ZMMINT_PO_CONVERSION

*&

&----


*&

*&

&----


REPORT ZMMINT_PO_CONVERSION.

************************************************************************

  • internal table to store the data

************************************************************************

DATA: poheader LIKE bapimepoheader,

poheaderx LIKE bapimepoheaderx,

poitem LIKE bapimepoitem OCCURS 0 WITH HEADER LINE,

poitemx LIKE bapimepoitemx OCCURS 0 WITH HEADER LINE,

return LIKE bapiret2 OCCURS 0 WITH HEADER LINE,

return2 LIKE bapiret2 OCCURS 0 WITH HEADER LINE,

exppurchaseorder LIKE bapimepoheader-po_number,

poschedule LIKE bapimeposchedule OCCURS 0 WITH HEADER LINE,

poschedulex LIKE bapimeposchedulx OCCURS 0 WITH HEADER LINE.

DATA: BEGIN OF itab OCCURS 0,

item_no(3),

ref_no(2),

vend_no(10),

material(18),

Quantity(13),

Price(10),

ord_unit(7),

Plant(4),

Strg_loc(4),

purch_grp(4),

purch_org(4),

c_code(4),

doc_typ(4),

del_date(10),

END OF itab.

Data: it_ref_no_old(2) type c,

it_record like line of itab,

it_record_x like line of itab.

************************************************************************

    • Definition of Variables *

************************************************************************

data: v_semfile like RLGRAP-FILENAME.

************************************************************************

  • Selection Screen *

************************************************************************

selection-screen begin of block b1 with frame title text-001.

parameters: p_ifname like rlgrap-filename obligatory.

selection-screen end of block b1.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_ifname.

CALL FUNCTION 'F4_FILENAME' "allows user to select path/file

EXPORTING

program_name = 'Z_PO_Creation'

dynpro_number = syst-dynnr

field_name = 'p_ifname'

IMPORTING

file_name = p_ifname.

************************************************************************

  • START-OF-SELECTION *

************************************************************************

start-of-selection.

perform get_data.

perform data_process.

end-of-selection.

&----


*& 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 = itab

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 call_bapi

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM call_bapi .

LOOP AT itab.

  • moving header data.

MOVE: itab-vend_no TO poheader-vendor,

itab-doc_typ TO poheader-doc_type,

itab-purch_org TO poheader-purch_org,

itab-purch_grp TO poheader-pur_group,

itab-c_code TO poheader-comp_code.

  • updating header data.

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: itab-item_no TO poitem-po_item,

itab-plant TO poitem-plant,

itab-Strg_loc TO poitem-stge_loc,

itab-material TO poitem-material,

itab-quantity TO poitem-quantity,

itab-price TO poitem-price_unit,

itab-ref_no TO poitem-ref_doc,

itab-ord_unit TO poitem-po_unit.

  • updating Item data.

poitemx-po_item = 'X'. "itab-item_no.

poitemx-plant = 'X'.

poitemx-stge_loc = 'X'.

poitemx-material = 'X'.

poitemx-quantity = 'X'.

move: itab-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

&----


*& Form data_process

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM data_process .

sort itab by ref_no.

clear: it_ref_no_old.

loop at itab into it_record.

if it_record-ref_no ne it_ref_no_old .

loop AT itab 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.

ENDFORM. " data_process

but still I am kind some errors, return value are as follows:

No instance of object type PurchaseOrder has been created. External reference:

Please enter material number or account assignment category

PO header data still faulty

Can you please help me out. I need to submit this thing by the end of the day.

Thanks

Rajeev Gupta

Read only

0 Likes
848

You should talk to your business analyst about this.

Rob

Read only

Former Member
0 Likes
848

Hi

  • updating Item data.

poitemx-plant = 'X'.

poitemx-stge_loc = 'X'.

poitemx-material = 'X'.

poitemx-quantity = 'X'.

poitem-price_unit = 'X'.

poitem-ref_doc = 'X'.

poitem-po_unit = 'X'.

it should be poitemx-price_unit, same for ref_doc and po_unit

Pls check and also i think you should pass document type, which you have commented

Regards

MD