BAPI

BAPI'S

BUSINESS APPLICATION PROGRAMMING INTERFACE


Bapi is also a remote enabled function module which is used to communicate between sap to sep or sap to non sap servers.


BAPI uses RFC Technology , At initial Stage it is a RFC Function Module.


Later this RFC Function Module will be inserted into  Business Object , then it is converted into BAPI.


BUSINESS OBJECT (BO) 


 BO is a collections of  ATTRIBUTES

                                    METHODS -> BAPI -> FUNCTIONS
                                    EVENTS -> Action Raised by User
                                    KEY FIELDS
                                    INTERFACES

For a Transaction (Tcode) .


EXAMPLE


MM01 is the Tcode  and the BO will be BUS1001.


SWO1 is the TCODE for BO.


RULES FOR BAPI


Bapi should be created with BAPI or ZBAPI.


The importing and Exporting Parameters should be of type Structures not the direct Data types. 


BAPI parameters Should be PASS BY VALUE.


A Return parameter to store Success / Failure messages as below


RETURN LIKE BAPIRET2 ---> under TABLE OPTION


If BAPI is successful we need to use BAPI_TRANSACTION_COMMIT to save data into database tables.


If BAPI is unsuccessful we need to use BAPI_TRANSACTION_ROLLBACK to rever back the chnages made to database tables.




 EXAMPLE PROGRAM ON HOW TO CREATE A CUSTOM BAPI  TO DISPLAY VTTK HEADER DATA WHICH CAN BE USED BY NON-SAP



STEP 1 


Need to Create a Structures for IMPORTING and EXPORTING Parameters.


Go to SE11 Tcode.


Give a Name as ZBAPI_TKNUM (This is importing parameter)


Click on CREATE




 Choose TYPE as STRUCTURE and Press ENTER






 Provide the Details as below




  Save and Activate It.


Create one more Structure with Name as ZBAPI_VTTK (This is for Exporting)


Give Name as ZBAPI_VTTK and Click on Create icon.





 Then Select TYPE as STRUCTURE and Press ENTER.





Provide some details as below





Save and Activate It.

 


STEP 2


Go to SE37 Tcode and Give a names as ZBAPI_VTTK_HEADER and click on create icon


provide some description and give FUNCTION GROUP Name and click on SAVE.


Function Groups can be created using Tcode SE80 and select OBJECT CATEGORY as FUNCTION GROUP and Provide a name to it and press ENTER.





 Then go to IMPORT tab .


Select Check Box PASS VALUE and provide the data as below.






 Go to EXPORT Tab.


Select Check Box PASS VALUE and provide the data as below.




 


Now need to specify RETURN Parameter.


Go to TABLES Tab and declare it as  Below.







Go to ATTRIBUTES Tab and make it REMOTE ENABLED MODULE.






Go to SOURCE CODE Tab and write the business logic.


FUNCTION ZBAPI_VTTK_HEADER.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(IM_TKNUM) TYPE  ZBAPI_TKNUM
*"  EXPORTING
*"     VALUE(EX_VTTK) TYPE  ZBAPI_VTTK
*"  TABLES
*"      RETURN STRUCTURE  BAPIRET2
*"----------------------------------------------------------------------

SELECT SINGLE *
  
FROM VTTK INTO CORRESPONDING FIELDS OF EX_VTTK
  WHERE TKNUM = IM_TKNUM.

 
ENDFUNCTION. 



SAVE AND ACTIVATE IT. 



STEP 3 

CREATING BO


Go to Tcode SWO1.


Provide a name and Click on CREATE.







Fill all Mandatory Fields and in APPLICATION Give as '*'. and click on CONTINUE.






 Click on SAVE.


Go to UTILITIES.


Select API -> ADD METHOD.






Provide the FUNCTION MODULE NAME as ZBAPI_VTTK_HEADER AND CLICK ON continue.






Click on the ICON (  NEXT STEP) 






Again click on the same icon. (NEXT STEP).






Click on YES. 







Expand the METHODS.







Double click on our METHOD  .






 click on ABAP Tab.







Our Bapi will be displayed.


Click on SAVE.


Go to EDIT -> CHANGE RELEASE STATUS -> OBJECT TYPE -> TO IMPLEMENTED.



 

Again go to EDIT -> CHANGE RELEASE STATUS -> OBJECT TYPE -> RELEASE.





Then we need to get a TICK mark for our BO as below.





EXAMPLE PROGRAM ON BAPI


BAPI TO READ MATERIAL MASTER DATA 


Here need to read material master data we use BAPI_MATERIAL_GET_DETAIL.


We can find BAPI'S Using TCODE  SE37.


Go to Tcode se37 and provide a name as below.





Click on F4 Button ,we will get list of BAPI's for material as below.






By reading description we can choose the bapi as per requirement.


Go to Tcode SE38 and create a program to get DETAILS of MATERIAL.




REPORT ZREPORT.
*&---------------------------------------------------------------------*
*& Report  ZBAPI_TO_GET_DETAIL
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT  ZBAPI_TO_GET_DETAIL.
**************************************************************************
" FOR PARAMETER TYPE WE NEED TO DOUBLE CLICK ON OUR FUNCTION, GO TO IMPORT TAB
" COPY THE ASSOCIATE TYPE OF THE PARAMETER NAME "MATERIAL".
**************************************************************************
PARAMETERS P_MATNR TYPE BAPIMATDET-MATERIAL.
**************************************************************************
" FOR WA_MARA DATA TYPE WE NEED TO DOUBLE CLICK ON OUR FUNCTION, GO TO EXPORT TAB
" COPY THE ASSOCIATE TYPE OF THE PARAMETER NAME MATERIAL_GENERAL_DATA.
***************************************************************************
DATA WA_MARA TYPE BAPIMATDOA.   " WORK AREA DECLARATION
**************************************************************************
" FOR WA_MARA DATA TYPE WE NEED TO DOUBLE CLICK ON OUR FUNCTION, GO TO EXPORT TAB
" COPY THE ASSOCIATE TYPE OF THE PARAMETER NAME RETURN
***************************************************************************
DATA WA_RETURN TYPE BAPIRETURN" DECLARATION FOR RETURN (ERROR/SUCCESS MESSAGES)
***********************************************
" CALLING THE BAPI BY USING PATTERN ICON
" SELECT CALL FUNCTION AND PROVIDE THE NAME AS
" BAPI_MATERIAL_GET_DETAIL
***********************************************
CALL FUNCTION 'BAPI_MATERIAL_GET_DETAIL'
  EXPORTING
    MATERIAL                    P_MATNR  " PARAMETER
*   PLANT                       =
*   VALUATIONAREA               =
*   VALUATIONTYPE               =
*   MATERIAL_EVG                =
 IMPORTING
   MATERIAL_GENERAL_DATA       WA_MARA  " WORK AREA
    RETURN                      WA_RETURN
*   MATERIALPLANTDATA           =
*   MATERIALVALUATIONDATA       =
          .
IF WA_RETURN-TYPE 'S'.   " S= SUCCESS MEANS IT DISPLAYS THE DATA
  WRITE:/ WA_MARA-MATL_GROUP,WA_MARA-MATL_DESC,
          WA_MARA-OLD_MAT_NO,WA_MARA-MATL_TYPE.
ELSE.
  WRITE:/ WA_RETURN-MESSAGE COLOR 3" IF NOT BAPI MESSAGES WILL DISPLAY (ERROR)
ENDIF.

***********************************************************************
" FOR DISPLAYING THE OUTPUT WE SHOULD USE THE FIELD NAMES AS PER IN BAPI
" SO WE NEED TO DOUBLE CLICK ON OUR TYPE WORK AREA TYPE
" IN THAT WE WILL HAVE ALL THE FIELD NAMES
***********************************************************************

OUTPUT:






IF WE ENTERED WRONG MATERIAL NUMBER








EXAMPLE PROGARM ON BAPI


PROGRAM TO WRITE THE DATA BY USING BAPI WITH FLAT FILE.


HERE WE NEED WRITE THE MATERIAL MASTER DATA USING THE BAPI_MATERIAL_SAVEDATA. 



