cancel
Showing results for 
Search instead for 
Did you mean: 

Need UserExit for Automatic Generation of STO on chaning the plant

Former Member
0 Kudos

Hi Frnds,

I need UserExit for automatic generation of STO on changing the plant.

Regards,

Jaya chandra

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi All

I am also looking for the same. Any help is most appreciated.

Thanks.

Tiag

Former Member
0 Kudos

Hi ,

Iam giving the entire code..I donno whether ur prob has been solved or not..

It may help you..

Include is MV45AFZZ.

Code :

data : lt_vbap type table of vbap,

wa_vbap type vbap,

lt_ekko type table of ekko ,

lt_ekko1 type table of ekko ,

lt_ekpo type table of ekpo ,

wa_ekpo type ekpo,

lt_ekbe type table of ekbe.

types : begin of ty_xvbap.

include structure vbapvb.

types : flag type c length 1.

types : end of ty_xvbap.

data: lt_xvbap type table of ty_xvbap,

wa_xvbap1 type ty_xvbap.

data : ltemp_xvbap type table of vbapvb,

watemp_xvbap type vbapvb,

wa_vbapvb type vbapvb.

data : wa_poheader type bapimepoheader,

wa_poheaderx type bapimepoheaderx,

lt_return type table of bapiret2,

lt_poitem type table of bapimepoitem,

wa_poitem type bapimepoitem,

lt_poitemx type table of bapimepoitemx,

wa_poitemx type bapimepoitemx,

lt_poekbe type table of bapiekbe,

g_ebeln type ebeln.

constants : c_comp_code type bapimepoheader-comp_code value 'P001',

c_purch_org type bapimepoheader-purch_org value '1000',

c_pur_group type bapimepoheader-pur_group value '100',

c_zz05 type bapimepoheader-doc_type value 'ZZ05',

c_001 type bapimepoitem-order_reason value '001',

c_x type bapiupdate value 'X'.

data: v_indx type sy-tabix,

v_indx1 type sy-tabix.

select * from vbap into table lt_vbap where vbeln = vbeln.

lt_xvbap[] = xvbap[].

sort lt_vbap by vbeln posnr.

loop at lt_xvbap into wa_xvbap1.

read table lt_vbap into wa_vbap with key vbeln = wa_xvbap1-vbeln

posnr = wa_xvbap1-posnr binary search.

if sy-subrc eq 0.

clear : v_indx1,v_indx.

v_indx1 = sy-tabix.

v_indx = sy-tabix.

*CHECK FOR PLANT CHANGING

if wa_vbap-werks <> wa_xvbap1-werks and wa_vbap-werks is not initial.

*check for PO existance based on Sales order and Supplying plant.

select * from ekko into table lt_ekko where ihrez = wa_xvbap1-vbeln

and reswk = wa_vbap-zzreswk.

if sy-subrc eq 4 and wa_xvbap1-flag eq ''.

select * from ekko into table lt_ekko1 where ihrez = wa_xvbap1-vbeln.

if lt_ekko1[] is not initial.

select * from ekpo into table lt_ekpo

for all entries in lt_ekko1

where ebeln = lt_ekko1-ebeln

and werks = wa_xvbap1-werks.

endif.

if lt_ekpo[] is not initial.

select * from ekbe into table lt_ekbe

for all entries in lt_ekpo

where ebeln = lt_ekpo-ebeln

and ebelp = lt_ekpo-ebelp

and vgabe = '8'.

if sy-subrc eq 4.

clear v_indx.

read table xvbap into wa_vbapvb with key vbeln = wa_xvbap1-vbeln

posnr = wa_xvbap1-posnr binary search .

v_indx = sy-tabix.

xvbap-zzreswk = wa_vbap-werks.

modify xvbap from wa_vbapvb index v_indx transporting zzreswk.

loop at lt_ekpo into wa_ekpo.

if wa_ekpo-werks eq wa_xvbap1-werks and wa_ekpo-matnr eq wa_xvbap1-matnr .

wa_xvbap1-flag = 'X'.

modify lt_xvbap index v_indx1 from wa_xvbap1.

wa_poheader-comp_code = c_comp_code.

wa_poheader-doc_type = c_zz05.

wa_poheader-creat_date = sy-datum.

wa_poheader-suppl_plnt = wa_vbap-zzreswk.

wa_poheader-purch_org = c_purch_org.

wa_poheader-pur_group = c_pur_group.

wa_poheader-ref_1 = vbeln.

wa_poheaderx-comp_code = c_x.

wa_poheaderx-doc_type = c_x.

wa_poheaderx-creat_date = sy-datum.

wa_poheaderx-suppl_plnt = c_x.

wa_poheaderx-purch_org = c_x.

wa_poheaderx-pur_group = c_x.

wa_poheaderx-ref_1 = c_x.

wa_poitem-po_item = wa_xvbap1-posnr+1(5).

wa_poitem-material = wa_xvbap1-matnr.

wa_poitem-plant = wa_xvbap1-werks.

wa_poitem-quantity = wa_xvbap1-kwmeng.

wa_poitem-plant = xvbap-werks.

wa_poitem-order_reason = c_001.

wa_poitem-batch = wa_xvbap1-charg.

append wa_poitem to lt_poitem.

wa_poitemx-po_item = wa_xvbap1-posnr+1(5).

wa_poitemx-po_itemx = c_x.

wa_poitemx-material = c_x.

wa_poitemx-plant = c_x.

wa_poitemx-quantity = c_x.

wa_poitemx-order_reason = c_x.

wa_poitemx-batch = c_x.

append wa_poitemx to lt_poitemx.

call function 'BAPI_PO_CHANGE'

exporting

purchaseorder = wa_ekpo-ebeln

tables

return = lt_return

poitem = lt_poitem

poitemx = lt_poitemx

pohistory = lt_poekbe.

read table lt_return with key type = 'E' transporting no fields.

if not sy-subrc eq 0.

call function 'BAPI_TRANSACTION_COMMIT'.

endif.

clear : wa_poheader ,

wa_poheaderx ,

wa_poitem ,

wa_poitemx ,

g_ebeln .

free : lt_return ,

lt_poitem ,

lt_poitemx ,

lt_poekbe .

endif.

endloop.

else.

clear : v_indx,wa_vbapvb.

read table xvbap into wa_vbapvb with key vbeln = wa_xvbap1-vbeln

posnr = wa_xvbap1-posnr binary search.

v_indx = sy-tabix.

xvbap-zzreswk = wa_vbap-werks.

modify xvbap from wa_vbapvb index v_indx transporting zzreswk.

wa_xvbap1-flag = 'X'.

modify lt_xvbap index v_indx from wa_xvbap1.

wa_poheader-comp_code = c_comp_code.

wa_poheader-doc_type = c_zz05.

wa_poheader-creat_date = sy-datum.

wa_poheader-suppl_plnt = xvbap-zzreswk.

wa_poheader-purch_org = c_purch_org.

wa_poheader-pur_group = c_pur_group.

wa_poheader-ref_1 = vbeln.

wa_poheaderx-comp_code = c_x.

wa_poheaderx-doc_type = c_x.

wa_poheader-creat_date = sy-datum.

wa_poheaderx-suppl_plnt = c_x.

wa_poheaderx-purch_org = c_x.

wa_poheaderx-pur_group = c_x.

wa_poheaderx-ref_1 = c_x.

wa_poheaderx-item_intvl = c_x.

wa_poitem-po_item = wa_xvbap1-posnr.

wa_poitem-material = wa_xvbap1-matnr.

wa_poitem-plant = wa_xvbap1-werks.

wa_poitem-quantity = wa_xvbap1-kwmeng.

wa_poitem-order_reason = c_001.

wa_poitem-batch = wa_xvbap1-charg.

append wa_poitem to lt_poitem.

wa_poitemx-po_item = wa_xvbap1-posnr.

wa_poitemx-po_itemx = c_x.

wa_poitemx-material = c_x.

wa_poitemx-plant = c_x.

wa_poitemx-quantity = c_x.

wa_poitemx-order_reason = c_x.

wa_poitemx-batch = c_x.

append wa_poitemx to lt_poitemx.

call function 'BAPI_PO_CREATE1'

exporting

poheader = wa_poheader

poheaderx = wa_poheaderx

importing

exppurchaseorder = g_ebeln

tables

return = lt_return

poitem = lt_poitem

poitemx = lt_poitemx.

read table lt_return with key type = 'E' transporting no fields.

if not sy-subrc eq 0.

call function 'BAPI_TRANSACTION_COMMIT'.

endif.

clear : wa_xvbap1 ,

wa_vbap ,

wa_poheader ,

wa_poheaderx ,

wa_poitem ,

wa_poitemx ,

g_ebeln .

free : lt_return ,

lt_poitemx ,

lt_poitem .

endif.

else.

ltemp_xvbap[] = xvbap[].

*If PO is not existing Save Supplying Plamt

delete ltemp_xvbap where werks <> wa_xvbap1-werks.

loop at ltemp_xvbap into watemp_xvbap where zzreswk = ''.

clear v_indx.

read table xvbap into wa_vbapvb with key vbeln = watemp_xvbap-vbeln

posnr = watemp_xvbap-posnr binary search.

v_indx = sy-tabix.

xvbap-zzreswk = wa_vbap-werks.

modify xvbap from wa_vbapvb index v_indx transporting zzreswk.

endloop.

wa_poheader-comp_code = c_comp_code.

wa_poheader-doc_type = c_zz05.

wa_poheader-creat_date = sy-datum.

wa_poheader-suppl_plnt = xvbap-zzreswk.

wa_poheader-purch_org = c_purch_org.

wa_poheader-pur_group = c_pur_group.

wa_poheader-ref_1 = vbeln.

wa_poheaderx-comp_code = c_x.

wa_poheaderx-doc_type = c_x.

wa_poheader-creat_date = sy-datum.

wa_poheaderx-suppl_plnt = c_x.

wa_poheaderx-purch_org = c_x.

