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 Problem

Former Member
0 Likes
626

Hi all,

I m totally new to bdc & bapi.

Can anybody tell me which one is better 'bdc' or 'bapi' for

extending the material codes to other plant with transection 'mm01'.

Best Regards,

Aastha

5 REPLIES 5
Read only

Former Member
0 Likes
606

I will prefer BAPI and for all complex transactions where in more screens come in we should go for BAPI.Here is the I found for MM01 transaction,see it may help u :


*&---------------------------------------------------------------------*
REPORT ZKAR_MATMAS_BAPI.
*----------------------------------------------------------------------*

* TABLES

*----------------------------------------------------------------------*
*----------------------------------------------------------------------*

* FLAGS *

*----------------------------------------------------------------------*
DATA: F_STOP. " Flag used to stop processing
*----------------------------------------------------------------------*

* DATA DECLARATIONS *

*----------------------------------------------------------------------*
DATA : V_EMPTY TYPE I, " No. of empty records

V_TOTAL TYPE I. " Total no. of records.
*----------------------------------------------------------------------*

* STRUCTURES & INTERNAL TABLES

*----------------------------------------------------------------------*
*--- BAPI structures
DATA: BAPI_HEAD LIKE BAPIMATHEAD, " Header Segment with Control Information

BAPI_MAKT LIKE BAPI_MAKT, " Material Description

BAPI_MARA1 LIKE BAPI_MARA, " Client Data

BAPI_MARAX LIKE BAPI_MARAX, " Checkbox Structure for BAPI_MARA

BAPI_MARC1 LIKE BAPI_MARC, " Plant View

BAPI_MARCX LIKE BAPI_MARCX, " Checkbox Structure for BAPI_MARC

BAPI_MBEW1 LIKE BAPI_MBEW, " Accounting View

BAPI_MBEWX LIKE BAPI_MBEWX, " Checkbox Structure for BAPI_MBEW

BAPI_RETURN LIKE BAPIRET2. " Return Parameter

*--- Internal table to hold excel file data

DATA: IT_INTERN TYPE ALSMEX_TABLINE OCCURS 0 WITH HEADER LINE.

*--- Internal table to hold Matetrial descriptions

DATA: BEGIN OF IT_MAKT OCCURS 100.

INCLUDE STRUCTURE BAPI_MAKT.

DATA: END OF IT_MAKT.

*--- Internal to hold the records in the text file

DATA : BEGIN OF IT_DATA OCCURS 100,

WERKS(4), " Plant

MTART(4), " Material type

MATNR(18), " Material number

MATKL(9) , " Material group

MBRSH(1), " Industry sector

MEINS(3), " Base unit of measure

GEWEI(3), " Weight Unit

SPART(2), " Division

EKGRP(3), " Purchasing group

VPRSV(1), " Price control indicator

STPRS(12), " Standard price

PEINH(3), " Price unit

SPRAS(2), " Language key

MAKTX(40), " Material description

END OF IT_DATA.

*----------------------------------------------------------------------*

* SELECTION SCREEN. *

*----------------------------------------------------------------------*

SELECTION-SCREEN BEGIN OF BLOCK SCR1 WITH FRAME TITLE TEXT-111.

PARAMETER : P_FILE TYPE RLGRAP-FILENAME OBLIGATORY DEFAULT " Input File

'C:\Material_master.XLS'.

PARAMETER : P_MAX(4) OBLIGATORY DEFAULT '100'. " no.of recs in a session

PARAMETERS: P_HEADER TYPE I DEFAULT 0. " Header Lines

PARAMETERS: P_BEGCOL TYPE I DEFAULT 1 NO-DISPLAY,

P_BEGROW TYPE I DEFAULT 1 NO-DISPLAY,

P_ENDCOL TYPE I DEFAULT 100 NO-DISPLAY,

P_ENDROW TYPE I DEFAULT 32000 NO-DISPLAY.

SELECTION-SCREEN END OF BLOCK SCR1.

*---------------------------------------------------------------------*

* AT SELECTION-SCREEN *

*---------------------------------------------------------------------*

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.

*--- Validating file

PERFORM VALIDATE_FILE USING P_FILE.

*----------------------------------------------------------------------*

* START-OF-SELECTION

*----------------------------------------------------------------------*

START-OF-SELECTION.

*--- Perform to convert the Excel data into an internal table

PERFORM CONVERT_XLS_ITAB.

IF NOT IT_DATA[] IS INITIAL.

*--- Perform to delete Header lines

PERFORM DELETE_HEADER_EMPTY_RECS.

ENDIF.

*----------------------------------------------------------------------*

* END OF SELECTION. *

*----------------------------------------------------------------------*

END-OF-SELECTION.

*--- Perform to upload Material Master data

PERFORM UPLOAD_MATMAS.