WE CAN CHECK OUR REQUIRED BAPI USING TCODE SE37 AS EXPLAINED ABOVE.


FLAT FILE CREATION




IN THE ABOVE FLAT FILE NOKIA1103 AND NOKIA 1104 ARE ERROR RECORDS.



REPORT ZREPORT22 NO STANDARD PAGE HEADING.

INCLUDE ZREPORT22_TOP" DECLARATION

*&---------------------------------------------------------------------*
*&  Include           ZREPORT22_TOP
*&---------------------------------------------------------------------*

TYPES BEGIN OF TY_FILE,
        MATNR TYPE MARA-MATNR,
        MBRSH TYPE MARA-MBRSH,
        MTART TYPE MARA-MTART,  " STRUCTURE WITH HOLDS THE FLAT FILE DATA
        MAKTX TYPE MAKT-MAKTX,
        MEINS TYPE MARA-MEINS,
      END OF TY_FILE.

DATA I_FILE TYPE TABLE OF TY_FILE .   " FLAT FILE INTERNAL TABLE AND WORK ARAEA DECLARATION
DATA WA_FILE TYPE TY_FILE .

DATA WA_HEADDATA TYPE BAPIMATHEAD,

       WA_CLIENTDATA TYPE BAPI_MARA,
       WA_CLIENTDATAX TYPE BAPI_MARAX,

       I_MARA TYPE TABLE OF  BAPI_MAKT,
       WA_MARA TYPE BAPI_MAKT ,

       I_RETURNMSGS  TYPE TABLE OF BAPI_MATRETURN2,
       WA_RETURNMSGS TYPE BAPI_MATRETURN2,

       WA_RETURN TYPE BAPIRET2. 


*****************************************************************
" UPLOADING THE DATA INTO INTERNAL TABLE I_FILE USING GUI_UPLOAD
*****************************************************************

CALL FUNCTION 'GUI_UPLOAD'
  EXPORTING
    FILENAME            'BAPI.TXT'
    FILETYPE            'ASC'
    HAS_FIELD_SEPARATOR 'X'
  TABLES
    DATA_TAB            I_FILE.

LOOP AT I_FILE INTO WA_FILE .

  WA_HEADDATA-MATERIAL WA_FILE-MATNR .
  WA_HEADDATA-MATL_TYPE WA_FILE-MTART .    "FIELD MAPPING FROM FLAT FILE
  WA_HEADDATA-IND_SECTOR WA_FILE-MBRSH .
  WA_HEADDATA-BASIC_VIEW 'X' .

  WA_CLIENTDATA-BASE_UOM WA_FILE-MEINS.    " FIELD MAPPING FROM FLAT FILE
  WA_CLIENTDATAX-BASE_UOM 'X'.

  WA_MARA-LANGU 'EN' .
  WA_MARA-MATL_DESC WA_FILE-MAKTX .       " FIELD MAPPING FROM FLAT FILE

  APPEND WA_MARA TO I_MARA .               " APPENDING THE DATA

*****************************************************************
  " BAPI TO SAVE THE MATERIAL MASTER DATA
****************************************************************

  CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
    EXPORTING
      HEADDATA            WA_HEADDATA
      CLIENTDATA          WA_CLIENTDATA
      CLIENTDATAX         WA_CLIENTDATAX
    IMPORTING
      RETURN              WA_RETURN
    TABLES
      MATERIALDESCRIPTION I_MARA
      RETURNMESSAGES      I_RETURNMSGS.

  IF  WA_RETURN-TYPE 'S'.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'  " INSERTS THE DATA INTO DATABASE TABLE
      EXPORTING
        WAIT 'X'.
   WRITE / WA_RETURN-MESSAGE .    " DISPLAYS THE BAPI MESSAGES OF SUCCESSFUL RECORDS

  ELSEIF   WA_RETURN-TYPE 'E'.

    LOOP AT I_RETURNMSGS INTO WA_RETURNMSGS WHERE TYPE 'E'.
      WRITE / WA_RETURNMSGS-MESSAGE COLOR 3" ERROR MESSAGES WILL BE DISPLAYED WHICH ARE NOT PROCESSED
    ENDLOOP.

  ENDIF .

  CLEAR WA_FILE,
          WA_HEADDATA,
          WA_CLIENTDATA,
          WA_CLIENTDATAX,
          WA_MARA,
          WA_RETURNMSGS,
          WA_RETURN .
  REFRESH I_MARA I_RETURNMSGS .
