Create a custom BAPI easily

BAPI's are very useful in SAP to perform some business function as well as to exchange data / interact between different SAP systems or Non SAP systems as well. Though SAP provides a whole bunch of ready to use BAPI's, one can create there own BAPI(s) easily if required.

Steps involved in creating a custom BAPI:

1 ) Create a BAPI import structure
Example : I am creating a BAPI import structure ZABPI_ADIIMPORT having MATNR component.





2 ) Create a BAPI return table structure:
I am creating a structure with matnr and werks , this will be output table of my BAPI.


3) Go to SE37 and create a custom BAPI FM(Function Module). ZBAPI_GET_WERKS is the bapi name. All custom BAPI FM’s should begin with ZBAPI*

Make the FM remote enabled by selecting Remote Enabled Module. All BAPI FM’s are RFC enabled.



Now add the bapi import structure we created in import tab. Always remember to tick Pass by value since BAPI FM which is a RFC enabled FM cannot use pass by reference.



 In the export tab we will put BAPI return parameters. BAPIRETURN is a standard BAPI return structure. In the source code of the program we need to make use of this parameter to show success or failure status along with messages.



 In the tables we will use our BAPI export table structure we created. We always use LIKE for tables.




 4) Source code:
Add the logic of the FM here.

FUNCTION ZBAPI_GET_WERKS.

*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(MATNR) TYPE  ZBAPI_ADIIMPORT-MATNR
*"  EXPORTING
*"     VALUE(RETURN) TYPE  BAPIRETURN
*"  TABLES
*"      ZTABLE STRUCTURE  ZBAPI_ADIEXPORT
*"----------------------------------------------------------------------

select matnr werks from marc
      
into TABLE ztable
        
where matnr eq matnr.
    
if sy-subrc eq 0.
      
"Update BAPI return table accordingly to show success here
      
else.
      
"Update BAPI return table accordingly to show failure here
    
ENDIF.
ENDFUNCTION.

 Logic of our BAPI is simple; it fetches matnr and werks value from marc table based on input matnr values and fills the BAPI table. Lets test the FM, click execute and input a test data




ZTABLE will be filled with values from marc table corresponding to input matnr value



 5 ) Place this Function Module in to BOR(Business object repository) using the T-code SWO1. Provide the name of the object and press create button.


 Add our Custom BAPI FM using Utilities = > API Method = >Add Method



 Press next to see the parameters involved



Press next and it will ask you that method has not been implemented, and will ask for confirmation to generate a template. Press Yes


 Go back and change the BOR status to Implemented.


 Next generate the BOR by clicking


 After generation of BOR , release the BOR.


 We can test the BOR by clicking execute


 Test is similar to FM test and we can see the result



Thats it :) . Our custom BAPI is now created. 
Now you can use this BAPI like other BAPI.To learn how to use BAPI in reports , refer