Application Development 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: 

how to use bapi

Former Member
0 Kudos

hi ,

i got a requirement to use a bapi instead of bdc.

how to use any standard bapi,if anyone of u used bapi can u send me the sample code.can anyone help on this?

thanks in advance

sriram

1 ACCEPTED SOLUTION
10 REPLIES 10

former_member181962
Active Contributor
0 Kudos

Hi Srirama,

This is a smaple bapi usage:

http://www.sap-img.com/abap/bapi-goodsmvt-create-to-post-goods-movement.htm

Instead of MIFO transaction.

Regards,

Ravi

Former Member
0 Kudos

Hi sriram,

u can consider bapi as a function module n pass the parameters that r required fr the bapi. first u need to find out if a bapi exists for the particular upload..then within the bapi, read the documentations that clearly mentions the working of the parameters...

we had used a bapi for PO ( me21n ) upload.. send me ur id..i ll put it up there,...

hope it hwlps,

Regards,

Bikash

Former Member
0 Kudos

Hi,

BAPI is like function module ; fill data in to the parameters ; in the same way like FM. analyze the table and relationship.

Regards

Manoj

Former Member
0 Kudos

Hi,

Refer this link for complete guide to BAPI.

Alternatively if u giv ur Email id i can giv u document which explains step by step example.

http://www.sapgenie.com/abap/bapi/index.htm

Regards,

Latheesh

0 Kudos

Hello friends,

If its not too much, please send me too some docs about BAPI and other SAP/ABAP related docs. my e-mail ID is viraylab@yahoo.com. Thanks a lot!

Former Member

Former Member
0 Kudos

Hi!!

Bapis are used just like a fuction module. Only thing to take care is its interface parameters. Those should be right and Bapis return a internal table for exceptions. no sy-subrc check is done on a BAPI.

Sample code:

report ztypes no standard page heading message-id z5.

----


*Purpose: To check Delivery Completion Date in Transaction ME23N for all

  • purchase orders whose Delivery Completion Date is unchecked

----


  • Type Declaration *

----


types: begin of type_ekpo, "Internal Table for Purchasing Document Item

ebeln type ebeln, "Purchasing Document Number

ebelp type ebelp, "Item Number of Purchasing Document

elikz type elikz, "Delivery Completed Indicator

bukrs type bukrs, "Company Code

ekorg type ekorg,

end of type_ekpo.

types: begin of i_return.

include structure bapiret2.

types: end of i_return.

types: begin of i_poitem.

include structure bapimepoitem.

types: end of i_poitem.

types: begin of i_poitemx.

include structure bapimepoitemx.

types: end of i_poitemx.

types: begin of i_poheader.

include structure bapimepoheader.

types: end of i_poheader.

types: begin of i_poheaderx.

include structure bapimepoheaderx.

types: end of i_poheaderx.

types: type_t_ekpo type standard table of type_ekpo,

type_t_poitem type standard table of i_poitem,

type_t_poitemx type standard table of i_poitemx,

type_t_return type standard table of i_return.

----


  • Internal Table Definition *

----


data: t_ekpo type type_t_ekpo.

data: t_poitem type type_t_poitem,

t_poitemx type type_t_poitemx,

t_return type type_t_return.

----


  • Variables *

----


data: v_ebeln type ekko-ebeln. "Variable used for validation at selection screen level

data: v_ebeln1 type ekko-ebeln.

data: v_bukrs type ekko-bukrs.

data: v_ekorg type ekko-ekorg.

----


  • Workareas used for different types *

----


data: s_ekpo type type_ekpo,

s_poitem type i_poitem,

s_poitemx type i_poitemx,

s_return type i_return,

t_poheader type i_poheader,

t_poheaderx type i_poheaderx.

----


  • Selection-Screen *

----


selection-screen begin of block b1 with frame title text-001.

select-options: s_ebeln for v_ebeln1 obligatory.

select-options: s_bukrs for v_bukrs.

select-options: s_ekorg for v_ekorg.

selection-screen end of block b1.

----


  • At Selection-Screen *

----


at selection-screen.

if not s_ebeln is initial.

select single ebeln

into v_ebeln

from ekko

where ebeln in s_ebeln.

if sy-subrc <> 0.

message e035.

endif.

endif.

----


  • Start of Selection *

----


start-of-selection.

perform z_query.

*CHANGING DELIVERY INDICATOR USING BAPI

perform z_change.

*&----


*& Form Z_QUERY

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


form z_query .

  • Get the purchase orders from S_EBELN into the range T_EBELN

refresh t_ekpo.

clear t_ekpo.

if not s_ebeln is initial.

select pebeln pbukrs pekorg qebelp q~elikz

from ekko as p

inner join ekpo as q

on pebeln = qebeln

into corresponding fields of table t_ekpo

where p~ebeln in s_ebeln

and p~bukrs in s_bukrs

and p~ekorg in s_ekorg

and q~elikz <> 'X'.

if sy-subrc <> 0.

message e035.

else.

sort t_ekpo by ebeln ebelp.

endif.

endif.

endform. " Z_QUERY

&----


*& Form Z_CHANGE

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


form z_change .

*LOOPING AT I_EKPO

loop at t_ekpo into s_ekpo.

refresh t_poitem.

clear t_poitem.

refresh t_poitemx.

clear t_poitemx.

clear s_poitem.

clear s_poitemx.

clear s_return.

clear t_poheader.

clear t_poheaderx.

t_poheader-po_number = s_ekpo-ebeln.

t_poheaderx-po_number = 'x'.

t_poheader-comp_code = s_ekpo-bukrs.

t_poheaderx-comp_code = 'x'.

t_poheader-purch_org = s_ekpo-ekorg.

t_poheaderx-purch_org = 'X'.

s_poitem-po_item = s_ekpo-ebelp.

s_poitem-no_more_gr = 'X'.

append s_poitem to t_poitem.

s_poitemx-po_item = s_ekpo-ebelp.

s_poitemx-po_itemx = 'X'.

s_poitemx-no_more_gr = 'X'.

append s_poitemx to t_poitemx.

*CALL BAPI TO CHANGE PO

call function 'BAPI_PO_CHANGE'

exporting

purchaseorder = s_ekpo-ebeln

poheader = t_poheader

poheaderx = t_poheaderx

tables

return = t_return

poitem = t_poitem

poitemx = t_poitemx.

*COMMIT TRANSACTION

read table t_return with key type = 'E' into s_return.

if sy-subrc = 0.

perform z_rollback.

else.

perform z_commit.

endif.

clear s_ekpo.

endloop.

endform. " Z_CHANGE

&----


*& Form Z_ROLLBACK

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


form z_rollback .

call function 'BAPI_TRANSACTION_ROLLBACK'.

endform. " Z_ROLLBACK

&----


*& Form Z_COMMIT

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


form z_commit .

call function 'BAPI_TRANSACTION_COMMIT'.

endform. " Z_COMMIT

Regards,

Sangeeta.

Plz donot forget to award points.

former_member188685
Active Contributor
0 Kudos

Hi,

Check the sample code to upload material master data using BAPI.

REPORT Z_BAPI_MAT.


**********************************************************************
*                STRUCTURE  DECLARATIONS                             *
**********************************************************************

TABLES: BAPIMATHEAD,  "Headerdata
        BAPI_MARA,    "Clientdata
        BAPI_MARAX,   "Clientdatax
        BAPI_MARC,    "Plantdata
        BAPI_MARCX,   "Plantdatax
        BAPI_MAKT,    "Material description
        BAPIRET2.     "Return messages

DATA:V_FILE TYPE STRING.


DATA:
  BEGIN OF LSMW_MATERIAL_MASTER,
    MATNR(018) TYPE C,  "Material number
    MTART(004) TYPE C,  "Material type
    MBRSH(001) TYPE C,  "Industry sector
    WERKS(004) TYPE C,  "Plant
    MAKTX(040) TYPE C,  "Material description
    DISMM(002) TYPE C,  "Extra Field Added In the Program as itsrequired
    MEINS(003) TYPE C,  "Base unit of measure
    MATKL(009) TYPE C,  "Material group
    SPART(002) TYPE C,  "Division
    LABOR(003) TYPE C,  "Lab/office
    PRDHA(018) TYPE C,  "Product hierarchy
    MSTAE(002) TYPE C,  "X-plant matl status
    MTPOS_MARA(004) TYPE C,  "Gen item cat group
    BRGEW(017) TYPE C,  "Gross weight
    GEWEI(003) TYPE C,  "Weight unit
    NTGEW(017) TYPE C,  "Net weight
    GROES(032) TYPE C,  "Size/Dimensions
    MAGRV(004) TYPE C,  "Matl grp pack matls
    BISMT(018) TYPE C,  "Old material number
    WRKST(048) TYPE C,  "Basic material
    PROFL(003) TYPE C,  "DG indicator profile
    KZUMW(001) TYPE C,  "Environmentally rlvt
    BSTME(003) TYPE C,  "Order unit
    VABME(001) TYPE C,
    EKGRP(003) TYPE C,  "Purchasing group
    XCHPF(001) TYPE C,  "Batch management
    EKWSL(004) TYPE C,  "Purchasing key value
    WEBAZ(003) TYPE C,  "GR processing time
    MFRPN(040) TYPE C,  "Manufacturer part number
    MFRNR(010) TYPE C,  "Manufacturer number
    VPRSV(001) TYPE C,  "Price control indicator
    STPRS(015) TYPE C,  "Standard price
    BWPRH(014) TYPE C,  "Commercial price1
    BKLAS(004) TYPE C,  "Valuation class
  END OF LSMW_MATERIAL_MASTER.


**********************************************************************
*                INTERNAL TABLE DECLARATIONS                         *
**********************************************************************

*to store the input data
DATA:
  BEGIN OF it_matmaster OCCURS 0.
        INCLUDE STRUCTURE LSMW_MATERIAL_MASTER.

DATA:
  END OF it_matmaster.

*for material description
DATA:BEGIN OF IT_MATERIALDESC OCCURS 0.
     INCLUDE STRUCTURE BAPI_MAKT .
DATA:END OF IT_MATERIALDESC.

*to return messages
DATA:BEGIN OF IT_RETURN OCCURS 0.
     INCLUDE STRUCTURE BAPIRET2.
DATA:END OF IT_RETURN.

************************************************************************
*  SELECTION SCREEN
************************************************************************
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-002.

PARAMETERS:P_FILE LIKE RLGRAP-FILENAME OBLIGATORY.
SELECTION-SCREEN END OF BLOCK B1 .

************************************************************************
* AT SELECTION SCREEN
************************************************************************
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      PROGRAM_NAME  = SYST-CPROG
      DYNPRO_NUMBER = SYST-DYNNR
      FIELD_NAME    = 'P_FILE'
    IMPORTING
      FILE_NAME     = P_FILE.



**********************************************************************
*                TO UPLOAD THE DATA                                  *
**********************************************************************

START-OF-SELECTION.
V_FILE = P_FILE.
CALL FUNCTION 'GUI_UPLOAD'
  EXPORTING
    filename                      = V_FILE
   FILETYPE                      = 'ASC'
   HAS_FIELD_SEPARATOR           = 'X'
*   HEADER_LENGTH                 = 0
*   READ_BY_LINE                  = 'X'
*   DAT_MODE                      = ' '
* IMPORTING
*   FILELENGTH                    =
*   HEADER                        =
  tables
    data_tab                      =  IT_MATMASTER
 EXCEPTIONS
   FILE_OPEN_ERROR               = 1
   FILE_READ_ERROR               = 2
   NO_BATCH                      = 3
   GUI_REFUSE_FILETRANSFER       = 4
   INVALID_TYPE                  = 5
   NO_AUTHORITY                  = 6
   UNKNOWN_ERROR                 = 7
   BAD_DATA_FORMAT               = 8
   HEADER_NOT_ALLOWED            = 9
   SEPARATOR_NOT_ALLOWED         = 10
   HEADER_TOO_LONG               = 11
   UNKNOWN_DP_ERROR              = 12
   ACCESS_DENIED                 = 13
   DP_OUT_OF_MEMORY              = 14
   DISK_FULL                     = 15
   DP_TIMEOUT                    = 16
   OTHERS                        = 17
          .
IF sy-subrc <> 0.
 MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

**********************************************************************
*                DATA POPULATIONS                                    *
**********************************************************************