ENDLOOP .

OUTPUT:





NOW WE CAN GO TO SE11 AND SELECT MARA TABLE THERE WE CAN SEE OUR  3 RECORDS.





EXAMPLE REPORT ON CREATION OF SALES ORDER USING BAPI


REPORT  ZBAPI_SALESORDER NO STANDARD PAGE HEADING.
**************************************************
" DECLARATIONS FOR BAPI VARIABLES
**************************************************
DATAHEADER_DATA LIKE BAPISDHD1,
      HEADER_DATAX 
LIKE BAPISDHD1X,

      I_PATNERS 
TYPE TABLE OF BAPIPARNR,
      W_PATNERS 
TYPE BAPIPARNR,

      V_VBELN 
TYPE VBAK-VBELN,

      I_RETURN 
TYPE TABLE OF BAPIRET2,
      W_RETURN 
TYPE BAPIRET2.
*********************************************
" HEADER INFORMATION
*********************************************

HEADER_DATA
-DOC_TYPE      'TA'.
HEADER_DATA
-SALES_ORG     '1020'.
HEADER_DATA
-DISTR_CHAN    '22'.
HEADER_DATA
-DIVISION      '00'.

HEADER_DATAX
-DOC_TYPE     'X'.
HEADER_DATAX
-SALES_ORG    'X'.
HEADER_DATAX
-DISTR_CHAN   'X'.
HEADER_DATAX
-DIVISION     'X'.
**********************************************
" PARTNER INFORMATION
**********************************************

W_PATNERS
-PARTN_ROLE     'AG'.
W_PATNERS
-PARTN_NUMB     '0000007777'.

APPEND W_PATNERS TO I_PATNERS" DATA IS APPENDING

***************************************************
" THIS BAPI IS USED TO CREATE CUSTOMER SALES ORDER
***************************************************
CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
  
EXPORTING
*   SALESDOCUMENTIN               =
    ORDER_HEADER_IN               
HEADER_DATA
    ORDER_HEADER_INX              
HEADER_DATAX*   SENDER                        =
*   BINARY_RELATIONSHIPTYPE       =
*   INT_NUMBER_ASSIGNMENT         =
*   BEHAVE_WHEN_ERROR             =
*   LOGIC_SWITCH                  =
*   TESTRUN                       =
*   CONVERT                       = ' '
    
IMPORTING
    SALESDOCUMENT                 
V_VBELN
  
TABLES
    
RETURN                        I_RETURN*   ORDER_ITEMS_IN                =
*   ORDER_ITEMS_INX               =
    ORDER_PARTNERS                
I_PATNERS*   ORDER_SCHEDULES_IN            =
*   ORDER_SCHEDULES_INX           =
*   ORDER_CONDITIONS_IN           =
*   ORDER_CONDITIONS_INX          =
*   ORDER_CFGS_REF                =
*   ORDER_CFGS_INST               =
*   ORDER_CFGS_PART_OF            =
*   ORDER_CFGS_VALUE              =
*   ORDER_CFGS_BLOB               =
*   ORDER_CFGS_VK                 =
*   ORDER_CFGS_REFINST            =
*   ORDER_CCARD                   =
*   ORDER_TEXT                    =
*   ORDER_KEYS                    =
*   EXTENSIONIN                   =
*   PARTNERADDRESSES              =
*   EXTENSIONEX                   =
          
.

IF V_VBELN IS NOT INITIAL.
  
WRITE:/ V_VBELN'CREATION DONE' COLOR 3.

  
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
  
EXPORTING
    
WAIT 'X'.

  
ELSE.

    
LOOP AT I_RETURN INTO W_RETURN.
      
WRITE:/ W_RETURN-MESSAGE.
      
ENDLOOP.
      
ENDIF.

output:



If we want to check whether creation is done or not.

Go to Tcode VA03 and give order type as 12065 and press enter.