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

Goods Receipt for PO using BAPI

Former Member
0 Likes
4,410

Hello,

I need to do a goods receipt for a purchase order using bapi_goodsmvt_create for movement type 101. My function module does not return the material document number. Can someone help me with my code?

Thanks,

A.P.

FUNCTION Z_GR_BAPI.

*"----


""Local interface:

*" IMPORTING

*" REFERENCE(I_CODE) TYPE GM_CODE

*" REFERENCE(I_MOVE_TYPE) TYPE CHAR3

*" REFERENCE(I_EKKO) TYPE EKKO

*" EXPORTING

*" REFERENCE(E_DOCUMENT) TYPE BAPI2017_GM_HEAD_RET-MAT_DOC

*" REFERENCE(E_RETURN) TYPE BAPIRET2

*" TABLES

*" T_EKPO STRUCTURE EKPO

*"----


  • Structures for BAPI

data: gm_header type bapi2017_gm_head_01.

data: gm_code type bapi2017_gm_code.

data: gm_headret type bapi2017_gm_head_ret.

data: gm_item type table of

bapi2017_gm_item_create with header line.

data: gm_return type bapiret2 occurs 0.

data: gm_retmtd type bapi2017_gm_head_ret-mat_doc.

data: x_ekpo type table of ekpo with header line.

x_ekpo[] = t_ekpo[].

clear: gm_return, gm_retmtd.

refresh gm_return.

gm_code-gm_code = i_code.

gm_header-pstng_date = sy-datum.

gm_header-doc_date = sy-datum.

gm_header-pr_uname = sy-uname.

loop at x_ekpo.

gm_item-material = x_ekpo-matnr.

gm_item-plant = x_ekpo-werks.

gm_item-stge_loc = x_ekpo-lgort.

gm_item-move_type = i_move_type.

gm_item-mvt_ind = 'B'.

gm_item-stck_type = space.

gm_item-entry_qnt = x_ekpo-menge.

gm_item-entry_uom = x_ekpo-meins.

append gm_item.

endloop.

CALL FUNCTION 'BAPI_GOODSMVT_CREATE'

EXPORTING

GOODSMVT_HEADER = gm_header

GOODSMVT_CODE = gm_code

  • TESTRUN = ' '

IMPORTING

GOODSMVT_HEADRET = gm_headret

MATERIALDOCUMENT = gm_retmtd

  • MATDOCUMENTYEAR =

TABLES

GOODSMVT_ITEM = gm_item

  • GOODSMVT_SERIALNUMBER =

RETURN = gm_return.

e_document = gm_retmtd.

CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

EXPORTING

WAIT = 'X'.

  • IMPORTING

  • RETURN =

ENDFUNCTION.

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
3,072

First problem is that I don't see where you are referencing the po number. You should be receiving against PO.

move '0000095851' to gm_item-po_number.
move '1'          to gm_item-po_item.

Regards,

Rich Heilman

Hello,

I need to do a goods receipt for a purchase order using bapi_goodsmvt_create for movement type 101. My function module does not return the material document number. Can someone help me with my code?

Thanks,

A.P.

FUNCTION Z_GR_BAPI.

*"----


""Local interface:

*" IMPORTING

*" REFERENCE(I_CODE) TYPE GM_CODE

*" REFERENCE(I_MOVE_TYPE) TYPE CHAR3

*" REFERENCE(I_EKKO) TYPE EKKO

*" EXPORTING

*" REFERENCE(E_DOCUMENT) TYPE BAPI2017_GM_HEAD_RET-MAT_DOC

*" REFERENCE(E_RETURN) TYPE BAPIRET2

*" TABLES

*" T_EKPO STRUCTURE EKPO

*"----


  • Structures for BAPI

data: gm_header type bapi2017_gm_head_01.

data: gm_code type bapi2017_gm_code.

data: gm_headret type bapi2017_gm_head_ret.

data: gm_item type table of

bapi2017_gm_item_create with header line.

data: gm_return type bapiret2 occurs 0.

data: gm_retmtd type bapi2017_gm_head_ret-mat_doc.

data: x_ekpo type table of ekpo with header line.

x_ekpo[] = t_ekpo[].

clear: gm_return, gm_retmtd.

refresh gm_return.

gm_code-gm_code = i_code.

gm_header-pstng_date = sy-datum.

gm_header-doc_date = sy-datum.

gm_header-pr_uname = sy-uname.

loop at x_ekpo.

gm_item-material = x_ekpo-matnr.

gm_item-plant = x_ekpo-werks.

gm_item-stge_loc = x_ekpo-lgort.

gm_item-move_type = i_move_type.

gm_item-mvt_ind = 'B'.

gm_item-stck_type = space.

gm_item-entry_qnt = x_ekpo-menge.

gm_item-entry_uom = x_ekpo-meins.

append gm_item.

endloop.

CALL FUNCTION 'BAPI_GOODSMVT_CREATE'

EXPORTING

GOODSMVT_HEADER = gm_header

GOODSMVT_CODE = gm_code

  • TESTRUN = ' '

IMPORTING

GOODSMVT_HEADRET = gm_headret

MATERIALDOCUMENT = gm_retmtd

  • MATDOCUMENTYEAR =

TABLES

GOODSMVT_ITEM = gm_item

  • GOODSMVT_SERIALNUMBER =

RETURN = gm_return.

e_document = gm_retmtd.

CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

EXPORTING

