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

give an example for bapi and give code plz

Former Member
0 Likes
928

how will u access bapi, give an object with expalnation

4 REPLIES 4
Read only

Former Member
0 Likes
893

Go to SE37.

put bapi*

press F4

you have a number of bapi's here.

you use them from se37 only.

you also have a number of bapi's and code as well, they are just like that of FM.

reward if useful.

Amit Singla

Message was edited by:

Amit Singla

Read only

Former Member
0 Likes
893

Hi,

try tcode BAPI.

Regards, Dieter

Read only

former_member404244
Active Contributor
0 Likes
893

Hi,

chek this

REPORT ztest .

----


  • TABLES DECLARATION

----


TABLES: ekko.

----


  • TYPES DECLARATIONS *

----


----


  • TYPES DECLARATION FOR OPEN QUANTITY PO ITEMS *

----


TYPES: BEGIN OF t_openpoitems,

ebeln TYPE ebeln, "Purchasing Document Number

ebelp TYPE ekpo-ebelp, "Item Number of Purchasing Document

bukrs TYPE bukrs, "Company Code

bedat TYPE bedat, "Purchasing Document Date

menge TYPE ekpo-menge, "Purchase order quantity

txz01 TYPE ekpo-txz01, "Short text

werks TYPE ekpo-werks, "Plant

matkl TYPE ekpo-matkl, "Material Group

elikz TYPE ekpo-elikz, "Delivery Completed Indicator

meins TYPE ekpo-meins, "Order Unit

netpr TYPE ekpo-netpr, "Net price in purchasing document

peinh TYPE ekpo-peinh, "Price unit

END OF t_openpoitems.

----


  • TYPES DECLARATION FOR UPDATION OF LINE ITEMS *

----


TYPES: BEGIN OF t_updateitems,

ebeln TYPE ebeln, "Purchasing Document Number

ebelp TYPE ekpo-ebelp, "Item Number of Purchasing Document

matnr TYPE ekpo-matnr, "Material Number

txz01 TYPE ekpo-txz01, " Item Description

menge TYPE ekpo-menge, "Purchase order quantity

meins TYPE ekpo-meins, "Order Unit

werks TYPE ekpo-werks, "Plant

elikz TYPE ekpo-elikz, "Delivery Completed Indicator

netpr TYPE ekpo-netpr, "Net price in purchasing document

peinh TYPE ekpo-peinh, "Price unit

ebelp_n TYPE ekpo-ebelp, " New line item

menge_n TYPE ekpo-menge, " New Qty

END OF t_updateitems.

----


  • TYPES DECLARATION TO SUM OF DELIVERED FOR AN ITEM *

----


TYPES: BEGIN OF t_podelitems,

ebeln TYPE ekko-ebeln, " Purchase doc. no

ebelp TYPE ekpo-ebelp, " Item number

wemng TYPE eket-wemng, " Quantity

END OF t_podelitems.

----


  • TYPES DECLARATION FOR MATERIAL THAT EXISTS *

----


TYPES: BEGIN OF t_matnr,

matnr TYPE mara-matnr, " Material

END OF t_matnr.

----


  • TYPES DECLARATION FOR ERROR LOG *

----


TYPES: BEGIN OF t_error,

ebeln(30) TYPE c, " Purchase order

ebelp(8) TYPE c, " Item Number

txz01(40) TYPE c, " Description

menge(18) TYPE c, " Quantity

ebelp_n(8) TYPE c, " New Item No

matnr(18) TYPE c, " Material No

menge_n(18) TYPE c, " Quantity new

msgnr(10) TYPE c, " SAP MSG no

txt(100) TYPE c, " Message Text

END OF t_error.

----


  • DATA DECLARATIONS *

----


DATA: i_matnr TYPE SORTED TABLE OF t_matnr WITH UNIQUE KEY matnr.

DATA: i_error TYPE TABLE OF t_error,

wa_error TYPE t_error.

DATA: i_updateitems TYPE TABLE OF t_updateitems,

wa_updateitems TYPE t_updateitems.

  • v_error TYPE c. "#EC *

----


  • CONSTANTS *

----


CONSTANTS: c_i(1) TYPE c VALUE 'X'. "Constant with Value 'X'.

----


  • SELECTION SCREEN *

----


SELECTION-SCREEN BEGIN OF BLOCK block1 WITH FRAME.

