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

Sample code for bapi po change

Former Member
0 Likes
9,873

Anyone plz send me the sample code for po change bapi

5 REPLIES 5
Read only

Former Member
0 Likes
2,951

REPORT zmmi_po_close NO STANDARD PAGE HEADING

LINE-COUNT 65

LINE-SIZE 128

MESSAGE-ID zcustrans.

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

  • DATA DECLARATION *

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

  • Table work area

TABLES: ekko,

ekpo,

eket.

  • Internal tables

DATA: BEGIN OF t_po OCCURS 0,

ebeln LIKE ekko-ebeln,

ebelp LIKE ekpo-ebelp,

matnr LIKE ekpo-matnr,

idnlf LIKE ekpo-idnlf,

menge LIKE ekpo-menge,

meins LIKE ekpo-meins,

netpr LIKE ekpo-netpr,

eindt LIKE eket-eindt,

lifnr LIKE ekko-lifnr,

END OF t_po.

DATA: BEGIN OF t_mess OCCURS 0,

ebeln LIKE ekko-ebeln,

message(100) TYPE c,

END OF t_mess.

DATA: t_po_dup LIKE t_po OCCURS 0 WITH HEADER LINE.

DATA: t_return LIKE bapiret2 OCCURS 0 WITH HEADER LINE.

DATA: t_item LIKE bapimepoitem OCCURS 0 WITH HEADER LINE.

DATA: t_itemx LIKE bapimepoitemx OCCURS 0 WITH HEADER LINE.

  • Variables

DATA: w_col1 TYPE i,

w_col2 TYPE i,

w_len TYPE i,

w_str TYPE i,

w_color TYPE c.

  • Constants

CONSTANTS: c_usd LIKE tcurc-waers VALUE 'USD'.

data : v_day(2) type c .

data : v_days LIKE tvak-vlauf .

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

  • INITIALIZATION *

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

INITIALIZATION.

PERFORM init_data.

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

  • TOP OF PAGE *

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

TOP-OF-PAGE.

PERFORM get_header.

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

  • SELECTION SCREEN *

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

SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.

SELECTION-SCREEN: SKIP 1.

PARAMETERS: p_werks LIKE ekpo-werks DEFAULT '1000' NO-DISPLAY,

p_lgort LIKE ekpo-lgort DEFAULT '1000' NO-DISPLAY,

p_bukrs LIKE ekko-bukrs DEFAULT '0011' NO-DISPLAY,

p_ekorg LIKE ekko-ekorg DEFAULT '1000'.

SELECT-OPTIONS: s_lifnr FOR ekko-lifnr,

s_ekgrp FOR ekko-ekgrp,

s_ebeln FOR ekko-ebeln,

s_matnr FOR ekpo-matnr.

SELECTION-SCREEN: SKIP 1.

SELECTION-SCREEN: END OF BLOCK b1.

SELECTION-SCREEN: BEGIN OF BLOCK b2 WITH FRAME TITLE text-020.

SELECTION-SCREEN: SKIP 1.

SELECTION-SCREEN: BEGIN OF LINE.

SELECTION-SCREEN COMMENT 1(79) text-021.

SELECTION-SCREEN: END OF LINE.

SELECTION-SCREEN: BEGIN OF LINE.

SELECTION-SCREEN COMMENT 1(42) text-022.

SELECTION-SCREEN: END OF LINE.

SELECTION-SCREEN: SKIP 1.

SELECT-OPTIONS: s_eindt FOR eket-eindt NO INTERVALS NO-EXTENSION

OBLIGATORY.

PARAMETERS: p_days LIKE v_day OBLIGATORY.

SELECTION-SCREEN: SKIP 1.

SELECTION-SCREEN: END OF BLOCK b2.

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

  • AT SELECTION SCREEN *

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

AT SELECTION-SCREEN.

  • Ensure the delivery date is only in past

PERFORM check_date.

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

  • START OF SELECTION *

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