WAIT = 'X'.

  • IMPORTING

  • RETURN =

ENDFUNCTION.

8 REPLIES 8
Read only

LucianoBentiveg
Active Contributor
0 Likes
3,072

If function module does not return the material document number, may be you have errors in gm_return.

Regards.

Read only

0 Likes
3,072

Hi Peluka,

Thnaks for ur reply. Any idea as to why I'm might get an error?

A.P.

Read only

Former Member
0 Likes
3,072

Hi,

Does BAPI_GOODSMVT_CREATE return your material doc number in structure GOODSMVT_HEADRET field MAT_DOC? According to the BAPI documentation for GOODSMVT_HEADRET, "This structure transfers the fields that are filled to the initiator when the material document was successfully posted."

Regards,

James G.

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
3,073

First problem is that I don't see where you are referencing the po number. You should be receiving against PO.

move '0000095851' to gm_item-po_number.
move '1'          to gm_item-po_item.

Regards,

Rich Heilman

Read only

Former Member
0 Likes
3,072

Hi,

This piece of code may be helpful.

  • Structures for BAPI

data: gm_header type bapi2017_gm_head_01.

data: gm_code type bapi2017_gm_code.

data: gm_headret type bapi2017_gm_head_ret.

data: gm_item type table of

bapi2017_gm_item_create with header line.

data: gm_return type bapiret2 occurs 0.

data: gm_retmtd type bapi2017_gm_head_ret-mat_doc.

clear: gm_return, gm_retmtd. refresh gm_return.

  • Setup BAPI header data.

gm_header-pstng_date = sy-datum.

gm_header-doc_date = sy-datum.

gm_code-gm_code = '06'. " MB11

  • Write 551 movement to table

clear gm_item.

move '551' to gm_item-move_type .

move '000000000040001234' to gm_item-material.

move '1' to gm_item-entry_qnt.

move 'EA' to gm_item-entry_uom.

move '0004' to gm_item-plant.

move '4000' to gm_item-stge_loc.

move '201' to gm_item-move_reas.

  • Determine cost center per plant

case xresb-werks.

when '0004'.

move '0000041430' to gm_item-costcenter.

when '0006'.

move '0000041630' to gm_item-costcenter.

when '0007'.

move '0000041731' to gm_item-costcenter.

when '0008'.

move '0000041830' to gm_item-costcenter.

endcase.

append gm_item.

  • Call goods movement BAPI

call function 'BAPI_GOODSMVT_CREATE'

exporting

goodsmvt_header = gm_header

goodsmvt_code = gm_code

importing

goodsmvt_headret = gm_headret

materialdocument = gm_retmtd

tables

goodsmvt_item = gm_item

return = gm_return.

call function 'BAPI_TRANSACTION_COMMIT'

exporting

wait = 'X'.

regards,

keerthi.

Read only

Former Member
0 Likes
3,072

Hi A.P,

Please try to place the code e_document = gm_retmtd after BAPI_TRANSACTION_COMMIT.


CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
  EXPORTING
    WAIT = 'X'.
 
e_document = gm_retmtd.

Hope this will help.

Regards,

Ferry Lianto

Read only

0 Likes
3,072

For example, this example program works very will for me in doing a GR against a PO.



report zrich_0001.

* Structures for BAPI
data: gm_header  type bapi2017_gm_head_01.
data: gm_code    type bapi2017_gm_code.
data: gm_headret type bapi2017_gm_head_ret.
data: gm_item    type table of
                 bapi2017_gm_item_create with header line.
data: gm_return  type bapiret2 occurs 0 with header line.
data: gm_retmtd  type bapi2017_gm_head_ret-mat_doc.

clear: gm_return, gm_retmtd. refresh gm_return.

* Setup BAPI header data.
gm_header-pstng_date = sy-datum.
gm_header-doc_date   = sy-datum.
<b>gm_code-gm_code      = '01'.     "" GR for PO</b>

* Write 101 movement to table
clear gm_item.
move '101'                 to gm_item-move_type     .
move '000000000040003572'  to gm_item-material.
move '100'      to gm_item-entry_qnt.
move 'BF'       to gm_item-entry_uom.
move '0008'     to gm_item-plant.
move '8003'     to gm_item-stge_loc.


<b>move '0000095851' to gm_item-po_number.
move '000001'     to gm_item-po_item.
move 'B'          to gm_item-mvt_ind.</b>

append gm_item.

* Call goods movement BAPI
call function 'BAPI_GOODSMVT_CREATE'
     exporting
          goodsmvt_header  = gm_header
          goodsmvt_code    = gm_code
     importing
          goodsmvt_headret = gm_headret
          materialdocument = gm_retmtd
     tables
          goodsmvt_item    = gm_item
          return           = gm_return.

if not gm_retmtd is initial.
  commit work and wait.
  call function 'DEQUEUE_ALL'.
else.
  commit work and wait.
  call function 'DEQUEUE_ALL'.
endif.

write:/ gm_retmtd.

loop at gm_return.
  write:/ gm_return.
endloop.


Regards,

Rich Heilman

Read only

Former Member
0 Likes
3,072

Hello Guys,

BAPI works great now. I forgot the add the PO to the gm_item table (thanks to Rich) and I had to write the e_return line after bapi_transaction_commit(thanks to Ferry). I guess it was trying to fish without a fishing rod. Anyways, thank you all for your super quick replies.

Regards,

A.P.