PARAMETERS p_file TYPE rlgrap-filename OBLIGATORY." ERROR FILE PATH

SELECT-OPTIONS s_ebeln FOR ekko-ebeln. " only for testing

SELECTION-SCREEN END OF BLOCK block1.

----


  • START-OF-SELECTION *

----


START-OF-SELECTION.

*->> get the BCPS purchase order data for update

PERFORM get_purchase_order_data.

*->> Call the BAPI for changing the PO's

PERFORM call_bapi_po_change.

----


  • END-OF-SELECTION *

----


END-OF-SELECTION.

IF i_updateitems[] IS INITIAL.

MESSAGE i002.

ELSE.

*->> Display the errors in spool or Error file

PERFORM error_report.

ENDIF.

----


  • TOP-OF-PAGE *

----


TOP-OF-PAGE.

PERFORM top_of_page.

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

  • SUBROUTINES *

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

&----


*& Form get_purchase_order_data

&----


  • Retrieve all the purchase orders for conversion

----


FORM get_purchase_order_data .

----


  • DATA DECLARATION *

----


CONSTANTS: lc_bpcs TYPE ekko-bsart VALUE 'BPCS', " PO type

lc_f TYPE ekko-bstyp VALUE 'F', " PO category

lc_r TYPE ekpo-knttp VALUE 'R'. " Item Cat

DATA: li_openpoitems TYPE TABLE OF t_openpoitems,

lwa_openpoitems TYPE t_openpoitems,

li_podelitems TYPE TABLE OF t_podelitems,

lwa_podelitems TYPE t_podelitems,

lwa_poitemsum TYPE t_podelitems,

li_poitemsum TYPE SORTED TABLE OF t_podelitems

WITH UNIQUE KEY ebeln ebelp,

lv_matnr TYPE mara-matnr,

lv_rcode TYPE sy-subrc.

*->> get the BPCS Purchase Order and there line items which

*->> are not completely delivered

SELECT ekkoebeln ekpoebelp ekkobukrs ekkobedat ekpo~menge txz01

werks matkl elikz meins netpr peinh

FROM ekko JOIN ekpo ON

ekpoebeln = ekkoebeln

INTO TABLE LI_OPENPOITEMS

WHERE bsart EQ lc_bpcs AND

ekko~bstyp EQ lc_f AND

ekko~ebeln IN s_ebeln AND

knttp EQ lc_r AND

elikz EQ space AND

ekpo~loekz EQ space AND

ekko~loekz EQ space.

IF sy-subrc EQ 0.

*->> get the sum of of delivered quantity for the

*->> purchase order scheduled items

SELECT ebeln ebelp wemng

FROM eket

INTO TABLE li_podelitems

FOR ALL ENTRIES IN li_openpoitems

WHERE ebeln = li_openpoitems-ebeln AND

ebelp = li_openpoitems-ebelp.

IF sy-subrc EQ 0.

*->> Sum the delivered quantity of each line item

LOOP AT li_podelitems INTO lwa_podelitems.

MOVE: lwa_podelitems TO lwa_poitemsum.

COLLECT lwa_poitemsum INTO li_poitemsum.

ENDLOOP.

ENDIF.

ELSE.

*->> Exit when there are no records selected

EXIT.

ENDIF. " if sy-subrc eq 0

*->> Get allthe purchase orders which needs to be changed

LOOP AT li_openpoitems INTO lwa_openpoitems.

*->> Read the table for delivered quantity

CLEAR lwa_poitemsum.

READ TABLE li_poitemsum INTO lwa_poitemsum

WITH KEY ebeln = lwa_openpoitems-ebeln

ebelp = lwa_openpoitems-ebelp BINARY SEARCH.

IF sy-subrc NE 0.

CONTINUE.

ENDIF.

*->> check if delivered QTY is GT PO QTY then skip

IF lwa_poitemsum-wemng GT lwa_openpoitems-menge.

CONTINUE.

ENDIF.

*->> Identify the material number from the description

*->> get the numberic part from the material description

*->> untill you find a character.

*->> get the length of the description

PERFORM get_material_from_description

USING lwa_openpoitems-txz01

CHANGING lv_matnr.

*->> If there is no numberic part in the desc. then error

IF lv_matnr = space.

MOVE-CORRESPONDING lwa_openpoitems TO wa_error.

MOVE: 'Material not found in the descrition'(001) TO wa_error-txt.

APPEND wa_error TO i_error.

CLEAR wa_error.

CONTINUE.

ENDIF.

*->> validate if it is a valid material.

PERFORM validate_material USING lv_matnr

CHANGING lv_rcode.

IF lv_rcode NE 0.

*->> Error the record and process next record

MOVE-CORRESPONDING lwa_openpoitems TO wa_error.

MOVE: 'Material not found in SAP'(002) TO wa_error-txt.

APPEND wa_error TO i_error.

CLEAR wa_error.

CONTINUE.

ENDIF.

*->> if the Purchase order quantity is greater than delivered quantity

*->> then change the purchase order item quantity and set delivery ind

*->> complete and create a new line item with the new material number

*->> with the remaining quantity

MOVE-CORRESPONDING lwa_openpoitems TO wa_updateitems.

IF lwa_poitemsum-wemng LE lwa_openpoitems-menge.

*->> now also we have to update the old item

wa_updateitems-elikz = c_i.

IF lwa_poitemsum-wemng GT 0.

wa_updateitems-menge_n = lwa_poitemsum-wemng.

ENDIF.

APPEND wa_updateitems TO i_updateitems.

wa_updateitems-ebelp_n = ''.

wa_updateitems-matnr = lv_matnr.

wa_updateitems-menge_n =

lwa_openpoitems-menge - lwa_poitemsum-wemng.

*->> Don't add when the line item is greater than 0

IF wa_updateitems-menge_n EQ 0.

CONTINUE.

ENDIF.

APPEND wa_updateitems TO i_updateitems.

ENDIF.

CLEAR wa_updateitems.

ENDLOOP.

ENDFORM. " get_purchase_order_data

&----


*& Form get_material_From

&----


  • Retrieves the material number from the description

----


FORM get_material_from_description

USING value(p_desc) TYPE ekpo-txz01

CHANGING p_matnr TYPE ekpo-matnr.

----


  • DATA DECLARATION *

----


DATA: lv_pos TYPE sy-tabix, " Index

lv_char TYPE c. " Get charater

  • lv_length(2) TYPE n. "#EC * " Length

CLEAR p_matnr.

DO 7 TIMES.

*->> Get each charater and check if it is numberic or not

lv_char = p_desc+lv_pos(1).

IF lv_char CO '0123456789'.

ELSE.

*->> if it finds the charater in the string then the initial part

*->> is the numberic part

IF lv_pos > 0.

p_matnr = p_desc+0(lv_pos).

EXIT. " Do loop.

ELSE.

EXIT.

ENDIF.

ENDIF.

lv_pos = lv_pos + 1.

ENDDO.

ENDFORM. " get_material_From

&----


*& Form validate_material

&----


  • Validate the material number found from the description

----


FORM validate_material USING value(p_matnr) TYPE mara-matnr

CHANGING p_rcode TYPE sy-subrc.

READ TABLE i_matnr WITH KEY matnr = p_matnr TRANSPORTING NO FIELDS.

IF sy-subrc NE 0.

SELECT matnr APPENDING TABLE i_matnr

FROM mara

WHERE matnr = p_matnr.

p_rcode = sy-subrc.

ELSE.

p_rcode = 0.

ENDIF.

ENDFORM. " validate_material

&----


*& Form call_bapi_po_change

&----


  • BAPI will be called here to update the line or add a new line item

----


FORM call_bapi_po_change .

DATA: lv_index TYPE sy-tabix, " Index

lv_purcno TYPE bapimepoheader-po_number, " Bapi PO variable

li_return TYPE TABLE OF bapiret2, " BAPI return

li_items TYPE TABLE OF bapimepoitem, " BAPI Item

li_itemx TYPE TABLE OF bapimepoitemx, " BAPI ITEMX

lwa_return TYPE bapiret2, " BAPI return heade

lwa_items TYPE bapimepoitem, " Item Wa

lwa_itemx TYPE bapimepoitemx, " ITEMx

lv_ebelp TYPE ekpo-ebelp. "#EC * " Purchase order

  • wa_cond TYPE bapimepocond. "#EC * "

  • li_cond TYPE TABLE OF bapimepocond.

SORT i_updateitems BY ebeln ebelp.

LOOP AT i_updateitems INTO wa_updateitems.

lv_index = sy-tabix.