START-OF-SELECTION.

  • << Start delete >> Rajs-02/17/2005

  • New rules have been created as variant. Do not need it any more

  • Over-write delivery date if run in background

  • IF NOT sy-batch IS INITIAL.

  • PERFORM init_data.

  • MESSAGE i000 WITH text-018 text-019.

  • ENDIF.

  • << End delete >>

  • Pass the value to Decmal variable

v_days = p_days.

  • Calculate date for selection: Rajs-04/08/2005

PERFORM get_delivery_date.

  • Extract and process data

PERFORM get_data.

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

  • END OF SELECTION *

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

END-OF-SELECTION.

  • Screen output

PERFORM display_report.

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

  • AT LINE SELECTION *

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

AT LINE-SELECTION.

  • Display PO document

IF NOT t_po_dup-ebeln IS INITIAL.

SET PARAMETER ID 'BES' FIELD t_po_dup-ebeln.

CALL TRANSACTION 'ME23N' AND SKIP FIRST SCREEN.

CLEAR: t_po_dup-ebeln.

ENDIF.

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

  • SUB ROUTINES *

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

&----


*& Form get_data

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM get_data.

CLEAR : t_po, t_po_dup, t_return, t_mess.

REFRESH: t_po, t_po_dup, t_return, t_mess.

  • Extract open PO items in past that have schedule delivery as given in

  • selection

SELECT a~ebeln

bebelp bmatnr bidnlf bmenge bmeins bnetpr

ceindt alifnr

INTO TABLE t_po

FROM ( ( ekko AS a INNER JOIN ekpo

AS b ON aebeln = bebeln )

INNER JOIN eket

AS c ON bebeln = cebeln AND

bebelp = cebelp )

WHERE a~ebeln IN s_ebeln AND

a~bstyp = 'F' AND

a~bsart = 'NB' AND

a~loekz = space AND

a~lifnr IN s_lifnr AND

a~ekorg = p_ekorg AND

a~ekgrp IN s_ekgrp AND

b~loekz = space AND

b~matnr IN s_matnr AND

b~werks = p_werks AND

b~lgort = p_lgort AND

b~elikz = space AND

c~eindt IN s_eindt.

CHECK sy-subrc = 0.

t_po_dup[] = t_po[].

SORT: t_po, t_po_dup.

LOOP AT t_po.

  • Get items

t_item-po_item = t_po-ebelp.

t_item-material = t_po-matnr.

t_item-plant = p_werks.

t_item-stge_loc = p_lgort.

t_item-no_more_gr = 'X'.

APPEND t_item.

  • Set item changes

t_itemx-po_item = t_po-ebelp.

t_itemx-no_more_gr = 'X'.

APPEND t_itemx.

AT END OF ebeln.

CLEAR : t_return.

REFRESH: t_return.

  • Set delivery complete indicator for items in a PO

CALL FUNCTION 'BAPI_PO_CHANGE'

EXPORTING

purchaseorder = t_po-ebeln

TABLES

return = t_return

poitem = t_item

poitemx = t_itemx.

LOOP AT t_return WHERE type = 'A' OR

type = 'E'.

MOVE: t_po-ebeln TO t_mess-ebeln,

t_return-message TO t_mess-message.

APPEND t_mess.

CLEAR t_mess.

ENDLOOP.

  • Commit if there is no error

IF sy-subrc <> 0.

CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

EXPORTING

wait = 'X'.

ELSE.

  • This table contains only POs that are successfully processed

DELETE t_po_dup WHERE ebeln = t_po-ebeln.

ENDIF.

CLEAR : t_item, t_itemx.

REFRESH: t_item, t_itemx.

ENDAT.

CLEAR t_po.

ENDLOOP.

ENDFORM. " get_data

&----


*& Form get_header

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM get_header.

FORMAT COLOR COL_HEADING INTENSIFIED OFF.

PERFORM calc_col_and_write USING text-002

sy-linsz.

PERFORM calc_col_and_write USING sy-title

sy-linsz.

  • User id

w_col1 = 2.

WRITE: AT /w_col1 'UserId : ', sy-uname.

w_len = sy-linsz - 1.

WRITE AT sy-colno(w_len) space.

  • Run date

w_col2 = sy-linsz - 16.

WRITE: AT w_col2 'Date: ', sy-datum.

  • Report name

WRITE: AT /w_col1 'Report Id : ', sy-repid.

WRITE AT sy-colno(w_len) space.

  • Run time

w_col2 = sy-linsz - 16.

WRITE: AT w_col2 'Time: ', sy-uzeit.

    • Delivery date

  • WRITE: AT /w_col1 'Delivery date: ', p_vdatu.

  • WRITE AT sy-colno(w_len) space.

  • Page number

w_col2 = sy-linsz - 16.

WRITE: AT /w_col2 'Page: ', sy-pagno.

FORMAT RESET.

ULINE.

SKIP 1.

  • Note

IF NOT t_po[] IS INITIAL.

FORMAT COLOR COL_NEGATIVE INVERSE ON.

WRITE: /2 text-012.

WRITE: /2 text-011.

FORMAT RESET.

ENDIF.

SKIP 1.

FORMAT COLOR COL_HEADING INVERSE OFF INTENSIFIED ON.

ULINE.

  • Column heading

WRITE: /1 sy-vline, 2 text-003,

14 sy-vline, 15 text-004,

22 sy-vline, 23 text-005,

34 sy-vline, 35 text-010,

72 sy-vline, 73 text-006,

92 sy-vline, 93 text-007,

98 sy-vline, 99 text-008,

115 sy-vline, 116 text-009,

128 sy-vline.

ULINE.

FORMAT RESET.

ENDFORM. " get_header

&----


*& Form display_report

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM display_report.

IF t_po[] IS INITIAL.

SKIP 2.

FORMAT COLOR COL_NEGATIVE INTENSIFIED OFF.

WRITE: / text-015.

ELSE.

  • List of PO items closed

LOOP AT t_po_dup.

IF w_color IS INITIAL.

FORMAT COLOR COL_NORMAL INTENSIFIED OFF.

w_color = 'X'.

ELSE.

FORMAT COLOR COL_NORMAL INTENSIFIED ON.

CLEAR w_color.

ENDIF.

WRITE: /1 sy-vline, 2 t_po_dup-ebeln,

14 sy-vline, 15 t_po_dup-ebelp,

22 sy-vline, 23 t_po_dup-matnr NO-ZERO,

34 sy-vline, 35 t_po_dup-idnlf,

72 sy-vline, 73 t_po_dup-menge UNIT t_po_dup-meins,

92 sy-vline, 93 t_po_dup-meins,

98 sy-vline, 99 t_po_dup-netpr CURRENCY c_usd,

115 sy-vline, 116 t_po_dup-lifnr,

128 sy-vline.

HIDE : t_po_dup.

CLEAR: t_po_dup.

AT LAST.

ULINE.

ENDAT.

ENDLOOP.

IF sy-subrc <> 0.

SKIP 2.

FORMAT COLOR COL_NEGATIVE INTENSIFIED OFF.

WRITE: / text-013.

ENDIF.

  • Error messages - List of PO items not closed

LOOP AT t_mess.

AT FIRST.

SKIP 2.

ULINE.

FORMAT COLOR COL_HEADING INTENSIFIED ON.

WRITE: /1 sy-vline, 2 text-003,

14 sy-vline, 15 text-014,

115 sy-vline.

ULINE.

ENDAT.

IF w_color IS INITIAL.

FORMAT COLOR COL_NEGATIVE INTENSIFIED OFF.

w_color = 'X'.

ELSE.

FORMAT COLOR COL_NEGATIVE INTENSIFIED ON.

CLEAR w_color.

ENDIF.

WRITE: /1 sy-vline, 2 t_mess-ebeln,

14 sy-vline, 15 t_mess-message,

115 sy-vline.

HIDE : t_mess.

CLEAR: t_mess.

AT LAST.

ULINE.

ENDAT.

ENDLOOP.

IF sy-subrc <> 0.

SKIP 2.

FORMAT COLOR COL_POSITIVE INTENSIFIED ON.

WRITE: / text-016.

ENDIF.

ENDIF.

ENDFORM. " display_report

&----


*& Form calc_col_and_write

&----


  • text

----


  • -->P_TEXT_002 text

  • -->P_SY_LINSZ text

----


FORM calc_col_and_write USING p_text

p_linsz.

w_str = strlen( p_text ).

w_col1 = ( p_linsz / 2 ) - ( w_str / 2 ).

w_len = p_linsz - w_col1 + 1.

WRITE: AT /w_col1 p_text.

WRITE AT sy-colno(w_len) space.

ENDFORM. " calc_col_and_write

&----


*& Form check_date

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM check_date.

LOOP AT s_eindt.

IF NOT s_eindt-high IS INITIAL AND

s_eindt-high > sy-datum.

MESSAGE e000 WITH text-017.

ELSEIF NOT s_eindt-low IS INITIAL AND

s_eindt-low > sy-datum.

MESSAGE e000 WITH text-017.

ENDIF.

ENDLOOP.

ENDFORM. " check_date

&----


*& Form init_data

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM init_data.

CLEAR s_eindt.

REFRESH s_eindt.

  • All POs that are open more than a specified number of days are to be

  • closed automatically. Current day - 5 is defaulted

s_eindt-sign = 'I'.

s_eindt-option = 'LE'.

s_eindt-low = sy-datum.

APPEND s_eindt.

CLEAR s_eindt.

p_days = '5'.

ENDFORM. " init_data

&----


*& Form get_delivery_date

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM get_delivery_date.

  • Now, the no. of days to retain the PO is given as a parameter

  • Any PO with delivery date prior to this date need to be closed

  • Calculate the date based on selection and default the sign to "=<".

LOOP AT s_eindt.

s_eindt-sign = 'I'.

s_eindt-option = 'LE'.

s_eindt-low = s_eindt-low - v_days.

MODIFY s_eindt.

CLEAR s_eindt.

ENDLOOP.

ENDFORM. " get_delivery_date

Read only

0 Likes
2,951

What is aebeln?

Read only

Former Member
0 Likes
2,951

Hi Sanmugam,

Look at the below link for a sample program.

http://www.sap-img.com/abap/sample-abap-code-on-bapi-po-change.htm

Thanks

Aneesh.

Read only

Former Member
0 Likes
2,951

hi,

am forwarding link, it containing the code for changing the po order using BAPI

<u><b>'BAPI_PO_CHANGE'</b></u>

http://www.sap-img.com/abap/sample-abap-code-on-bapi-po-change.htm

regards,

Ashokreddy.

Read only

Former Member
0 Likes
2,951

Hi Sanmugam Ganesh,

Find the code below.

----


  • PROGRAM : zpo_bapi_purchord_tej *

  • TITLE : Purchase Order BAPI *

  • AUTHOR : Shree Tejus C *

  • DESCRIPTION: report that displays the top 10 Sales Orders in terms *

  • of Net Value of the Sales Order in Document Currency. *

----


  • M O D I F I C A T I O N L O G : *

----


  • Date Correction Programmer Description of Change *

  • ======== ========== =========== ============================== *

  • 29/03/2007 Shree Tejus C Initial Implementation *

----


REPORT zpo_bapi_purchord_tej.

----


  • DATA DECLARATIONS *

----


TYPE-POOLS slis.

TYPES: BEGIN OF ty_table,

v_legacy(8),

vendor TYPE bapimepoheader-vendor,

purch_org TYPE bapimepoheader-purch_org,

pur_group TYPE bapimepoheader-pur_group,

material TYPE bapimepoitem-material,

quantity(13),

delivery_date TYPE bapimeposchedule-delivery_date,

net_price(23),

plant TYPE bapimepoitem-plant,

END OF ty_table.

TYPES: BEGIN OF ty_alv,

v_legs(8),