*----------------------------------------------------------------------*

* Form : validate_input_file

*----------------------------------------------------------------------*

* Description : To provide F4 help for file if read from PC

*----------------------------------------------------------------------*

FORM VALIDATE_FILE USING F_FILE TYPE RLGRAP-FILENAME.

CALL FUNCTION 'KD_GET_FILENAME_ON_F4'

CHANGING

FILE_NAME = F_FILE

EXCEPTIONS

MASK_TOO_LONG = 1

OTHERS = 2.

IF SY-SUBRC <> 0.

MESSAGE S010(ZLKPL_MSGCLASS). " 'Error in getting filename'.

ENDIF.

ENDFORM. " validate_input_file

*&---------------------------------------------------------------------*

*& Form CONVER_XLS_ITAB

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

FORM CONVERT_XLS_ITAB.

CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'

EXPORTING

FILENAME = P_FILE

I_BEGIN_COL = P_BEGCOL

I_BEGIN_ROW = P_BEGROW

I_END_COL = P_ENDCOL

I_END_ROW = P_ENDROW

TABLES

INTERN = IT_INTERN.

IF SY-SUBRC <> 0.

* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

*--- Perform to move the data into an internal data

PERFORM MOVE_DATA.

ENDFORM. " CONVERT_XLS_ITAB

*&---------------------------------------------------------------------*

*& Form MOVE_DATA

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

FORM MOVE_DATA.

DATA : LV_INDEX TYPE I.

FIELD-SYMBOLS .

*--- Sorting the internal table

SORT IT_INTERN BY ROW COL.

CLEAR IT_INTERN.

LOOP AT IT_INTERN.

MOVE IT_INTERN-COL TO LV_INDEX.

*--- Assigning the each record to an internal table row

ASSIGN COMPONENT LV_INDEX OF STRUCTURE IT_DATA TO .

*--- Asigning the field value to a field symbol

MOVE IT_INTERN-VALUE TO .

AT END OF ROW.

APPEND IT_DATA.

CLEAR IT_DATA.

ENDAT.

ENDLOOP.

ENDFORM. " MOVE_DATA

*&---------------------------------------------------------------------*

*& Form DELETE_HEADER_EMPTY_RECS

*&---------------------------------------------------------------------*

* To delete the Header and empty records

*----------------------------------------------------------------------*

FORM DELETE_HEADER_EMPTY_RECS.

DATA: LV_TABIX LIKE SY-TABIX.

IF NOT P_HEADER IS INITIAL.

LOOP AT IT_DATA.

IF P_HEADER > 0 AND NOT IT_DATA IS INITIAL.

DELETE IT_DATA FROM 1 TO P_HEADER.

* P_HEADER = 0.

EXIT.

ENDIF.

ENDLOOP.

ENDIF.

CLEAR IT_DATA.

*--- To delete the empty lines from internal table

LOOP AT IT_DATA.

LV_TABIX = SY-TABIX.

IF IT_DATA IS INITIAL.

V_EMPTY = V_EMPTY + 1.

DELETE IT_DATA INDEX LV_TABIX..

ENDIF.

ENDLOOP.

CLEAR IT_DATA.

*--- Total no of recs in file

DESCRIBE TABLE IT_DATA LINES V_TOTAL.

IF V_TOTAL = 0.

MESSAGE I013(ZLKPL_MSGCLASS). " No records in the file

F_STOP = 'X'.

STOP.

ENDIF.

ENDFORM. " DELETE_HEADER_EMPTY_RECS

*&---------------------------------------------------------------------*

*& Form UPLOAD_MATMAS

*&---------------------------------------------------------------------*

* to upload Material Master data

*----------------------------------------------------------------------*

FORM UPLOAD_MATMAS .

LOOP AT IT_DATA.

* Header

BAPI_HEAD-MATERIAL = IT_DATA-MATNR.

BAPI_HEAD-IND_SECTOR = IT_DATA-MBRSH.

BAPI_HEAD-MATL_TYPE = IT_DATA-MTART.

BAPI_HEAD-BASIC_VIEW = 'X'.

BAPI_HEAD-PURCHASE_VIEW = 'X'.

BAPI_HEAD-ACCOUNT_VIEW = 'X'.

* Material Description

REFRESH IT_MAKT.

IT_MAKT-LANGU = IT_DATA-SPRAS.

IT_MAKT-MATL_DESC = IT_DATA-MAKTX.

APPEND IT_MAKT.

* Client Data - Basic

BAPI_MARA1-MATL_GROUP = IT_DATA-MATKL.

BAPI_MARA1-BASE_UOM = IT_DATA-MEINS.

BAPI_MARA1-UNIT_OF_WT = IT_DATA-GEWEI.

BAPI_MARA1-DIVISION = IT_DATA-SPART.

BAPI_MARAX-MATL_GROUP = 'X'.