*->> Purchase order for passing to the BAPI

lv_purcno = wa_updateitems-ebeln.

AT NEW ebeln.

*->> Get the Maximum line item number

CLEAR lv_ebelp.

SELECT MAX( ebelp ) INTO lv_ebelp

FROM ekpo

WHERE ebeln = lv_purcno.

lv_ebelp = lv_ebelp + 10.

ENDAT. " at new ebeln

*->> if material number is populated then it is the new item

*->>if not it is update of the old item

IF NOT wa_updateitems-matnr IS INITIAL.

wa_updateitems-ebelp_n = lv_ebelp.

MOVE: wa_updateitems-ebelp_n TO lwa_items-po_item,

wa_updateitems-ebelp_n TO lwa_itemx-po_item,

wa_updateitems-matnr TO lwa_items-material,

c_i TO lwa_itemx-material,

wa_updateitems-txz01 TO lwa_items-short_text,

c_i TO lwa_itemx-short_text,

wa_updateitems-werks TO lwa_items-plant,

c_i TO lwa_itemx-plant,

wa_updateitems-menge_n TO lwa_items-quantity,

c_i TO lwa_itemx-quantity,

wa_updateitems-meins TO lwa_items-po_unit,

c_i TO lwa_itemx-po_unit,

wa_updateitems-netpr TO lwa_items-net_price,

c_i TO lwa_itemx-net_price,

wa_updateitems-peinh TO lwa_items-price_unit,

c_i TO lwa_itemx-price_unit.

lv_ebelp = lv_ebelp + 10.

ELSE.

MOVE: wa_updateitems-ebelp TO lwa_items-po_item,

wa_updateitems-ebelp TO lwa_itemx-po_item,

c_i TO lwa_items-no_more_gr,

c_i TO lwa_itemx-no_more_gr.

IF wa_updateitems-menge_n GT 0.

MOVE: wa_updateitems-menge_n TO lwa_items-quantity,

c_i TO lwa_itemx-quantity.

ENDIF.

ENDIF.

APPEND lwa_items TO li_items.

APPEND lwa_itemx TO li_itemx.

CLEAR: lwa_items,lwa_itemx.

AT END OF ebelp.

*->> Get the correct values from the right hand side of the change

READ TABLE i_updateitems INTO wa_updateitems INDEX lv_index.

CALL FUNCTION 'BAPI_PO_CHANGE'

EXPORTING

purchaseorder = lv_purcno

TABLES

return = li_return

poitem = li_items

poitemx = li_itemx.

REFRESH: li_items,li_itemx.

*->> Check if there are any errrors in the PO

*->> if no error exists the commit the PO

*->>if not populate the error table.

*->> Delete message which you don't need

DELETE li_return WHERE id = 'MEPO' AND

number = '000'.

LOOP AT li_return INTO lwa_return WHERE type = 'E'.

MOVE-CORRESPONDING wa_updateitems TO wa_error.

MOVE:

wa_updateitems-ebelp_n TO wa_error-ebelp_n,

wa_updateitems-menge_n TO wa_error-menge_n,

lwa_return-number TO wa_error-msgnr,

lwa_return-message TO wa_error-txt.

APPEND wa_error TO i_error.

ENDLOOP.

IF sy-subrc NE 0.

*->> Commit the Purchase order

CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

EXPORTING

wait = c_i.

ENDIF. " if sy-subrc ne 0

REFRESH li_return.

ENDAT.

ENDLOOP.

ENDFORM. " call_bapi_po_change

&----


*& Form error_report

&----


  • Display errors in file or spool as the the execution of the program

----


FORM error_report .

*->> message on completion of the program

*->> Print the error report only when there are errors.

IF NOT i_error[] IS INITIAL.

*->> If the program is run in backgroung then send to spool.

IF sy-batch = c_i.

PERFORM send_to_spool.

ELSE.

PERFORM write_errors_to_file.

ENDIF.

MESSAGE s520.

ELSE.

MESSAGE s521.

ENDIF.

ENDFORM. " error_report

&----


*& Form write_errors_to_file

&----


  • all errrors when the program runs in foreground will be stored in the

  • the file as per the selection screen

----


FORM write_errors_to_file .

DATA: lv_file TYPE string,

wa_error TYPE t_error.

*->> prepare the heading for the download file.

