Monday, December 3, 2007

EXAMPLE SHOWING HOW TO CREATE A BAPI

Programming a BAPI consists of some tasks like: •Defining BAPI Data structures ( using SE11 )
•Creating BAPI Function Modules (For each method)
•Defining BAPI Methods in the BOR
•Documentation of the BAPI
•Generate ALE interface for asynchronous BAPIs
•Generate and release

NOTE:
Here we will not be covering the two points – Documentation of BAPI and Generating ALE Interface for asynchronous BAPIs.

NOTE: You can use the BAPI explorer (T-code BAPI) to get a step-by-step instruction / checklist in how to create a BAPI. In the BAPI explorer select the Project tab.


EXAMPLE – HOW TO CREATE A BAPI
In the example we will create a BAPI that reads some information about the line items for a Sales Invoice from table VBRP based on the Invoice No. that is supplied to the import parameter of the BAPI Function Module.

BAPI Name ZGetInvoiceItems
Function group ZBAPIVIN
Function module: ZBAPI_GET_BILL_ITEMS
FM Import parameters : IV_BILLNO TYPE ZBAPI_BILL_ITEMS-VBELN
FM Tables :IT_VBRP LIKE ZBAPI_BILL_ITEMS
FM Export parameters : RETURN LIKE BAPIRETURN

Defining BAPI Structures

This is the basic step followed while creating BAPIs. All the relevant structures that are required for BAPI have to be created using T-Code SE11. The structures can be created for Import/Tables parameters. Use Data type -> Structure

In our case we do not have multiple inputs but just one input i.e. Sales Invoice No. and so we have not made use of any structure for the purpose. But if required, structure can be used for import parameter also.

The following are the components of structure ZBAPI_BILL_ITEMS:

Field Name Description
VBELN Invoice Number
POSNR Invoice Item Number
MATNR Material Number
FKIMG Quantity
VRKME Sales Units (Quantity)
NETWR Amount

Point of Caution
It is required to define a structure for every parameter in the BAPI and use of same structures which are used in existing applications cannot be done because BAPI structures are frozen when BAPIs are released and then there are restrictions on changing them.


Screenshot of Structure – ZBAPI_BILL_ITEMS



Creating BAPI Function Modules (For each method)
We must create new function group for each BAPI. If the BAPIs are related then the same can be grouped under the same FUNCTION GROUP to enable the sharing of global data amongst the related BAPIs

Screenshot of Attributes Tab in the FM ZBAPI_BILL_ITEMS


Screenshot of Import Parameters Tab in the FM ZBAPI_BILL_ITEMS


Screenshot of Export Parameters Tab in the FM ZBAPI_BILL_ITEMS


NOTE:
Since Remote Enabled module processing type is selected and the Import/Export parameters can only be BY VALUE for an RFC enabled function module, select the checkbox for Pass Value for each IMPORT/EXPORT parameter.

Screenshot of Tables Tab in the FM ZBAPI_BILL_ITEMS



Code in the Function Module ZBAPI_BILL_ITEMS &
related Includes in the Function Group


INCLUDE LZBAPISTATUSUXX
*****************************************************************
* THIS FILE IS GENERATED BY THE FUNCTION LIBRARY. *
* NEVER CHANGE IT MANUALLY, PLEASE! *
*****************************************************************
INCLUDE LZBAPIVINU01.
"ZBAPI_GET_BILL_ITEMS

INCLUDE LZBAPIVINTOP “ Global data
FUNCTION-POOL ZBAPIVIN. "MESSAGE-ID

TABLES: VBRK, VBRP.
DATA: T_VBRP LIKE ZBAPI_BILL_ITEMS OCCURS 0.

* STRUCTURE FOR RETURN MESSAGES BY BAPI FUNCTION MODULE
DATA:
BEGIN OF MESSAGE,
MSGTY LIKE SY-MSGTY,
MSGID LIKE SY-MSGID,
MSGNO LIKE SY-MSGNO,
MSGV1 LIKE SY-MSGV1,
MSGV2 LIKE SY-MSGV2,
MSGV3 LIKE SY-MSGV3,
MSGV4 LIKE SY-MSGV4,
END OF MESSAGE.

INCLUDE LZBAPIVINU01 - Subroutines
***INCLUDE LZBAPIVINU01.
FUNCTION ZBAPI_GET_BILL_ITEMS.
*"----------------------------------------------------------------------
*"*"Local interface:
*" IMPORTING
*" VALUE(IV_BILLNO) TYPE VBELN
*" EXPORTING
*" VALUE(RETURN) TYPE BAPIRETURN
*" TABLES
*" IT_VBRP STRUCTURE ZBAPI_BILL_ITEMS
*"----------------------------------------------------------------------
* Check if the Invoice exists
select single *
from vbrk
where vbeln eq iv_billno.
if sy-subrc ne 0.
* If not return the error message
clear message.
message-msgty = 'E'.
message-msgid = 'Z3'.
message-msgno = '001'.
message-msgv1 = iv_billno.
perform return_bapi_message using message
changing return.
exit.
endif.

* If the Invoice exists, get all the required item lines information
* in the table it_vbrp
refresh it_vbrp.
clear vbrp.

select vbeln posnr matnr fkimg vrkme netwr
into table it_vbrp
from vbrp
where vbeln eq iv_billno.

ENDFUNCTION.

*---------------------------------------------------------------------*
* FORM RETURN_BAPI_MESSAGE *
*---------------------------------------------------------------------*
* --> VALUE(IV_MESSAGE) *
* --> XV_RETURN *
*---------------------------------------------------------------------*
form return_bapi_message using value(iv_message) like message
changing xv_return like bapireturn.

check not message is initial.

call function 'BALW_BAPIRETURN_GET'
exporting
type = iv_message-msgty
cl = iv_message-msgid
number = iv_message-msgno
par1 = iv_message-msgv1
par2 = iv_message-msgv2
par3 = iv_message-msgv3
par4 = iv_message-msgv4
importing
bapireturn = xv_return
exceptions
others = 1.

endform.

No comments:

Blog Archive