success(10),

v_legf(8),

END OF ty_alv.

TYPES: BEGIN OF ty_alv1,

v_legf1(8),

v_msg(500),

END OF ty_alv1.

*-----Work area declarations.

DATA: x_table TYPE ty_table,

x_header TYPE bapimepoheader,

x_headerx TYPE bapimepoheaderx,

x_item TYPE bapimepoitem,

x_itemx TYPE bapimepoitemx,

x_sched TYPE bapimeposchedule,

x_schedx TYPE bapimeposchedulx,

x_commatable(255),

x_alv TYPE ty_alv,

x_alv1 TYPE ty_alv1,

x_alv2 TYPE ty_alv1.

*-----Internal table declarations.

DATA: it_table TYPE TABLE OF ty_table,

it_commatable LIKE TABLE OF x_commatable,

it_item TYPE TABLE OF bapimepoitem,

it_itemx TYPE TABLE OF bapimepoitemx,

it_sched TYPE TABLE OF bapimeposchedule,

it_schedx TYPE TABLE OF bapimeposchedulx,

it_alv TYPE TABLE OF ty_alv,

it_alv1 TYPE TABLE OF ty_alv1,

it_alv2 TYPE TABLE OF ty_alv1.

DATA: po_number TYPE bapimepoheader-po_number,

x_return TYPE bapiret2,

it_return TYPE TABLE OF bapiret2,

v_file TYPE string,

v_temp(8),

v_succsount TYPE i VALUE 0,

v_failcount TYPE i VALUE 0,

v_total TYPE i.

DATA: v_temp1(5) TYPE n VALUE 0.

DATA: x_event TYPE slis_t_event,

x_fieldcat TYPE slis_t_fieldcat_alv,

x_list_header TYPE slis_t_listheader,

x_event1 LIKE LINE OF x_event,

x_layout1 TYPE slis_layout_alv,

x_variant1 TYPE disvariant,

x_repid2 LIKE sy-repid.

DATA : it_fieldcat TYPE TABLE OF slis_t_fieldcat_alv.

----


  • SELECTION-SCREEN *

----


SELECTION-SCREEN BEGIN OF BLOCK v_b1 WITH FRAME.

*-----To fetch the flat file.

PARAMETERS: p_file TYPE rlgrap-filename.

SELECTION-SCREEN END OF BLOCK v_b1.

----


  • AT SELECTION-SCREEN *

----


AT SELECTION-SCREEN.

IF p_file IS INITIAL.

MESSAGE text-001 TYPE 'E'.

ENDIF.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

*-----To use F4 help to find file path.

CALL FUNCTION 'F4_FILENAME'

EXPORTING

program_name = syst-cprog

dynpro_number = syst-dynnr

IMPORTING

file_name = p_file.

v_file = p_file.

----


  • START-OF-SELECTION *

----


START-OF-SELECTION.

PERFORM gui_upload.

LOOP AT it_table INTO x_table.

PERFORM header_details.

v_temp = x_table-v_legacy.

LOOP AT it_table INTO x_table WHERE v_legacy = v_temp.

PERFORM lineitem.

PERFORM schedule.

ENDLOOP.

DELETE it_table WHERE v_legacy = v_temp.

PERFORM bapicall.

MOVE po_number TO x_alv-success.

APPEND x_alv TO it_alv.

CLEAR x_alv.

*-----To clear the item details in internal table after the operation for a header.

REFRESH: it_item,

it_itemx,

it_sched,

it_schedx.

CLEAR: v_temp1.

ENDLOOP.

v_total = v_succsount + v_failcount.

PERFORM display_alv.

----


  • FORM GUI_UPLOAD *

----


FORM gui_upload .

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

filename = v_file

filetype = 'ASC'

TABLES

data_tab = it_commatable

EXCEPTIONS

file_open_error = 1

file_read_error = 2

no_batch = 3

gui_refuse_filetransfer = 4

invalid_type = 5

no_authority = 6

unknown_error = 7

bad_data_format = 8

