2007 Oct 15 9:27 AM
Hi all,
Can any one give me an real time exmpale of bapi,
and explain the functionality of that bapi clearly,
I will give u full points.
Thanks®ards
Bhushan
Hi all,
Can any one give me an real time exmpale of bapi,
and explain the functionality of that bapi clearly,
I will give u full points.
Thanks®ards
Bhushan
2007 Oct 15 9:35 AM
HI,
BAPI <b>BAPI_BUPA_FS_CREATE_FROM_DATA2 </b> is used to creat Business partner in CRM. Check the code below:
In this program ut_data is a internal table which is having the data to be used for creating business partners.
LOOP AT ut_data.
w_centraldata-searchterm1 = ut_data-first_name.
w_centraldata-searchterm2 = ut_data-last_name.
w_centralperson-firstname = ut_data-first_name.
w_centralperson-lastname = ut_data-last_name.
w_centralperson-middlename = ut_data-middle_name.
w_address-str_suppl1 = ut_data-street2.
w_address-str_suppl2 = ut_data-street3.
w_address-city = ut_data-city.
w_address-postl_cod1 = ut_data-postal_code.
w_address-region = ut_data-state.
w_address-country = ut_data-country.
i_telephone-r_3_user = '2'.
APPEND i_telephone.
i_telephone-telephone = ut_data-telephone.
i_telephone-r_3_user = '1'.
APPEND i_telephone.
i_email-e_mail = ut_data-email.
APPEND i_email.
i_partner_role-partnerrole = ut_data-role.
APPEND i_partner_role.
Create the Business Partner
CALL FUNCTION 'BAPI_BUPA_FS_CREATE_FROM_DATA2'
EXPORTING
partnercategory = c_partnercat
centraldata = w_centraldata
centraldataperson = w_centralperson
addressdata = w_address
IMPORTING
businesspartner = g_buspartner
TABLES
telefondata = i_telephone
faxdata = i_fax
e_maildata = i_email
return = i_bapiret2
roles = i_partner_role.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
ENDLOOP.
It is mandatory to commit the transaction after calling any BAPI so as to reflect the changes in database tables.
Regards,
Amit
Message was edited by:
Amit Kumar
2007 Oct 15 9:42 AM
Hi Bhushan,
Bapis....
These are the system defined function modules which are used to perform some operations which will affect more than one table(s), through user defined programs.
Without these BAPIs it is difficult to achieve this and it is not adviceble, because for performing these operations there will be a need of lot of conditions have to be checked. These things are difficult to write manual coding
For example BAPI_AR_ACC_GETOPENITEMS...
This function module will give Customer account open items at a key date.
If we want to write code manually we need to follow lot of conditions, which you can check by going inside the function module.
I think at least this will be helpful for you.
2007 Oct 15 9:56 AM
hi
good
try this links
For Step by Step with screen shots refer
https://www.sap-img.com/abap/bapi-step-by-step-guidance.htm
http://www.erpgenie.com/sap/abap/bapi/example.htm
reward point if helpful.
thanks
mrutyun^
2007 Oct 15 9:58 AM
Hi,
chek this
Program Logic : This program is used to modify all BPCS open *
Purchase Orders in SAP system. material numbers *
will be populated into PO line items by matching*
materialtext. BAPI_PO_CHANGE will be used to *
update the purchase order *
&----
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
Regards,
Nagaraj
| User | Count |
|---|---|
| 3 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |