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

PO Creation

Former Member
0 Likes
1,260

Hi Gurus,

I am new to ABAP and right now I am working on an interface where I need to get the data from the text file and create PO and for this I have written the following 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(3),

purch_org(4),

c_code(3),

doc_typ(4),

END OF itab.

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

  • START-OF-SELECTION *

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

START-OF-SELECTION.

PERFORM get_data.

PERFORM call_bapi.

END-OF-SELECTION.

&----


*& Form get_data

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM get_data .

CALL FUNCTION 'WS_UPLOAD'

EXPORTING

filename = 'C:\SAP\purchaseorder_bapi'

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

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 my text file looks like this:

the sequence of the fileds are:

<b>Ref no., Vendor, material,Quantity, Price, Ord_unit, Plant,

Strg_loc, purch_org, purch_grp,c_code, doc_typ</b>

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

So basically my program should take care of all the aspects in the above text file,So basically on the basis of unique refrence number my program should create three PO's but as of now it is only taking care of ist item in the file. Can you please tell me how to take care of the multiple items.

Thanks

Rajeev Gupta

1 ACCEPTED SOLUTION
Read only

uwe_schieferstein
Active Contributor
0 Likes
1,223

Hello Rajeev

I assume that all records having the same REF NO should belong to the same purchase order.

DATA:
  ld_refno_old(2)  type c,
  ls_record          LIKE LINE OF itab,
  ls_record_x      LIKE LINE OF itab.

  SORT itab BY ref_no.

  CLEAR: ls_refno_old.
  LOOP AT itab INTO ls_record.

    IF ( ls_record-ref_no NE ld_refno_old ).
      LOOP AT itab INTO ls_record_x
                           WHERE ( ref_no = ls_record-ref_no ).
"       collect items of PO
      ENDLOOP.
      IF ( syst-subrc = 0 ).
"       create PO -> commit work
      ENDIF.
    ENDIF.

    ld_refno_old = ls_record-refno.  " update
  ENDLOOP.

I prefer to use this kind of logic instead of AT NEW or similar statements.

Regards

Uwe

Hi Gurus,

I am new to ABAP and right now I am working on an interface where I need to get the data from the text file and create PO and for this I have written the following 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(3),

purch_org(4),

c_code(3),

doc_typ(4),

END OF itab.

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

  • START-OF-SELECTION *

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

START-OF-SELECTION.

PERFORM get_data.

PERFORM call_bapi.

END-OF-SELECTION.

&----


*& Form get_data

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM get_data .

CALL FUNCTION 'WS_UPLOAD'

EXPORTING

filename = 'C:\SAP\purchaseorder_bapi'

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

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 my text file looks like this:

the sequence of the fileds are:

<b>Ref no., Vendor, material,Quantity, Price, Ord_unit, Plant,

Strg_loc, purch_org, purch_grp,c_code, doc_typ</b>

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

So basically my program should take care of all the aspects in the above text file,So basically on the basis of unique refrence number my program should create three PO's but as of now it is only taking care of ist item in the file. Can you please tell me how to take care of the multiple items.

Thanks

Rajeev Gupta

6 REPLIES 6
Read only

Former Member
0 Likes
1,223

Hi

I think you have to change the logic

Take the ITAB to different internal table ITAB1.

Sort itab1 by ref_no.

delete adjacent duplicates from itab1 comparing ref_no.

Loop at itab1.

read table itab with key ref_no = itab1-ref_no.

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

loop at itab where ref_no = itab1-ref_no.

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.

append poitem.

  • updating Item data.

poitemx-plant = 'X'.

poitemx-stge_loc = 'X'.

poitemx-material = 'X'.

poitemx-quantity = 'X'.

append poitemx.

endloop.

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

Refresh : poitem, poitemx.

endloop.

I hope this should work.

Let me know, if you need further information on the same

Regards

MD

Read only

uwe_schieferstein
Active Contributor
0 Likes
1,224

Hello Rajeev

I assume that all records having the same REF NO should belong to the same purchase order.

DATA:
  ld_refno_old(2)  type c,
  ls_record          LIKE LINE OF itab,
  ls_record_x      LIKE LINE OF itab.

  SORT itab BY ref_no.

  CLEAR: ls_refno_old.
  LOOP AT itab INTO ls_record.

    IF ( ls_record-ref_no NE ld_refno_old ).
      LOOP AT itab INTO ls_record_x
                           WHERE ( ref_no = ls_record-ref_no ).
"       collect items of PO
      ENDLOOP.
      IF ( syst-subrc = 0 ).
"       create PO -> commit work
      ENDIF.
    ENDIF.

    ld_refno_old = ls_record-refno.  " update
  ENDLOOP.

I prefer to use this kind of logic instead of AT NEW or similar statements.

Regards

Uwe

Read only

0 Likes
1,223

HI,

First of all thanks for your reply, as per your suggestion I have modified my code"

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

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(3),

purch_org(4),

c_code(3),

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,

.

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

  • 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-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'.

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.

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

So this should work for me right??

Thanks

Rajeev Gupta

Message was edited by:

Rajeev Gupta

Read only

0 Likes
1,223

I see in your code -

poitemx-price_unit,

poitemx-ref_doc,

poitemx-po_unit.

are not populated with value 'X'.

Please add that too.

Read only

0 Likes
1,223

ok I will do that...

is everything else fine???

Thanks

Rajeev Gupta

Read only

0 Likes
1,223

You have

CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

IMPORTING

return = return2.

just after the BAPI call.

If BAPI call is successful, then only you should commit. You need to check contents of RETURN table, if last record contains a SUCCESS message, then you should commit. Run your program in debug, check the value and it will help you to handle this further.