wa_error-ebeln = 'Purchase order'(013).

wa_error-ebelp = 'Old Item No'(014).

wa_error-txz01 = 'Description'(015).

wa_error-menge = 'Old Qty'(007).

wa_error-ebelp_n = 'New Item no'(016).

wa_error-matnr = 'Material No'(009).

wa_error-menge_n = 'New Qty'(010).

wa_error-msgnr = 'Msg NO'(017).

wa_error-txt = 'Message Text'(018).

INSERT wa_error INTO i_error INDEX 1.

lv_file = p_file.

*->> Call the function to download

CALL FUNCTION 'GUI_DOWNLOAD'

EXPORTING

filename = lv_file

filetype = 'ASC'

write_field_separator = c_i

TABLES

data_tab = i_error

  • FIELDNAMES = x

EXCEPTIONS

file_write_error = 1

no_batch = 2

gui_refuse_filetransfer = 3

invalid_type = 4

no_authority = 5

unknown_error = 6

header_not_allowed = 7

separator_not_allowed = 8

filesize_not_allowed = 9

header_too_long = 10

dp_error_create = 11

dp_error_send = 12

dp_error_write = 13

unknown_dp_error = 14

access_denied = 15

dp_out_of_memory = 16

disk_full = 17

dp_timeout = 18

file_not_found = 19

dataprovider_exception = 20

control_flush_error = 21

OTHERS = 22.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

ENDFORM. " write_errors_to_file

&----


*& Form send_to_spool

&----


  • when the program is run in bacground the program will send the error

  • messages in to spool

----


FORM send_to_spool .

LOOP AT i_error INTO wa_error.

WRITE:/1 wa_error-ebeln,

11 wa_error-ebelp,

21 wa_error-txz01,

62 wa_error-menge DECIMALS 2 ,

82 wa_error-ebelp_n,

92 wa_error-matnr,

110 wa_error-menge_n DECIMALS 2,

130 wa_error-msgnr,

137 wa_error-txt.

ENDLOOP.

ENDFORM. " send_to_spool

&----


*& Form top_of_page

&----


  • This subroutine call the standard genentech top-of-page function

  • for displaying the top of the page

----


FORM top_of_page .

CALL FUNCTION 'Z_REPORT_HEADER'

EXPORTING

report_header = text-003

line_size = '255'

EXCEPTIONS

user_error = 1

OTHERS = 2.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

WRITE:/1 'PO Number'(004),

11 'Old itm'(005),

21 'Item Text'(006),

62 'Old Qty'(007),

82 'New Itm'(008),

92 'Material No'(009),

110 'New Qty'(010),

130 'Msgno'(011),

137 'Error text'(012).

ENDFORM. " top_of_page

Reward if helpful.

Regards,

Nagaraj

Read only

Former Member
0 Likes
893

int_bapi_item_data-invoice_doc_item = '0001'.

int_bapi_item_data-po_number = '6510003165'.

int_bapi_item_data-po_item = '0001'.

int_bapi_item_data-tax_code = 'E0'.

int_bapi_item_data-item_amount = '10000.00'.

APPEND int_bapi_item_data.

acc_data-invoice_doc_item = '0001'.

acc_data-xunpl = 'X'.

acc_data-tax_code = 'E0'.

acc_data-item_amount = '10000.00'.

acc_data-gl_account = '0009900001'.

APPEND acc_data.

wf_bapi_header_data-invoice_ind = 'X'.

wf_bapi_header_data-doc_type = 'RE'.

wf_bapi_header_data-doc_date = sy-datum.

wf_bapi_header_data-pstng_date = '20070806'.

CONCATENATE P_ipABrp p_ipabrj loc_date sy-uzeit INTO

wf_bapi_header_data-ref_doc_no.

wf_bapi_header_data-comp_code = '1000'.

wf_bapi_header_data-gross_amount = '122222' .

wf_bapi_header_data-currency = 'USD'.

wf_bapi_header_data-pmnttrms = 'NT00'.

CALL FUNCTION 'ZSMP_INVOICE_CREATE'

EXPORTING

headerdata = wf_bapi_header_data

IMPORTING

invoicedocnumber = wf_belnr

fiscalyear = wf_gjahr

TABLES

itemdata = int_bapi_item_data

accountingdata = acc_data

return = int_bapi_return_messages.

IF sy-subrc = 0.

CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'