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

Bapi

Former Member
0 Likes
597

Hi

Can anybody give me step by step guide as how to use bapis that are already existing.

Regards

abaper

1 ACCEPTED SOLUTION
Read only

uwe_schieferstein
Active Contributor
0 Likes
473

Hello

In order to understand how BAPIs work you need to know how the corresponding transaction works and which are its mandatory input values. For example, BAPI_USER_CREATE1 corresponds to transaction SU01 (Create SAP user). To create a user in dialog you need to provide at least

- last name

- password

- user type

The same mandatory input needs to be fed to the BAPI's interface. Therefore, I like to describe BAPI's as <i>"dialog without dialog"</i>.

In contrast to normal (RFC-enabled) function modules BAPIs never raise any exceptions. Instead, they collect the messages and return them, usually as TABLES parameter (e.g. RETURN of type BAPIRET2).

Most BAPIs do not execute a commit work. Thus, if the returned messages do not indicate any problem you have to commit work yourself.

Example of a remotely called BAPI:


  CALL FUNCTION 'BAPI_USER_CREATE1'  destination '<rfc destination>'
     EXPORTING
       ...
     IMPORTING
       ...
     TABLES
       return = lt_return
     EXCEPTIONS
       system_failure            = 1 MESSAGE ld_rfc_msg
       communication_failure = 2 MESSAGE ld_rfc_msg.
" Note: when the BAPI is called via RFC add the default exceptions for RFC-calls

  IF ( syst-subrc NE 0 ).
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'  
          destination '<rfc destination>'.

  ELSE.
    LOOP AT lt_return TRANSPORTING NO FIELDS
                   WHERE ( type CA 'AEX' ).  " abort, error, dump message
      EXIT.
    ENDLOOP.
    IF ( syst-subrc = 0 ).
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'  
          destination '<rfc destination>'.
    ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'  
          destination '<rfc destination>'
        EXPORTING
          wait = 'X'.   " 'X' = synchronous, ' ' = asynchronous
    ENDIF.

  ENDIF.

Please note that when you call BAPIs remotely you have to do data conversion (external -> internal) yourself. For example, if you use BAPI_MATERIAL_SAVEDATA then you have to convert an external material number (e.g. '12345678') into the internal format with leading zeros.

Regards

Uwe

2 REPLIES 2
Read only

uwe_schieferstein
Active Contributor
0 Likes
474

Hello

In order to understand how BAPIs work you need to know how the corresponding transaction works and which are its mandatory input values. For example, BAPI_USER_CREATE1 corresponds to transaction SU01 (Create SAP user). To create a user in dialog you need to provide at least

- last name

- password

- user type

The same mandatory input needs to be fed to the BAPI's interface. Therefore, I like to describe BAPI's as <i>"dialog without dialog"</i>.

In contrast to normal (RFC-enabled) function modules BAPIs never raise any exceptions. Instead, they collect the messages and return them, usually as TABLES parameter (e.g. RETURN of type BAPIRET2).

Most BAPIs do not execute a commit work. Thus, if the returned messages do not indicate any problem you have to commit work yourself.

Example of a remotely called BAPI:


  CALL FUNCTION 'BAPI_USER_CREATE1'  destination '<rfc destination>'
     EXPORTING
       ...
     IMPORTING
       ...
     TABLES
       return = lt_return
     EXCEPTIONS
       system_failure            = 1 MESSAGE ld_rfc_msg
       communication_failure = 2 MESSAGE ld_rfc_msg.
" Note: when the BAPI is called via RFC add the default exceptions for RFC-calls

  IF ( syst-subrc NE 0 ).
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'  
          destination '<rfc destination>'.

  ELSE.
    LOOP AT lt_return TRANSPORTING NO FIELDS
                   WHERE ( type CA 'AEX' ).  " abort, error, dump message
      EXIT.
    ENDLOOP.
    IF ( syst-subrc = 0 ).
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'  
          destination '<rfc destination>'.
    ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'  
          destination '<rfc destination>'
        EXPORTING
          wait = 'X'.   " 'X' = synchronous, ' ' = asynchronous
    ENDIF.

  ENDIF.

Please note that when you call BAPIs remotely you have to do data conversion (external -> internal) yourself. For example, if you use BAPI_MATERIAL_SAVEDATA then you have to convert an external material number (e.g. '12345678') into the internal format with leading zeros.

Regards

Uwe

Read only

Former Member
0 Likes
473

Hi Abap,

Go to the below link for BAPI.

<a href="http://help.sap.com/saphelp_nw04/helpdata/en/e0/9eb2370f9cbe68e10000009b38f8cf/frameset.htm">Bapi_help_link</a>

How to use a standard Bapi is :

1. Go to SE37 give the BAPI name and see its documentation.

2. Assert which parameters are mandatory

3. Execute the bapi and give test data

4. It should create a record with your test data

5. Then send the same data in the same order programatically.

I am giving the code below for your reference.



*&---------------------------------------------------------------------*
*& Report  ZBAPI_CREATE_PO                                             *
*&                                                                     *
*&---------------------------------------------------------------------*
*& Program demonstrates the BAPI call to create Purchase Order         *
*& Minimum required parameters are used are as per the current         *
*& system configuration                                                *
*&---------------------------------------------------------------------*
REPORT  ZBAPI_CREATE_PO                         .
*&---------------------------------------------------------------------*
*DATA DECLARATION
CONSTANTS : C_X VALUE 'X'.

*Structures to hold PO header data
DATA : HEADER LIKE  BAPIMEPOHEADER   ,
       HEADERX LIKE  BAPIMEPOHEADERX .

*Internal Tables to hold PO ITEM DATA
DATA : ITEM   LIKE BAPIMEPOITEM  OCCURS 0 WITH HEADER LINE,
       ITEMX  LIKE BAPIMEPOITEMX OCCURS 0 WITH HEADER LINE,

*Internal table to hold messages from BAPI call
       RETURN LIKE BAPIRET2 OCCURS 0 WITH HEADER LINE.
data : w_header(40) value 'PO Header'.

data : ws_langu like sy-langu.

*text-001 = 'PO Header' - define as text element
selection-screen begin of block b1 with frame title text-001.
parameters : company like header-comp_code  default '2700'      ,
             doctyp  like HEADER-DOC_TYPE   default 'NB'        ,
             cdate   like HEADER-CREAT_DATE default sy-datum    ,
             vendor  like HEADER-VENDOR     default '0010000023',
             pur_org like HEADER-PURCH_ORG  default '2700'      ,
             pur_grp like HEADER-PUR_GROUP  default '001'       .

selection-screen end of block b1.

selection-screen begin of block b2 with frame title text-002.
parameters : item_num like ITEM-PO_ITEM  default '00001',
             material like ITEM-MATERIAL default 'CRANE'   ,
             plant    like ITEM-PLANT    default '2700' ,
             quantity like ITEM-QUANTITY default 100.

selection-screen end of block b2.



*&---------------------------------------------------------------------*
START-OF-SELECTION.
*&---------------------------------------------------------------------*
*DATA POPULATION
*&---------------------------------------------------------------------*
ws_langu = sy-langu.   "Language variable

*POPULATE HEADER DATA FOR PO
HEADER-COMP_CODE  = company    .
HEADER-DOC_TYPE   = doctyp     .
HEADER-CREAT_DATE = cdate      .
HEADER-VENDOR     = vendor     .
HEADER-LANGU      = ws_langu   .
HEADER-PURCH_ORG  = pur_org    .
HEADER-PUR_GROUP  = pur_grp    .

*&---------------------------------------------------------------------*
*POPULATE HEADER FLAG.
*&---------------------------------------------------------------------*
HEADERX-comp_code  = c_x.
HEADERX-doc_type   = c_x.
HEADERX-creat_date = c_x.
HEADERX-vendor     = c_x.
HEADERX-langu      = c_x.
HEADERX-purch_org  = c_x.
HEADERX-pur_group  = c_x.
HEADERX-doc_date   = c_x.


*&---------------------------------------------------------------------*
*POPULATE ITEM DATA.
*&---------------------------------------------------------------------*
ITEM-PO_ITEM  = item_num.
ITEM-MATERIAL = material.
ITEM-PLANT    = plant.
ITEM-QUANTITY = quantity.
APPEND ITEM.

*&---------------------------------------------------------------------*
*POPULATE ITEM FLAG TABLE
*&---------------------------------------------------------------------*
ITEMX-PO_ITEM    = item_num.
ITEMX-MATERIAL   = C_X.
ITEMX-PLANT      = C_X .
ITEMX-STGE_LOC   = C_X .
ITEMX-QUANTITY   = C_X .
ITEMX-TAX_CODE   = C_X .
ITEMX-ITEM_CAT   = C_X .
ITEMX-ACCTASSCAT = C_X .
APPEND ITEMX.

*&---------------------------------------------------------------------*
*BAPI CALL
*&---------------------------------------------------------------------*
CALL FUNCTION 'BAPI_PO_CREATE1'
  EXPORTING
    POHEADER                     = HEADER
    POHEADERX                    = HEADERX
*   POADDRVENDOR                 =
*   TESTRUN                      =
* IMPORTING
*   EXPPURCHASEORDER             =
*   EXPHEADER                    =
*   EXPPOEXPIMPHEADER            =
 TABLES
   RETURN                       = RETURN
   POITEM                       = ITEM
   POITEMX                      = ITEMX.

*&---------------------------------------------------------------------*
*Confirm the document creation by calling database COMMIT
*&---------------------------------------------------------------------*
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
 EXPORTING
   WAIT          = 'X'
* IMPORTING
*   RETURN        =
          .

end-of-selection.
*&---------------------------------------------------------------------*
*Output the messages returned from BAPI call
*&---------------------------------------------------------------------*
LOOP AT RETURN.
 WRITE / RETURN-MESSAGE.
ENDLOOP.

Reward points if useful,

Aleem.