LOOP AT  IT_MATMASTER.
  BAPIMATHEAD-MATERIAL = IT_MATMASTER-MATNR.
  BAPIMATHEAD-IND_SECTOR = IT_MATMASTER-Mbrsh.
  BAPIMATHEAD-MATL_TYPE = IT_MATMASTER-Mtart.
  BAPIMATHEAD-BASIC_VIEW = 'X'.
  BAPIMATHEAD-PURCHASE_VIEW = 'X'.
  BAPIMATHEAD-ACCOUNT_VIEW = 'X'.
  BAPI_MARA-MATL_GROUP = IT_MATMASTER-MATKL.
  BAPI_MARA-OLD_MAT_NO = IT_MATMASTER-BISMT.
  BAPI_MARA-BASE_UOM = IT_MATMASTER-MEINS.
  BAPI_MARA-BASIC_MATL = IT_MATMASTER-WRKST.
  BAPI_MARA-MFR_NO = IT_MATMASTER-MFRNR.
  BAPI_MARAX-MATL_GROUP = 'X'.
  BAPI_MARAX-OLD_MAT_NO = 'X'.
  BAPI_MARAX-BASE_UOM = 'X'.
  BAPI_MARAX-BASIC_MATL = 'X'.
  BAPI_MARAX-MFR_NO = 'X'.
  BAPI_MARC-PLANT = IT_MATMASTER-WERKS.
  BAPI_MARC-PUR_GROUP = IT_MATMASTER-EKGRP.
  BAPI_MARCX-PLANT = IT_MATMASTER-WERKS.
  BAPI_MARCX-PUR_GROUP = 'X'.
  IT_MATERIALDESC-LANGU = 'EN'.
  IT_MATERIALDESC-MATL_DESC = IT_MATMASTER-MAKTX.
  append IT_materialdesc.
  CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
    EXPORTING
      headdata                   = BAPIMATHEAD
      CLIENTDATA                 = BAPI_MARA
      CLIENTDATAX                = BAPI_MARAx
     PLANTDATA                  = BAPI_MARc
     PLANTDATAX                 = BAPI_MARcx
*     FORECASTPARAMETERS         =
*     FORECASTPARAMETERSX        =
*     PLANNINGDATA               =
*     PLANNINGDATAX              =
*     STORAGELOCATIONDATA        =
*     STORAGELOCATIONDATAX       =
*     VALUATIONDATA              =
*     VALUATIONDATAX             =
*     WAREHOUSENUMBERDATA        =
*     WAREHOUSENUMBERDATAX       =
*     SALESDATA                  =
*     SALESDATAX                 =
*     STORAGETYPEDATA            =
*     STORAGETYPEDATAX           =
*     FLAG_ONLINE                = ' '
*     FLAG_CAD_CALL              = ' '
   IMPORTING
     RETURN                     = IT_RETURN
   TABLES
     MATERIALDESCRIPTION        = IT_MATERIALDESC
*     UNITSOFMEASURE             =
*     UNITSOFMEASUREX            =
*     INTERNATIONALARTNOS        =
*     MATERIALLONGTEXT           =
*     TAXCLASSIFICATIONS         =
*     RETURNMESSAGES             =
*     PRTDATA                    =
*     PRTDATAX                   =
*     EXTENSIONIN                =
*     EXTENSIONINX               =
            .
if sy-subrc = 0.
  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
* EXPORTING
*   WAIT          =
* IMPORTING
*   RETURN        =
            .
endif.
 WRITE:/    IT_RETURN-TYPE,
        2   IT_RETURN-ID,
        22  IT_RETURN-NUMBER,
        25  IT_RETURN-MESSAGE.
*            IT_RETURN-LOG_NO,
*            IT_RETURN-LOG_MSG_NO,
*            IT_RETURN-MESSAGE_V1,
*            IT_RETURN-MESSAGE_V2,
*            IT_RETURN-MESSAGE_V3,
*            IT_RETURN-MESSAGE_V4,
*            IT_RETURN-PARAMETER,
*            IT_RETURN-ROW,
*            IT_RETURN-FIELD,
*            IT_RETURN-SYSTEM.

ENDLOOP.

Regards

vijay

Former Member
0 Kudos

Hi !

Bapis are function modules that can "simply" be called by a CALL FUNCTION from ABAP-Code....

You can use the SAP-BAPI-Exporer (T-Code BAPI) to find usefull bapis and see what they do and how thier parametes should be filled.

Regards

Rainer

Some forum points would be fine if that helped you a bit...