BAPI_MARAX-BASE_UOM = 'X'.

BAPI_MARAX-UNIT_OF_WT = 'X'.

BAPI_MARAX-DIVISION = 'X'.

* Plant - Purchasing

BAPI_MARC1-PLANT = IT_DATA-WERKS.

BAPI_MARC1-PUR_GROUP = IT_DATA-EKGRP.

BAPI_MARCX-PLANT = IT_DATA-WERKS.

BAPI_MARCX-PUR_GROUP = 'X'.

* Accounting

BAPI_MBEW1-VAL_AREA = IT_DATA-WERKS.

BAPI_MBEW1-PRICE_CTRL = IT_DATA-VPRSV.

BAPI_MBEW1-STD_PRICE = IT_DATA-STPRS.

BAPI_MBEW1-PRICE_UNIT = IT_DATA-PEINH.

BAPI_MBEWX-VAL_AREA = IT_DATA-WERKS.

BAPI_MBEWX-PRICE_CTRL = 'X'.

BAPI_MBEWX-STD_PRICE = 'X'.

BAPI_MBEWX-PRICE_UNIT = 'X'.

*--- BAPI to create material

call function 'BAPI_MATERIAL_SAVEDATA'

exporting

HEADDATA = BAPI_HEAD

CLIENTDATA = BAPI_MARA1

CLIENTDATAX = BAPI_MARAX

PLANTDATA = BAPI_MARC1

PLANTDATAX = BAPI_MARCX

* FORECASTPARAMETERS =

* FORECASTPARAMETERSX =

* PLANNINGDATA =

* PLANNINGDATAX =

* STORAGELOCATIONDATA =

* STORAGELOCATIONDATAX =

* VALUATIONDATA = BAPI_MBEW1

* VALUATIONDATAX = BAPI_MBEWX

* WAREHOUSENUMBERDATA =

* WAREHOUSENUMBERDATAX =

* SALESDATA = BAPI_MVKE1

* SALESDATAX = BAPI_MVKEX

* STORAGETYPEDATA =

* STORAGETYPEDATAX =

IMPORTING

RETURN = BAPI_RETURN

TABLES

MATERIALDESCRIPTION = IT_MAKT

* UNITSOFMEASURE =

* UNITSOFMEASUREX =

* INTERNATIONALARTNOS =

* MATERIALLONGTEXT =

* TAXCLASSIFICATIONS =

* RETURNMESSAGES =

* PRTDATA =

* PRTDATAX =

* EXTENSIONIN =

* EXTENSIONINX =

.

IF BAPI_RETURN-TYPE = 'E'.

WRITE:/ 'Error:' ,BAPI_RETURN-MESSAGE ,'for material:' ,it_data-matnr.

ELSEIF BAPI_RETURN-TYPE = 'S'.

WRITE: 'Successfully created material' ,it_data-matnr.

ENDIF.

ENDLOOP.

ENDFORM. " UPLOAD_MATMAS

Read only

0 Likes
606

Hi Rock,

Thanks 4 ur reply.

As i had already inform that i m new to bdc & bapi & even function module.

I want to know a newbie like me can judge , what parameters need to pass & which one to remain commented.

Best Regards,

Aastha

Read only

Former Member
0 Likes
606

Hi,

SAP recommends BAPI over BDC.

The reason being during upgrade their is a possibility of screen flow being changed, which might affect the existing logic of BDC programs.

Best regards,

Prashant

Read only

Former Member
0 Likes
606

Dear Astha,

Bapi's are prefered in current scenario compare to BDC's. For material creation or extension to plants u can use this specific BAPI with structures described as follows.

U need to handle these structures properly.( Exmp. For each and every field that u are entering in plant data u need to mark 'X' those fields in plantdatax to get updated in database ).

One of the key reason to prefer BAPI's on BDC's:

BDC - Deals with Screens of SAP Transactions.

BAPI's - Deals with tables DB. ( Updates irrelavant to Trns)

CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'

EXPORTING

headdata = i_mathead

plantdata = i_plant

plantdatax = i_plantx

storagelocationdata = i_storage

storagelocationdatax = i_storagex

valuationdata = i_valuation

valuationdatax = i_valuationx

salesdata = i_sales

salesdatax = i_salesx

IMPORTING

return = i_return1

TABLES

materialdescription = it_maktx

taxclassifications = it_tax.

Feel free to revert for more concerns on BAPI's . Also provide the points if it helps u .

Read only

0 Likes
606

Hello Gaurav

Can u tell that , when i goto fun module 'BAPI_MATERIAL_SAVEDATA' , there is

1)RFC Target sys

2) No Of Import parameters

3)No ofTables

And u select few import paramets and few tables.

What is the criteria to judge which parameter And table is 2 b taken.

Best Regards,

Aastha