wa_poheaderx-pur_group = c_x.

wa_poheaderx-ref_1 = c_x.

wa_poheaderx-item_intvl = c_x.

loop at ltemp_xvbap into watemp_xvbap.

clear v_indx.

read table lt_xvbap into wa_xvbap1 with key vbeln = watemp_xvbap-vbeln

posnr = watemp_xvbap-posnr binary search.

v_indx = sy-tabix.

wa_xvbap1-flag = 'X'.

modify lt_xvbap index v_indx from wa_xvbap1.

wa_poitem-po_item = watemp_xvbap-posnr.

wa_poitem-material = watemp_xvbap-matnr.

wa_poitem-plant = watemp_xvbap-werks.

wa_poitem-quantity = watemp_xvbap-kwmeng.

wa_poitem-order_reason = c_001.

wa_poitem-batch = watemp_xvbap-charg.

append wa_poitem to lt_poitem.

wa_poitemx-po_item = watemp_xvbap-posnr.

wa_poitemx-po_itemx = c_x.

wa_poitemx-material = c_x.

wa_poitemx-plant = c_x.

wa_poitemx-quantity = c_x.

wa_poitemx-order_reason = c_x.

wa_poitemx-batch = c_x.

append wa_poitemx to lt_poitemx.

endloop.

call function 'BAPI_PO_CREATE1'

exporting

poheader = wa_poheader

poheaderx = wa_poheaderx

importing

exppurchaseorder = g_ebeln

tables

return = lt_return

poitem = lt_poitem

poitemx = lt_poitemx.

read table lt_return with key type = 'E' transporting no fields.

if not sy-subrc eq 0.

call function 'BAPI_TRANSACTION_COMMIT'.

endif.

clear : wa_xvbap1 ,

wa_vbap ,

wa_poheader ,

wa_poheaderx ,

wa_poitem ,

wa_poitemx ,

g_ebeln .

free : lt_return ,

lt_poitemx ,

lt_poitem .

endif.

else.

if lt_ekko[] is not initial.

select * from ekpo into table lt_ekpo

for all entries in lt_ekko

where ebeln = lt_ekko-ebeln.

endif.

if lt_ekpo[] is not initial.

select * from ekbe into table lt_ekbe

for all entries in lt_ekpo

where ebeln = lt_ekpo-ebeln

and ebelp = lt_ekpo-ebelp.

sort lt_ekbe by ebeln ebelp.

endif.

*For that PO check the Receiving plant is same or different for the current given plant.

loop at lt_ekpo into wa_ekpo.

if wa_ekpo-werks <> wa_xvbap1-werks.

clear v_indx.

v_indx = sy-tabix.

wa_xvbap1-flag = 'X'.

modify lt_xvbap index v_indx from wa_xvbap1.

wa_poheader-comp_code = c_comp_code.

wa_poheader-doc_type = c_zz05.

wa_poheader-creat_date = sy-datum.

wa_poheader-suppl_plnt = wa_vbap-zzreswk.

wa_poheader-purch_org = c_purch_org.

wa_poheader-pur_group = c_pur_group.

wa_poheader-ref_1 = vbeln.

wa_poheaderx-comp_code = c_x.

wa_poheaderx-doc_type = c_x.

wa_poheaderx-creat_date = sy-datum.

wa_poheaderx-suppl_plnt = c_x.

wa_poheaderx-purch_org = c_x.

wa_poheaderx-pur_group = c_x.

wa_poheaderx-ref_1 = c_x.

wa_poitem-po_item = wa_xvbap1-posnr.

wa_poitem-material = wa_xvbap1-matnr.

wa_poitem-plant = wa_xvbap1-werks.

wa_poitem-quantity = wa_xvbap1-kwmeng.

wa_poitem-plant = xvbap-werks.

wa_poitem-order_reason = c_001.

wa_poitem-batch = wa_xvbap1-charg.

append wa_poitem to lt_poitem.

wa_poitemx-po_item = wa_xvbap1-posnr.

wa_poitemx-po_itemx = c_x.

wa_poitemx-material = c_x.

wa_poitemx-plant = c_x.

wa_poitemx-quantity = c_x.

wa_poitemx-order_reason = c_x.

wa_poitemx-batch = c_x.

append wa_poitemx to lt_poitemx.

call function 'BAPI_PO_CREATE1'

exporting

poheader = wa_poheader

poheaderx = wa_poheaderx

importing

exppurchaseorder = g_ebeln

tables

return = lt_return

poitem = lt_poitem

poitemx = lt_poitemx.

read table lt_return with key type = 'E' transporting no fields.

if not sy-subrc eq 0.

call function 'BAPI_TRANSACTION_COMMIT'.

endif.

clear : wa_xvbap1 ,

wa_vbap ,

wa_poheader ,

wa_poheaderx ,

wa_poitem ,

wa_poitemx ,

g_ebeln .

free : lt_return ,

lt_poitemx ,

lt_poekbe .

else.

continue.

endif.

endloop.

endif.

endif.

endif. "Plant checking

endloop. "XVBAP