header_not_allowed = 9

separator_not_allowed = 10

header_too_long = 11

unknown_dp_error = 12

access_denied = 13

dp_out_of_memory = 14

disk_full = 15

dp_timeout = 16

OTHERS = 17

.

IF sy-subrc = 0.

*-----To fetch the comma seperated flat file into an internal table.

LOOP AT it_commatable INTO x_commatable.

IF x_commatable IS NOT INITIAL.

SPLIT x_commatable AT ',' INTO

x_table-v_legacy

x_table-vendor

x_table-purch_org

x_table-pur_group

x_table-material

x_table-quantity

x_table-delivery_date

x_table-net_price

x_table-plant.

APPEND x_table TO it_table.

ENDIF.

CLEAR x_table.

ENDLOOP.

ENDIF.

ENDFORM. " gui_upload

----


  • FORM HEADER_DETAILS *

----


FORM header_details .

MOVE 'NB' TO x_header-doc_type.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

EXPORTING

input = x_table-vendor

IMPORTING

output = x_table-vendor

.

MOVE x_table-vendor TO x_header-vendor.

MOVE x_table-purch_org TO x_header-purch_org.

MOVE x_table-pur_group TO x_header-pur_group.

x_headerx-doc_type = 'X'.

x_headerx-vendor = 'X'.

x_headerx-purch_org = 'X'.

x_headerx-pur_group = 'X'.

ENDFORM. " header_details

----


  • FORM LINEITEM *

----


FORM lineitem .

v_temp1 = v_temp1 + 10.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

EXPORTING

input = v_temp1

IMPORTING

output = v_temp1.

MOVE v_temp1 TO x_item-po_item.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

EXPORTING

input = x_table-material

IMPORTING

output = x_table-material.

MOVE x_table-material TO x_item-material.

MOVE x_table-quantity TO x_item-quantity.

MOVE x_table-net_price TO x_item-net_price.

MOVE x_table-plant TO x_item-plant.

x_itemx-po_item = v_temp1.

x_itemx-material = 'X'.

x_itemx-quantity = 'X'.

x_itemx-net_price = 'X'.

x_itemx-plant = 'X'.

APPEND x_item TO it_item.

APPEND x_itemx TO it_itemx.

CLEAR: x_item, x_itemx.

ENDFORM. " lineitem1

----


  • FORM SCHEDULE *

----


FORM schedule .

MOVE x_table-delivery_date TO x_sched-delivery_date.

MOVE v_temp1 TO x_sched-po_item.

x_schedx-delivery_date = 'X'.

x_schedx-po_item = v_temp1.

APPEND x_sched TO it_sched.

APPEND x_schedx TO it_schedx.

CLEAR: x_sched, x_schedx.

ENDFORM. " schedule

----


  • FORM BAPICALL *

----


FORM bapicall .

CALL FUNCTION 'BAPI_PO_CREATE1'

EXPORTING

poheader = x_header

poheaderx = x_headerx

IMPORTING

exppurchaseorder = po_number

TABLES

return = it_return

poitem = it_item

poitemx = it_itemx

poschedule = it_sched

poschedulex = it_schedx.

IF po_number IS NOT INITIAL.

v_succsount = v_succsount + 1.

MOVE x_table-v_legacy TO x_alv-v_legs.

CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.

ELSE.

v_failcount = v_failcount + 1.

MOVE x_table-v_legacy TO x_alv-v_legf.

MOVE x_table-v_legacy TO x_alv1-v_legf1.

LOOP AT it_return INTO x_return.

IF x_alv1-v_msg IS INITIAL.

MOVE x_return-message TO x_alv1-v_msg.

ELSE.

CONCATENATE x_alv1-v_msg x_return-message INTO x_alv1-v_msg SEPARATED BY space.

ENDIF.

ENDLOOP.

APPEND x_alv1 TO it_alv1.

CLEAR x_alv1.

ENDIF.

ENDFORM. " bapicall

-


Reward Points if useful.

Thanks,

Tej..